Skip to content

Build and Push Docker Image #7

Build and Push Docker Image

Build and Push Docker Image #7

name: Build and Push Docker Image
on:
push:
tags:
- '*.*.*'
workflow_dispatch:
inputs:
version:
description: 'The version tag to build (e.g., v1.2.3)'
required: true
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
with:
# On workflow_dispatch, checkout the tag passed in. On a tag push, checkout the tag from the event.
ref: ${{ github.event.inputs.version || github.ref }}
- name: Log in to GitHub Container Registry
uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set version
id: version
run: echo "VERSION=${{ github.event.inputs.version || github.ref_name }}" >> $GITHUB_ENV
- name: Convert repository name to lowercase
id: repo
run: echo "REPO_NAME=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
- name: Build Docker image
run: |
docker build -t ghcr.io/${{ env.REPO_NAME }}:${{ env.VERSION }} .
docker tag ghcr.io/${{ env.REPO_NAME }}:${{ env.VERSION }} ghcr.io/${{ env.REPO_NAME }}:latest
- name: Push Docker image
run: |
docker push ghcr.io/${{ env.REPO_NAME }}:${{ env.VERSION }}
docker push ghcr.io/${{ env.REPO_NAME }}:latest