How to version Docker images with GitLab CI/CD

How to version Docker images with GitLab CI/CD

Variables of GitLab

CI_REGISTRY_USER: User actually of GitLab
CI_REGISTRY_PASSWORD: Password actually of GitLab
CI_REGISTRY: “registry.gitlab.com”
CI_PROJECT_PATH: “gitlab-example/my-project”
CI_COMMIT_SHORT_SHA: Recent commit ID

docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
docker build . -t $CI_REGISTRY/$CI_PROJECT_PATH/$IMAGE_NAME:$CI_COMMIT_SHORT_SHA
IMAGE_ID=$(docker images | grep $CI_REGISTRY/$CI_PROJECT_PATH\/$IMAGE_NAME | awk '{print $3}')
docker tag $IMAGE_ID $CI_REGISTRY/$CI_PROJECT_PATH/$IMAGE_NAME:latest
docker push $CI_REGISTRY/$CI_PROJECT_PATH/$IMAGE_NAME:$CI_COMMIT_SHORT_SHA
docker push $CI_REGISTRY/$CI_PROJECT_PATH/$IMAGE_NAME:latest
build:
image:
docker:19.03.12
stage: build
services:
- docker:19.03.12-dind
rules:
- if: $CI_PIPELINE_SOURCE == "push"
variables:
IMAGE_NAME: "my-app"
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
# BUILD IMAGE WITH COMMIT SHA - docker build . -t $CI_REGISTRY/$CI_PROJECT_PATH/$IMAGE_NAME:$CI_COMMIT_SHORT_SHA # RETAG IMAGE LATEST WITH DIGEST FROM PREVIOUS IMAGE - IMAGE_ID=$(docker images | grep $CI_REGISTRY/$CI_PROJECT_PATH\/$IMAGE_NAME | awk '{print $3}')
- docker tag $IMAGE_ID $CI_REGISTRY/$CI_PROJECT_PATH/$IMAGE_NAME:latest
# PUSH IMAGE COMMIT SHA and LATEST - docker push $CI_REGISTRY/$CI_PROJECT_PATH/$IMAGE_NAME:$CI_COMMIT_SHORT_SHA
- docker push $CI_REGISTRY/$CI_PROJECT_PATH/$IMAGE_NAME:latest
68713c41   Published 1 minute ago
41.58 MiB Digest: 2fd477a
latest Published 1 minute ago
41.58 MiB Digest: 2fd477a

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store