|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + # This is a GitLab CI configuration to build the project as a docker image |
| 5 | +# The file is generic enough to be dropped in a project containing a working Dockerfile |
| 6 | +# Author: Florent CHAUVEAU <[email protected]> |
| 7 | +# Mentioned here: https://blog.callr.tech/building-docker-images-with-gitlab-ci-best-practices/ |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +stages: |
| 12 | + - build |
| 13 | + - test |
| 14 | + - push |
| 15 | + |
| 16 | +.docker: |
| 17 | + # do not use "latest" here, if you want this to work in the future |
| 18 | + image: docker:20 |
| 19 | + services: |
| 20 | + - docker:20-dind |
| 21 | + # Use this if your GitLab runner does not use socket binding |
| 22 | + # services: |
| 23 | + # - docker:dind |
| 24 | + |
| 25 | + before_script: |
| 26 | + # docker login asks for the password to be passed through stdin for security |
| 27 | + # we use $CI_REGISTRY_PASSWORD here which is a special variable provided by GitLab |
| 28 | + # https://docs.gitlab.com/ce/ci/variables/predefined_variables.html |
| 29 | + - echo -n $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY |
| 30 | + |
| 31 | +Build: |
| 32 | + extends: .docker |
| 33 | + stage: build |
| 34 | + script: |
| 35 | + # fetches the latest image (not failing if image is not found) |
| 36 | + - docker pull $CI_REGISTRY_IMAGE:latest || true |
| 37 | + # builds the project, passing proxy variables, using OCI labels |
| 38 | + # notice the cache-from, which is going to use the image we just pulled locally |
| 39 | + # the built image is tagged locally with the commit SHA, and then pushed to |
| 40 | + # the GitLab registry |
| 41 | + - > |
| 42 | + docker build |
| 43 | + --pull |
| 44 | + --cache-from $CI_REGISTRY_IMAGE:latest |
| 45 | + --label "org.opencontainers.image.title=$CI_PROJECT_TITLE" |
| 46 | + --label "org.opencontainers.image.url=$CI_PROJECT_URL" |
| 47 | + --label "org.opencontainers.image.created=$CI_JOB_STARTED_AT" |
| 48 | + --label "org.opencontainers.image.revision=$CI_COMMIT_SHA" |
| 49 | + --label "org.opencontainers.image.version=$CI_COMMIT_REF_NAME" |
| 50 | + --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA |
| 51 | + . |
| 52 | + - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA |
| 53 | + |
| 54 | +include: |
| 55 | + - local: __tests__/.gitlab-ci.yml |
| 56 | + |
| 57 | +validate-wrapper: |
| 58 | + image: |
| 59 | + name: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA |
| 60 | + entrypoint: [""] |
| 61 | + |
| 62 | +# Here, the goal is to tag the "master" branch as "latest" |
| 63 | +Push latest: |
| 64 | + extends: .docker |
| 65 | + variables: |
| 66 | + # We are just playing with Docker here. |
| 67 | + # We do not need GitLab to clone the source code. |
| 68 | + GIT_STRATEGY: none |
| 69 | + stage: push |
| 70 | + only: |
| 71 | + # Only "master" should be tagged "latest" |
| 72 | + - master |
| 73 | + script: |
| 74 | + # Because we have no guarantee that this job will be picked up by the same runner |
| 75 | + # that built the image in the previous step, we pull it again locally |
| 76 | + - docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA |
| 77 | + # Then we tag it "latest" |
| 78 | + - docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:latest |
| 79 | + # Annnd we push it. |
| 80 | + - docker push $CI_REGISTRY_IMAGE:latest |
| 81 | + |
| 82 | +# Finally, the goal here is to Docker tag any Git tag |
| 83 | +# GitLab will start a new pipeline everytime a Git tag is created, which is pretty awesome |
| 84 | +Push tag: |
| 85 | + extends: .docker |
| 86 | + variables: |
| 87 | + # Again, we do not need the source code here. Just playing with Docker. |
| 88 | + GIT_STRATEGY: none |
| 89 | + stage: push |
| 90 | + only: |
| 91 | + # We want this job to be run on tags only. |
| 92 | + - tags |
| 93 | + script: |
| 94 | + - docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA |
| 95 | + - docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME |
| 96 | + - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME |
0 commit comments