Published on: January 28, 2025
4 min read
This tutorial shows how to use GitLab’s Google Cloud integration to deploy a Golang server in less than 10 minutes, helping developers become more independent and efficient.
Deploying an application to the cloud often requires assistance from production or DevOps engineers. GitLab's Google Cloud integration empowers developers to handle deployments independently. In this tutorial, you'll learn how to deploy a server to Google Cloud in less than 10 minutes using Go. Whether you’re a solo developer or part of a large team, this setup allows you to deploy applications efficiently.
main.go
We decided to call our project golang-cloud-run
for simplicity.
main.go
demo.Find the main.go
demo here.
// Sample run-helloworld is a minimal Cloud Run service.
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
log.Print("starting server...")
http.HandleFunc("/", handler)
// Determine port for HTTP service.
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("defaulting to port %s", port)
}
// Start HTTP server.
log.Printf("listening on port %s", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
name := os.Getenv("NAME")
if name == "" {
name = "World"
}
fmt.Fprintf(w, "Hello %s!\n", name)
}
Navigate to Operate > Google Cloud > Create Service account.
This merge request adds a CI/CD deployment job to your pipeline definition. In our case, this is also creating a pipeline definition, as we didn’t have one before.
Note: The CI/CD variables GCP_PROJECT_ID
, GCP_REGION
, GCP_SERVICE_ACCOUNT
, GCP_SERVICE_ACCOUNT_KEY
will all be automatically populated from the previous steps.
Alternatively, you can navigate to Operate > Environments to see a list of deployments for your environments.
By clicking on the environment called main, you’ll be able to view a complete list of deployments specific to that environment.
To get started with developing your Go application, try adding another endpoint. For instance, in your main.go
file, you can add a /bye
endpoint as shown below (don’t forget to register the new handler function in main!):
func main() {
log.Print("starting server...")
http.HandleFunc("/", handler)
http.HandleFunc("/bye", byeHandler)
func byeHandler(w http.ResponseWriter, r *http.Request) {
name := os.Getenv("NAME")
if name == "" {
name = "World"
}
fmt.Fprintf(w, "Bye %s!\n", name)
}
Your main.go
file should now look something like this:
// Sample run-helloworld is a minimal Cloud Run service.
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
log.Print("starting server...")
http.HandleFunc("/", handler)
http.HandleFunc("/bye", byeHandler)
// Determine port for HTTP service.
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("defaulting to port %s", port)
}
// Start HTTP server.
log.Printf("listening on port %s", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
name := os.Getenv("NAME")
if name == "" {
name = "World"
}
fmt.Fprintf(w, "Hello %s!\n", name)
}
func byeHandler(w http.ResponseWriter, r *http.Request) {
name := os.Getenv("NAME")
if name == "" {
name = "World"
}
fmt.Fprintf(w, "Bye %s!\n", name)
}
Push the changes to the repo, and watch the deploy-to-cloud-run job
deploy the updates. Once it’s complete, go back to the Service URL and navigate to the /bye
endpoint to see the new functionality in action.
To prevent incurring charges on your Google Cloud account for the resources used in this tutorial, you can either delete the specific resources or delete the entire Google Cloud project. For detailed instructions, refer to the cleanup guide.
Discover more tutorials like this in our Solutions Architecture area.