Last week I tested google functions at gcp using Go, after that, I decided to add another service I like gitlab. 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.
Click on the button "+ CREATE SERVICE ACCOUNT"
Type a name to identify the account
Check that JSON is checked and click on the create button
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
Ok now we will create the code to test our deploy.I'm using the examples from Google
We will create 3 files, hello_world.go that is the function
// 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
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
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
That's all folks, I hope it can be useful to you =). You can see the code in the repository
Top comments (0)