From 812f506432f291876cf0f5bdd42a7e6fc5bf26c4 Mon Sep 17 00:00:00 2001 From: Hector Date: Tue, 20 Jun 2023 07:20:11 +0000 Subject: [PATCH] feat: tag docker images with extra labels (!85) Add a new build step to tag docker images with extra labels: - `:latest` - `:` - `:.` This job is only triggered on main release tags. https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/merge_requests/85 --- .gitlab-ci.yml | 20 ++++++++++++++++---- .gitlab-ci/tagLatestImage.sh | 14 ++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) create mode 100755 .gitlab-ci/tagLatestImage.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 098e42a..0c9342f 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -46,13 +46,25 @@ release: GIT_DEPTH: 0 rules: - if: $CI_COMMIT_TAG =~ /^v.*$/ - script: | - # GITLAB_TOKEN is needed to create GitLab releases. - # DOCKER_* are needed to push Docker images. - docker run --rm --privileged \ + script: + - docker run --rm --privileged \ -v $PWD:/go/src/gitlab.com/hectorjsmith/fail2ban-prometheus-exporter \ -w /go/src/gitlab.com/hectorjsmith/fail2ban-prometheus-exporter \ -v /var/run/docker.sock:/var/run/docker.sock \ -e DOCKER_USERNAME -e DOCKER_PASSWORD -e DOCKER_REGISTRY \ -e GITLAB_TOKEN \ goreleaser/goreleaser release --clean + +tag images: + stage: release + image: docker:stable + services: + - docker:dind + needs: + - release + rules: + - if: $CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+$/ + variables: + IMAGE_NAME: registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter + script: + - ./.gitlab-ci/tagLatestImage.sh diff --git a/.gitlab-ci/tagLatestImage.sh b/.gitlab-ci/tagLatestImage.sh new file mode 100755 index 0000000..a2973c7 --- /dev/null +++ b/.gitlab-ci/tagLatestImage.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +VERSION=`echo $CI_COMMIT_TAG | cut -c 2-` +MAJOR=`echo $VERSION | cut -d "." -f 1` +MINOR=`echo $VERSION | cut -d "." -f 2` + +echo "version: $VERSION (major: $MAJOR; minor: $MINOR)" + +docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY +docker pull $IMAGE_NAME:$VERSION +docker tag $IMAGE_NAME:$VERSION $IMAGE_NAME:latest +docker tag $IMAGE_NAME:$VERSION $IMAGE_NAME:$MAJOR +docker tag $IMAGE_NAME:$VERSION $IMAGE_NAME:$MAJOR.$MINOR +docker push $IMAGE_NAME