Create and publish Docker image #304
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
# This workflow handles creating the Docker images and publishing them | |
# on the GitHub Container registry | |
name: Create and publish Docker image | |
on: | |
# Trigger workflow when a new release has been published | |
workflow_run: | |
workflows: [Publish release] | |
types: [completed] | |
branches: [minor-release, major-release, patch] | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
jobs: | |
# Get the version number of the latest release in order to tag the Docker images: | |
# https://github.com/pozetroninc/github-action-get-latest-release | |
get-latest-version: | |
runs-on: ubuntu-latest | |
if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
outputs: | |
latest-version: ${{ steps.get-latest-version-step.outputs.release }} | |
steps: | |
- id: get-latest-version-step | |
name: Get latest version of repository | |
uses: pozetroninc/github-action-get-latest-release@master | |
with: | |
repository: ${{ github.repository }} | |
token: ${{ secrets.GITHUB_TOKEN }} | |
# Build the images and push them to the GitHub Container registry: | |
# https://docs.github.com/en/actions/publishing-packages/publishing-docker-images | |
# Since a RicePilaf release involves building multiple images (for local installation, | |
# for deployment, and for the data preparation workflow), this job uses the matrix strategy | |
# to build and push the images in parallel: | |
# https://docs.github.com/en/actions/using-jobs/using-a-matrix-for-your-jobs | |
build-and-push-image: | |
runs-on: ubuntu-latest | |
needs: get-latest-version | |
strategy: | |
fail-fast: false | |
matrix: # Cannot use env variables inside matrix | |
include: | |
- dockerfile: Dockerfile-app | |
image: ghcr.io/bioinfodlsu/rice-pilaf/app | |
- dockerfile: Dockerfile-workflow | |
image: ghcr.io/bioinfodlsu/rice-pilaf/workflow | |
- dockerfile: Dockerfile-deploy | |
image: ghcr.io/bioinfodlsu/rice-pilaf/deploy | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@v4 | |
with: | |
images: ${{ matrix.image }} | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v2 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Log in to the Container registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v3 | |
with: | |
context: . | |
cache-from: type=gha | |
cache-to: type=gha, mode=max | |
file: ${{ matrix.dockerfile }} | |
push: true | |
tags: ${{ matrix.image }}:${{ needs.get-latest-version.outputs.latest-version }}, ${{ matrix.image }}:latest | |
labels: ${{ steps.meta.outputs.labels }} | |
# Handle case where triggering workflow failed | |
on-failure: | |
runs-on: ubuntu-latest | |
if: ${{ github.event.workflow_run.conclusion == 'failure' }} | |
steps: | |
- run: echo 'The triggering workflow failed' |