docker publish Gitlab CI/CD jobs Template
This GitLab CI template is designed to publish Docker images to Docker Hub. It contains a job called "Publish Docker Image to Docker Hub" that handles the image publishing process.
Stage
publish
Variables
The following variables are used in the job:
-
DOCKER_IMAGE_BUILD_NAME
: The name of the Docker image to build. By default, it uses the GitLab CI project name. -
DOCKER_IMAGE_BUILD_REGISTRY
: The registry for the Docker image build. By default, it uses the GitLab CI registry image. -
DOCKER_IMAGE_BUILD_TAG
: The tag for the Docker image build. By default, it uses the GitLab CI commit SHA. -
DOCKER_IMAGE_BUILD_TARGET_PLATFORMS
: A comma-separated list of available platforms for the Docker image build. Supported platforms include:linux/amd64
,linux/amd64/v2
,linux/amd64/v3
,linux/arm64
,linux/riscv64
,linux/ppc64
,linux/ppc64le
,linux/s390x
,linux/386
,linux/mips64le
,linux/mips64
,linux/arm/v7
,linux/arm/v6
. -
CI_REGISTRY_USER
: The username for logging in to the GitLab CI registry. -
CI_REGISTRY_PASSWORD
: The password for logging in to the GitLab CI registry. -
NFC_DOCKERHUB_USERNAME
: The username for logging in to Docker Hub. -
NFC_DOCKERHUB_TOKEN
: The access token or password for logging in to Docker Hub. -
DOCKER_IMAGE_PUBLISH_REGISTRY
: The registry for publishing the Docker image to Docker Hub. -
DOCKER_IMAGE_PUBLISH_NAME
: The name of the Docker image for publishing to Docker Hub. -
DOCKER_IMAGE_PUBLISH_URL
: The URL to access the published Docker image on Docker Hub.
Services
docker:23-dind
: Runs Docker in Docker (DinD) service with version 23.
Script
The job executes the following steps in the script
section:
-
Logs in to the GitLab CI registry using the provided credentials (
CI_REGISTRY_USER
andCI_REGISTRY_PASSWORD
). -
If the
DOCKER_IMAGE_BUILD_TARGET_PLATFORMS
variable is specified, it iterates over the platforms and inspects the Docker image usingdocker buildx imagetools inspect
. -
If the
DOCKER_IMAGE_BUILD_TARGET_PLATFORMS
variable is not specified, it pulls the Docker image from the specified registry. -
Logs in to Docker Hub using the provided credentials (
NFC_DOCKERHUB_USERNAME
andNFC_DOCKERHUB_TOKEN
). -
Lists the Docker images using
docker image ls
. -
Determines the appropriate tag for the Docker image based on the pipeline source and commit tag.
-
If the
DOCKER_IMAGE_BUILD_TARGET_PLATFORMS
variable is specified, it creates multi-arch images usingdocker buildx imagetools create
and tags them with the appropriate tag. -
If the
DOCKER_IMAGE_BUILD_TARGET_PLATFORMS
variable is not specified, it tags the Docker image with the appropriate tag based on the pipeline source and commit tag. -
Pushes the Docker image to Docker Hub.
-
Logs out of Docker Hub.
Environment
The job sets the following environment variables:
-
Name: DockerHub
-
URL: The URL to access the published Docker image on Docker Hub.
Rules
The job is controlled by the following rules:
-
Runs when the pipeline is triggered by a Git tag and there is no associated branch.
-
Runs when the pipeline is triggered by a push to the
master
branch and there is a Dockerfile present. -
Runs when the pipeline is triggered by a push to the
development
branch, there are changes in the Dockerfile or theincludes/
directory compared to themaster
branch, and it allows failure. -
Never runs explicitly.
gitlab-ci.yml definition
.gitlab-ci.yml | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
About:
This page forms part of our Project Gitlab-CI.
Page Metadata
Version: ToDo: place files short git commit hereDate Created: 2023-06-10
Date Edited: 2023-06-10
Contribution:
Would You like to contribute to our Gitlab-CI project? You can assist in the following ways:
- Edit This Page If there is a mistake or a way you can improve it.
- Add a Page to the Manual if you would like to add an item to our manual
- Raise an Issue if there is something about this page you would like to improve, and git is unfamiliar to you.
ToDo: Add the page list of contributors