Skip to content

Instantly share code, notes, and snippets.

@oofnikj
Last active October 9, 2019 14:48
Show Gist options
  • Save oofnikj/aff92570f6c1c2d6dae00d25c39014d2 to your computer and use it in GitHub Desktop.
Save oofnikj/aff92570f6c1c2d6dae00d25c39014d2 to your computer and use it in GitHub Desktop.
GitLab CI example with private image registry
stages:
- build
- test
- cleanup
- tag
variables:
# don't do checkout unless we need to.
# Tests run on the code already present in the image which was just built.
GIT_STRATEGY: none
IMAGE_REPO: gcr.io/${CLOUDSDK_CORE_PROJECT}/app/${CI_COMMIT_REF_NAME}
## Globally defined variables for project:
## 1. CLOUDSDK_CORE_PROJECT, the GCP project id,
## which is required for cloud-sdk image.
## 2. DOCKER_AUTH_CONFIG contains login credentials to private GCR.
## 3. GOOGLE_APPLICATION_CREDENTIALS contains base64-encoded
## service account file in JSON format.
.docker_build:
image: docker:stable
services:
- docker:19.03.1-dind
variables:
GIT_STRATEGY: fetch
before_script:
- cat $GOOGLE_APPLICATION_CREDENTIALS
| docker login -u _json_key --password-stdin https://gcr.io
after_script:
- docker logout gcr.io
.pytest:
image: ${IMAGE_REPO}:${CI_COMMIT_SHORT_SHA}
stage: test
artifacts:
reports:
junit: test_results/*.xml
.gcloud:
image: google/cloud-sdk:alpine
before_script:
- cat $GOOGLE_APPLICATION_CREDENTIALS | gcloud auth activate-service-account --key-file -
after_script:
- gcloud auth revoke
build app:
extends: .docker_build
stage: build
variables:
GIT_STRATEGY: fetch
script:
- docker build
--build-arg GIT_COMMIT=$CI_COMMIT_SHA
--build-arg GIT_BRANCH=$CI_COMMIT_REF_NAME
--build-arg BUILD_NUMBER=$CI_JOB_ID
-t $IMAGE_REPO:${CI_COMMIT_SHORT_SHA}
-f modules/app/docker/Dockerfile .
- docker push $IMAGE_REPO:${CI_COMMIT_SHORT_SHA}
unit tests:
extends: .pytest
script:
- pytest
--junit-xml=test_results/unit_tests.xml
--color=yes
-k 'unit_tests'
modules/app/test
integration tests:
extends: .pytest
script:
- pytest
--junit-xml=test_results/integration_tests.xml
--color=yes
-k 'integration_tests'
modules/app/test
regression tests:
extends: .pytest
script:
- pytest
--junit-xml=test_results/regression_tests.xml
--color=yes
-k 'regression_tests'
modules/app/test
Delete failed image:
extends: .gcloud
stage: cleanup
when: on_failure
script:
- gcloud container images delete
${IMAGE_REPO}:${CI_COMMIT_SHORT_SHA}
Tag latest:
extends: .gcloud
stage: tag
when: on_success
script:
- gcloud container images add-tag
${IMAGE_REPO}:${CI_COMMIT_SHORT_SHA} ${IMAGE_REPO}:latest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment