Skip to content

Create GitHub Release & Docker Build and Push #4

Create GitHub Release & Docker Build and Push

Create GitHub Release & Docker Build and Push #4

name: Create GitHub Release & Docker Build and Push
on:
workflow_dispatch:
jobs:
release-and-build:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
permissions:
contents: write # Needed to create tags and releases
packages: write # Needed to push to GHCR
id-token: write # For trusted publishing
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
persist-credentials: false
- name: Generate tag name from current date
id: date
run: echo "tag=$(date +'%Y.%m.%d')" >> $GITHUB_OUTPUT
- name: Check if tag already exists
id: check_tag
run: |
if git rev-parse "${{ steps.date.outputs.tag }}" >/dev/null 2>&1; then
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
fi
- name: Create git tag and push using token
if: steps.check_tag.outputs.exists == 'false'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag ${{ steps.date.outputs.tag }}
git push https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.repository }} ${{ steps.date.outputs.tag }}
- name: Create GitHub Release
if: steps.check_tag.outputs.exists == 'false'
uses: ncipollo/release-action@v1
with:
tag: ${{ steps.date.outputs.tag }}
name: Release ${{ steps.date.outputs.tag }}
generateReleaseNotes: true
draft: false
prerelease: false
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- 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 metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=raw,value=${{ steps.date.outputs.tag }},priority=1000
type=raw,value=latest
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max