Skip to content

Build and publish docker on Tag and Release #16

Build and publish docker on Tag and Release

Build and publish docker on Tag and Release #16

# When a new release is published,
# upload image to Dockerhub.
#
# Requires the following repository secrets:
# - DOCKER_IMAGE - Configured as a secret so it can be configured per fork.
# - DOCKER_HUB_USERNAME
# - DOCKER_HUB_ACCESS_TOKEN
# - GITHUBPAT - The github account to use for downloading CRAN dependencies.
# Needed to avoid "API rate limit exceeded" from github.
name: Build and publish docker on Tag and Release
on:
push:
tags:
- "*"
workflow_dispatch:
inputs:
docker_image_name:
description: 'The name of the image to be deployed.'
default: 'broadsea-hades'
type: string
tag:
description: 'The tag of image to be created.'
required: true
type: string
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-release:
name: build and create release
runs-on: ubuntu-latest
outputs:
upload_url: ${{ steps.create_release.outputs.upload_url }}
steps:
- name: Checkout
uses: actions/Checkout@v4
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
body: |
Update docker image version ${{ github.ref }}
draft: true
prerelease: false
- name: zip and upload release file
run: |
zip -r release.zip . -x ".git/*" ".github/*"
- name: upload released asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url}}
asset_path: ./release.zip
asset_name: ${{ github.ref }}
asset_content_type: application/zip
build-docker-image:
name: build docker image and push to ghcr and release
runs-on: ubuntu-latest
needs: [build-and-release]
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Checkout
uses: actions/Checkout@v4
- name: Log in to ghcr
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN}}
- name: Prepare docker metadata
id: metadata
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tag: |
type=semver,pattern={{version}}
type=raw,value=${{ inputs.tag }}
type=sha
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Build and push Dockerhub
id: push
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
# Allow running the image on the architectures supported by nginx-unprivileged:alpine.
platforms: linux/amd64,linux/arm64
push: true
secrets: |
build_github_pat=${{ secrets.GITHUBPAT }}
tags: ${{ steps.metadata.outputs.tags }}
labels: |
${{ steps.metadata.outputs.labels }}
org.opencontainers.image.licenses=Apache-2.0
- name: Inspect image
run: |
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.metadata.outputs.tags }}
docker image inspect ${{ env.DOCKER_IMAGE }}:${{ steps.metadata.outputs.tags }}