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.
You'll learn how to:
- Create a new project in GitLab
- Create a Go server utilizing
main.go
- Use the Google Cloud integration to create a Service account
- Use the Google Cloud integration to create Cloud Run via a merge request
- Access your newly deployed Go server
- Clean up your environment
Prerequisites
- Owner access on a Google Cloud Platform project
- Working knowledge of Golang
- Working knowledge of GitLab CI
- 10 minutes
Step-by-step Golang server deployment to Google Cloud
1. Create a new blank project in GitLab.
We decided to call our project golang-cloud-run
for simplicity.
2. Create a server utilizing this 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)
}
3. Use the Google Cloud integration to create a Service account.
Navigate to Operate > Google Cloud > Create Service account.
4. Configure the region you would like the Cloud Run instance deployed to.
5. Use the Google Cloud integration to configure Cloud Run via Merge Request.
6. This will open a merge request. Immediately merge the MR.
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.
7. Voila! Check your pipeline and you will see you have successfully deployed to Google Cloud Run utilizing GitLab CI.
8. Click the Service URL to view your newly deployed server.
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.
Next steps
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.
Clean up the environment
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.