Skip to content

Build and Push Docker Image #6

Build and Push Docker Image

Build and Push Docker Image #6

name: Build and Push Docker Image
on:
push:
tags:
- '*'
workflow_dispatch:
inputs:
tag:
description: 'Tag version (e.g. 1.2.3)'
required: true
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract version info
id: version
run: |
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
TAG_NAME="${{ github.event.inputs.tag }}"
else
RAW_TAG="${GITHUB_REF#refs/tags/}"
TAG_NAME="${RAW_TAG#v}"
fi
MAJOR_MINOR=$(echo "$TAG_NAME" | cut -d. -f1,2)
echo "Using Docker tag: $TAG_NAME"
echo "TAG=$TAG_NAME" >> $GITHUB_ENV
echo "MAJOR_MINOR=$MAJOR_MINOR" >> $GITHUB_ENV
- name: Normalize repository name (lowercase)
run: |
echo "REPO_LOWER=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
file: .docker/Dockerfile
push: true
tags: |
ghcr.io/${{ env.REPO_LOWER }}:${{ env.TAG }}
ghcr.io/${{ env.REPO_LOWER }}:${{ env.MAJOR_MINOR }}