Commit 34e801b4 authored by Michael Friedrich's avatar Michael Friedrich Committed by Shinya Maeda

Use the default branch for Docker image CI builds

This allows to use 'main' as default branch without
modifying the CI template.

It also uses CI rules instead of only/except.
parent 2714854b
---
title: Unify the Docker Image build CI template and use the default branch instead
of hardcoded 'master'
merge_request: 51931
author: dnsmichi
type: changed
docker-build-master: # Build a Docker image with CI/CD and push to the GitLab registry.
# Official docker image. # Docker-in-Docker documentation: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html
image: docker:latest #
stage: build # This template uses one generic job with conditional builds
services: # for the default branch and all other (MR) branches.
- docker:dind
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
script:
- docker build --pull -t "$CI_REGISTRY_IMAGE" .
- docker push "$CI_REGISTRY_IMAGE"
only:
- master
docker-build: docker-build:
# Official docker image. # Use the official docker image.
image: docker:latest image: docker:latest
stage: build stage: build
services: services:
- docker:dind - docker:dind
before_script: before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
# Default branch leaves tag empty (= latest tag)
# All other branches are tagged with the escaped branch name (commit ref slug)
script: script:
- docker build --pull -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" . - |
- docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
except: tag=""
- master echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'"
else
tag=":$CI_COMMIT_REF_SLUG"
echo "Running on branch '$CI_COMMIT_BRANCH': tag = $tag"
fi
- docker build --pull -t "$CI_REGISTRY_IMAGE${tag}" .
- docker push "$CI_REGISTRY_IMAGE${tag}"
# Run this job in a branch where a Dockerfile exists
rules:
- if: $CI_COMMIT_BRANCH
exists:
- Dockerfile
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment