Skip to content

Instantly share code, notes, and snippets.

@cpxPratik
Forked from foklepoint/.gitlab-ci.yml
Created January 14, 2020 07:49
Show Gist options
  • Save cpxPratik/ba2e4b4086e97a80d0bb0e23cc470075 to your computer and use it in GitHub Desktop.
Save cpxPratik/ba2e4b4086e97a80d0bb0e23cc470075 to your computer and use it in GitHub Desktop.
Build and Push images to GCP Container Registry with Gitlab CI
image: docker:latest
# When using dind, it's wise to use the overlayfs driver for
# improved performance.
variables:
DOCKER_DRIVER: overlay
GCP_PROJECT_ID: CHANGE-TO-GCP-PROJECT-ID
IMAGE_NAME: image_id
services:
- docker:dind
stages:
- publish
publish-image:
stage: publish
script:
# Install CA certs, openssl to https downloads, python for gcloud sdk
- apk add --update make ca-certificates openssl python
- update-ca-certificates
# Write our GCP service account private key into a file
- echo $GCLOUD_SERVICE_KEY | base64 -d > ${HOME}/gcloud-service-key.json
# Download and install Google Cloud SDK
- wget https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz
- tar zxvf google-cloud-sdk.tar.gz && ./google-cloud-sdk/install.sh --usage-reporting=false --path-update=true
- google-cloud-sdk/bin/gcloud --quiet components update
- google-cloud-sdk/bin/gcloud auth activate-service-account --key-file ${HOME}/gcloud-service-key.json
# Create our image. Expected to create an image 'image_id'
- make-my-image
# Tag our image for container registry
- docker tag $IMAGE_NAME gcr.io/$GCP_PROJECT_ID/$IMAGE_NAME
# Optionally tag the image with the commit short-sha
- docker tag $IMAGE_NAME gcr.io/$GCP_PROJECT_ID/$IMAGE_NAME:$(echo $CI_COMMIT_SHA | cut -c1-8)
- google-cloud-sdk/bin/gcloud docker -- push gcr.io/$GCP_PROJECT_ID/$IMAGE_NAME:latest
- google-cloud-sdk/bin/gcloud docker -- push gcr.io/$GCP_PROJECT_ID/$IMAGE_NAME:$(echo $CI_COMMIT_SHA | cut -c1-8)
# Only run builds for these refs
only:
- master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment