Go, Google Functions and Gitlab-ci a perfect combination
Tags: go, serverless, ci, gitlab
Published: 2019-01-19 17:27:03

---
title: Go, Google Functions and Gitlab-ci a perfect combination
published: true
description: A tutorial to show how to use Go in the google functions and do a deploy Gitlab-ci
tags: #go,#serverless,#ci,#gitlab
cover_image: https://docs.gitlab.com/ee/ci/img/cicd_pipeline_infograph.png
---

Last week I tested google functions at [gcp](https://cloud.google.com/) using Go, after that, I decided to add another service I like [gitlab](https://about.gitlab.com/). I liked it so much that I decided to write this post.

If you know how to add an account to do a deploy at GCP or you have an account, you can ignore this section.

## Creating an account to use at Gitlab.
I'll add a lot of screenshots to make it clear.
You need to access IAM & admin then Service accounts.
![Service Account](https://thepracticaldev.s3.amazonaws.com/i/li425pbjjwp44ifb9cbi.png)

Click on the button "+ CREATE SERVICE ACCOUNT"
![Service Account](https://thepracticaldev.s3.amazonaws.com/i/e3avlb1trnb5c5oph89n.png)

Type a name to identify the account
![Service Account](https://thepracticaldev.s3.amazonaws.com/i/jesqynonz1celsilrzmx.png)

You need to add 3 roles
![Service Account](https://thepracticaldev.s3.amazonaws.com/i/zhd3b5sfs5zdr5g4nfib.png)

Create a key
![Service Account](https://thepracticaldev.s3.amazonaws.com/i/0r5d9rzau63iqpq1q1pp.png)

Check that JSON is checked and click on  the create button
![Service Account](PIC 6)

---

We finish the config at GCP, now we will add the env vars in the repository. PROJECT_ID is the project's id and SERVICE_ACCOUNT is the content you do a download when you created your key 
![Service Account](https://thepracticaldev.s3.amazonaws.com/i/gds21jd33zpsesieyqpb.png)

Ok now we will create the code to test our deploy.I'm using the [examples from Google](https://github.com/GoogleCloudPlatform/golang-samples/tree/master/functions/helloworld)
We will create 3 files, hello_world.go that is the function 
```go
// Package helloworld provides a set of Cloud Function samples.
package helloworld

import (
	"fmt"
	"net/http"
)

// HelloGet is an HTTP Cloud Function.
func HelloGet(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hello, World!")
}
```
hello_world_test.go that is the test of the function
```go

package helloworld

import (
	"io/ioutil"
	"net/http/httptest"
	"strings"
	"testing"
)

func TestHelloGet(t *testing.T) {
	payload := strings.NewReader("")
	req := httptest.NewRequest("GET", "/", payload)

	rr := httptest.NewRecorder()
	HelloGet(rr, req)

	out, err := ioutil.ReadAll(rr.Result().Body)
	if err != nil {
		t.Fatalf("ReadAll: %v", err)
	}
	want := "Hello, World!"
	if got := string(out); got != want {
		t.Errorf("HelloWorld = %q, want %q", got, want)
	}
}
```
and .gitlab-ci.yml that is the config to run the continuous integration
```bash
image: google/cloud-sdk:alpine

test_production:
  image: golang:alpine
  stage: test
  only:
  - production
  script:
  - CGO_ENABLED=0 go test ./...

deploy_production:
  stage: deploy
  environment: Production
  only:
  - production
  script:
  - echo $SERVICE_ACCOUNT > /tmp/$CI_PIPELINE_ID.json
  - gcloud auth activate-service-account --key-file /tmp/$CI_PIPELINE_ID.json
  - gcloud --quiet --project $PROJECT_ID functions deploy HelloGet --runtime go111 --trigger-http
  after_script:
  - rm /tmp/$CI_PIPELINE_ID.json
```
The workflow is, the actions will run when we merged changes in production branch. 

That's all, as you can see in the next screenshot, the deploy was done and the return from Google shows the URL created

![Service Account](https://thepracticaldev.s3.amazonaws.com/i/ptb9e4zladmqjhfz7397.png)

That's all folks, I hope it can be useful to you =). You can see the code in the [repository](https://gitlab.com/renatosuero/gcp-functions-example)