bitbucket-pipelines.yml - Deploy Google App Engine (Golang)

Die Anleitung von Google hat leider nicht ausgereicht, um mit Bitbucket Pipelines die Beispiel App auf GCP bereitzustellen. Die Anleitung gibt aber schon einmal einen guten Überblick über die nötige Konfiguration für GCP.

Die entsprechende bitbucket-pipelines.yml sieht dann so aus:

image: golang:1.8

pipelines:
  branches:
    toapp:
      - step:
          script: 
            - PACKAGE_PATH="${GOPATH}/src/bitbucket.org/${BITBUCKET_REPO_OWNER}/${BITBUCKET_REPO_SLUG}"
            - mkdir -pv "${PACKAGE_PATH}"
            - mkdir -pv "${GOPATH}/pkg"
            - mkdir -pv "${GOPATH}/bin"
            - tar -cO --exclude-vcs --exclude=bitbucket-pipelines.yml . | tar -xv -C "${PACKAGE_PATH}"
            - cd "${PACKAGE_PATH}"
            - go get
            - go build
            - cd "${PACKAGE_PATH}"
            # Downloading the Google Cloud SDK
            - SDK_VERSION=197.0.0
            - SDK_FILENAME=google-cloud-sdk-${SDK_VERSION}-linux-x86_64.tar.gz
            - curl -o /tmp/google-cloud-sdk.tar.gz https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/${SDK_FILENAME}
            - tar -xvf /tmp/google-cloud-sdk.tar.gz -C /tmp/
            - /tmp/google-cloud-sdk/install.sh -q
            - source /tmp/google-cloud-sdk/path.bash.inc
            # Authenticating with the service account key file
            - gcloud components install app-engine-go
            - echo ${GOOGLE_CLIENT_SECRET} > client-secret.json
            - gcloud auth activate-service-account --key-file client-secret.json          # Linking to the Google Cloud project
            - gcloud config set project $CLOUDSDK_CORE_PROJECT
            - gcloud app deploy app.yaml

Zunächst werden die Abhänigkieten vom Go Programm mit go get und go build bereitgestellt. Ohne cd “${PACKAGE_PATH}" gibt es einen Fehler von go install (Welches von go build aufgerufen wird).

- PACKAGE_PATH="${GOPATH}/src/bitbucket.org/${BITBUCKET_REPO_OWNER}/${BITBUCKET_REPO_SLUG}"
- mkdir -pv "${PACKAGE_PATH}"
- mkdir -pv "${GOPATH}/pkg"
- mkdir -pv "${GOPATH}/bin"
- tar -cO --exclude-vcs --exclude=bitbucket-pipelines.yml . | tar -xv -C "${PACKAGE_PATH}"
- cd "${PACKAGE_PATH}"
- go get
- go build
- cd "${PACKAGE_PATH}"

Es folgt die Installtion von Google Cloud SDK und der app-engine-go Komponente. Die Version 197.0.0 funktioniert, 127.0.0 hatte wohl noch einen Bug. Man könnte auch einfach die aktuelle nehmen …

- SDK_VERSION=197.0.0
- SDK_FILENAME=google-cloud-sdk-${SDK_VERSION}-linux-x86_64.tar.gz
- curl -o /tmp/google-cloud-sdk.tar.gz https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/${SDK_FILENAME}
- tar -xvf /tmp/google-cloud-sdk.tar.gz -C /tmp/
- /tmp/google-cloud-sdk/install.sh -q
- source /tmp/google-cloud-sdk/path.bash.inc
- gcloud components install app-engine-go

Und dann zuletzt, setzen der Authentifizierung und Deploy :)

- echo ${GOOGLE_CLIENT_SECRET} > client-secret.json
- gcloud auth activate-service-account --key-file client-secret.json          # Linking to the Google Cloud project
- gcloud config set project $CLOUDSDK_CORE_PROJECT
- gcloud app deploy app.yaml

Die Bereitstellung ist auf den Branch toapp eingegrenzt, so wird nicht jeder push auf origin deployed.

pipelines:
  branches:
    toapp:

Dies ist schon einmal ein guter Start und funktioniert. Ein Deploy dauert ca. 1,5 Minuten. Am längsten dauert die Installation der app-engine-go Komponente, dies könnte man durch bereitstellen eines eigenen Docker Image auf Dockerhub noch verkürzen. Der Free Plan bei Bitbucket beinhaltet 50 Frei Minuten pro Monat.