Skip to content

Build versioned image by @John-Magtoto #2

Build versioned image by @John-Magtoto

Build versioned image by @John-Magtoto #2

name: Build versioned image
run-name: Build versioned image by @${{ github.actor }}
on:
workflow_dispatch
defaults:
run:
shell: bash
jobs:
build_image:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
- name: Get ref name
id: github-ref-name
run: |
ref='${{ github.ref_name }}'
echo "Tag ref is ${ref}"
# replace any characters not permitted in a docker tag with an underscore
ref_name="${ref//[^A-Za-z0-9\.\-_]/_}"
echo "Sanitised ref name is ${ref_name}"
echo "ref=${ref_name}" >>"${GITHUB_OUTPUT}"
- name: Get docker image tag
id: docker-image-tag
run: |
sha_short=$(git rev-parse --short HEAD)
timestamp=$(date +'%Y%m%d_%H%M%S')
ref="${{ steps.github-ref-name.outputs.ref }}"
sha_tag="${ref}-${sha_short}"
timestamp_tag="${ref}-${timestamp}"
echo "Docker image tags are ${sha_tag} and ${timestamp_tag}"
echo "sha_tag=${sha_tag}" >>"${GITHUB_OUTPUT}"
echo "timestamp_tag=${timestamp_tag}" >>"${GITHUB_OUTPUT}"
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: Build and push
id: docker_build
uses: docker/build-push-action@v5
with:
context: ./
file: ./Dockerfile
push: true
tags: |
"vocovo/docker-mosquitto:${{ steps.docker-image-tag.outputs.sha_tag }}"
"vocovo/docker-mosquitto:${{ steps.docker-image-tag.outputs.timestamp_tag }}"