stages: - 'test' - 'build' - 'push' pytest: stage: 'test' image: python only: - test cache: paths: - $CI_PROJECT_DIR/venv/ before_script: - cd $CI_PROJECT_DIR - python -m venv venv - chmod u+x venv/bin/activate - source venv/bin/activate - pip install --upgrade --quiet pip - pip install --quiet -U -r requirements.txt - pip install --quiet -U -r tests/requirements.txt script: - pytest -v variables: # fill those if you have a proxy in your environment DOCKER_DRIVER: overlay2 # See https://github.com/docker-library/docker/pull/166 DOCKER_TLS_CERTDIR: "" make_image: stage: build image: docker:26.1.3-dind services: - docker:26.1.3-dind before_script: - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY script: # fetches the latest image (not failing if image is not found) - docker pull $CI_REGISTRY_IMAGE:latest || true # builds the project, passing proxy variables, using OCI labels # notice the cache-from, which is going to use the image we just pulled locally # the built image is tagged locally with the commit SHA, and then pushed to # the GitLab registry - > DOCKER_BUILDKIT=1 docker build --pull --label "org.opencontainers.image.title=$CI_PROJECT_TITLE" --label "org.opencontainers.image.url=$CI_PROJECT_URL" --label "org.opencontainers.image.created=$CI_JOB_STARTED_AT" --label "org.opencontainers.image.revision=$CI_COMMIT_SHA" --label "org.opencontainers.image.version=$CI_COMMIT_REF_NAME" --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA . - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA # Here, the goal is to tag the "master" branch as "latest" Push latest: needs: - make_image variables: # We are just playing with Docker here. # We do not need GitLab to clone the source code. GIT_STRATEGY: none stage: push script: # Because we have no guarantee that this job will be picked up by the same runner # that built the image in the previous step, we pull it again locally - docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA # Then we tag it "latest" - docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:latest # Annnd we push it. - docker push $CI_REGISTRY_IMAGE:latest # Finally, the goal here is to Docker tag any Git tag # GitLab will start a new pipeline everytime a Git tag is created, which is pretty awesome Push commit: needs: - make_image variables: # Again, we do not need the source code here. Just playing with Docker. GIT_STRATEGY: none stage: push script: - docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA - docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME Push tag: needs: - make_image variables: # Again, we do not need the source code here. Just playing with Docker. GIT_STRATEGY: none stage: push only: # We want this job to be run on tags only. - tags script: - docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA - docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME