Publish Docker Image #1073
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: "Publish Docker Image" | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
# This workflow uses actions that are not certified by GitHub. | |
# They are provided by a third-party and are governed by | |
# separate terms of service, privacy policy, and support | |
# documentation. | |
on: | |
# run every day at midnight | |
schedule: | |
- cron: '0 0 * * *' | |
# Run on every push | |
push: | |
branches: main | |
# Allows you to run this workflow manually from the Actions tab | |
workflow_dispatch: | |
env: | |
# Use docker.io for Docker Hub if empty | |
REGISTRY: ghcr.io | |
# use DOCKERHUB_USERNAME as the name maybe different from the github username | |
IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/automatic-ripping-machine | |
TAG: latest | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
permissions: write-all | |
steps: | |
- | |
name: Checkout repository | |
uses: actions/checkout@v4 | |
- | |
name: Get branch name | |
id: branch-name | |
uses: tj-actions/branch-names@v8 | |
- | |
name: Set tag for non-default branch | |
if: steps.branch-name.outputs.is_default == 'false' && steps.branch-name.outputs.default_branch != '' | |
run: | | |
echo "Branch name is ${{ steps.branch-name.outputs.ref_branch }}" | |
echo "Main name is ${{ steps.branch-name.outputs.default_branch }}" | |
echo "TAG=${{ steps.branch-name.outputs.ref_branch }}" >> $GITHUB_ENV | |
# Workaround: https://github.com/docker/build-push-action/issues/461 | |
- | |
name: Setup Docker buildx | |
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 | |
# Login against all registries | |
# https://github.com/docker/login-action | |
- | |
name: Log into registry ${{ env.REGISTRY }} | |
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- | |
name: Login to DockerHub | |
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
# Extract metadata (tags, labels) for Docker | |
# https://github.com/docker/metadata-action | |
- | |
name: Extract Docker metadata | |
id: meta | |
uses: docker/metadata-action@dbef88086f6cef02e264edb7dbf63250c17cef6c | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
- | |
name: Set Version number | |
run: | | |
if [ ${{ steps.branch-name.outputs.is_default }} = true ]; then | |
VER=$(cat VERSION) | |
echo "VERSION=$VER" >> $GITHUB_ENV | |
else | |
echo "VERSION=${{ env.TAG }}" >> $GITHUB_ENV | |
echo "${{ env.TAG }}" > ./VERSION | |
fi | |
- | |
name: Set build datetime | |
run: | | |
TIMESTAMP=$(date -u +'%Y-%m-%d T%H:%M:%SZ') | |
echo "BUILD_DATE=$TIMESTAMP" >> $GITHUB_ENV | |
# Build and push Docker image with Buildx | |
# https://github.com/docker/build-push-action | |
- | |
name: Build and push Docker image | |
id: build-and-push | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
push: ${{ github.event_name != 'pull_request' }} | |
platforms: linux/amd64,linux/arm64,linux/arm/v7 | |
tags: | | |
${{ env.IMAGE_NAME }}:${{ env.TAG }} | |
${{ env.IMAGE_NAME }}:${{ env.VERSION }} | |
labels: ${{ steps.meta.outputs.labels }} | |
# Fetches all tags for the repo | |
- | |
name: Fetch tags | |
run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* | |
- | |
name: 'Check: version has corrosponding git tag' | |
id: tagged | |
shell: bash | |
run: git show-ref --tags --verify --quiet -- "refs/tags/${{ env.VERSION }}" && echo "::set-output name=tagged::0" || echo "::set-output name=tagged::1" | |
- | |
name: Create Release | |
if: steps.tagged.outputs.tagged == 1 | |
uses: "lauravuo/action-automatic-releases@test-changes" | |
with: | |
repo_token: "${{ secrets.GITHUB_TOKEN }}" | |
automatic_release_tag: "${{ env.VERSION }}" | |
prerelease: false | |
auto_generate_release_notes: true | |
title: "${{ env.VERSION }}" | |
files: | | |
LICENSE | |
VERSION |