Skip to content

Commit

Permalink
Merge pull request #13 from exdial/infra/enable-image-tags
Browse files Browse the repository at this point in the history
Added tag management
  • Loading branch information
exdial committed Aug 22, 2023
2 parents d747324 + 9bfbbeb commit 81a178e
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 13 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*
!.dockerignore
!Dockerfile
.versions
18 changes: 7 additions & 11 deletions .github/workflows/build-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ jobs:
- Dockerfile
- .dockerignore
- .github/workflows/build-image.yaml
- .versions
build_image:
needs: check_changes
#if: needs.check_changes.outputs.dockerfile == 'true'
if: needs.check_changes.outputs.dockerfile == 'true'
runs-on: ubuntu-latest

steps:
Expand All @@ -49,20 +50,13 @@ jobs:
echo "terragrunt_version=$terragrunt_version" >> $GITHUB_ENV
image_create_date=$(date '+%Y-%m-%dT%H:%M:%S')
echo "image_create_date=$image_create_date" >> $GITHUB_ENV
echo "image_version=${GITHUB_SHA::7}" >> $GITHUB_ENV
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: exdial/infra-tools
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
Expand All @@ -77,12 +71,14 @@ jobs:
uses: docker/build-push-action@v4
with:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
tags: |
exdial/infra-tools:latest
exdial/infra-tools:${{ env.image_version }}
build-args: |
ALPINE_VERSION=${{ env.alpine_version }}
PACKER_VERSION=${{ env.packer_version }}
TERRAFORM_VERSION=${{ env.terraform_version }}
TERRAGRUNT_VERSION=${{ env.terragrunt_version }}
IMAGE_CREATE_DATE=${{ env.image_create_date }}
IMAGE_VERSION=latest
IMAGE_VERSION=${{ env.image_version }}
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ ARG IMAGE_VERSION

# Metadata as defined in OCI image spec annotations
# https://github.com/opencontainers/image-spec/blob/main/annotations.md
LABEL org.opencontainers.image.title="infra tools" \
org.opencontainers.image.description="infra tools" \
LABEL org.opencontainers.image.title="infra-tools" \
org.opencontainers.image.description="infra-tools" \
org.opencontainers.image.authors="github.com/exdial" \
org.opencontainers.image.created=$IMAGE_CREATE_DATE \
org.opencontainers.image.version=$IMAGE_VERSION
Expand Down
98 changes: 98 additions & 0 deletions utils/release_tag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env bash
#
# +------------------+----------------------+----------------------+
# | x | no tags in repo | repo has tags |
# |------------------|----------------------|----------------------|
# | run without args | generate_first_tag() | increment_last_tag() |
# | run with args | create_new_tag() | create_new_tag() |
# +------------------+----------------------+----------------------+
#
# There are 4 use cases:
# 1) If the program is executed without any arguments and
# no tags have been created yet, the program will initially
# set the first tag as "v0.0.1".
# 2) If the program is run without any arguments and there are
# existing tags in the repository, it will automatically
# increase the third part of the version (patch).
# 3) If you run the program with arguments and no tags have been
# created yet, the programm will check if the argument is in the
# correct semantic versioning format. If the argument is valid,
# the program will add a new tag that you specified using those
# arguments.
# 4) When running the program with arguments and there are already
# tags in the repository, it will verify if the argument follows
# the correct semantic versioning format. If the argument is deemed
# valid, the program will proceed to add a new tag as per
# the arguments you specified.

set -eo pipefail

TAG_PREFIX="v"
#TAG="$1"

usage() {
echo
echo "Usage: $0 ${TAG_PREFIX}<tag>"
echo "Example: $0 ${TAG_PREFIX}0.1.2"
}

are_there_any_tags() {
num_of_tags() {
git tag -l | wc -l | awk '{print $1}'
}
if [ "$(num_of_tags)" -eq "0" ]; then
echo "There are no tags yet."
false
else
echo "Found $(num_of_tags) tags."
true
fi
}

increment_last_tag() {
echo "Incrementing last tag..."
get_last_tag() {
git tag -l --sort=version:refname "$TAG_PREFIX"* | \
tail -n 1 | tr -d 'v'
}
MAJOR=$(get_last_tag | cut -d '.' -f1)
MINOR=$(get_last_tag | cut -d '.' -f2)
PATCH=$(get_last_tag | cut -d '.' -f3)
echo "Current tag: ${TAG_PREFIX}${MAJOR}.${MINOR}.${PATCH}."
let "PATCH++"
NEW_TAG="${TAG_PREFIX}${MAJOR}.${MINOR}.${PATCH}"
echo "New tag: ${NEW_TAG}."
git tag "$NEW_TAG"
}

generate_first_tag() {
echo "Generating first tag..."
echo New tag: "${TAG_PREFIX}0.0.1"
git tag "${TAG_PREFIX}0.0.1"
}

# Check number of arguments
case "$#" in
# No tags passed
0)
are_there_any_tags && increment_last_tag || generate_first_tag
;;
# Tag is defined via argument
1)
# Validate the tag has semver format
if [[ "$1" =~ ^${TAG_PREFIX}[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Creating new tag..."
echo "New tag: ${1}"
git tag "$1"
else
echo "Invalid tag format!"
usage
exit 1
fi
;;
# Fail if more than 1 argument passed
*)
echo "Wrong number of arguments!"
usage
exit 1
esac

0 comments on commit 81a178e

Please sign in to comment.