Skip to content

Build and Publish Docker Images #433

Build and Publish Docker Images

Build and Publish Docker Images #433

# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#name
name: Build and Publish Docker Images
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#on
on:
push:
branches: [main, develop]
paths-ignore: [README.md]
pull_request:
branches: [main]
workflow_dispatch:
schedule:
# https://crontab.guru/
# Rebuild 2am every Monday, LSIO rebuilds on Friday's
# TODO: Figure out how to build main and develop vs. just the default branch
- cron: 0 2 * * MON
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobs
jobs:
# Set the build matrix
setmatrix:
name: Set Matrix
runs-on: ubuntu-latest
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idoutputs
outputs:
matrix: ${{ steps.setmatrix.outputs.matrix }}
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idsteps
steps:
# https://github.com/marketplace/actions/setup-net-core-sdk
- name: Setup .NET SDK 7
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.x
# https://github.com/marketplace/actions/checkout
- name: Checkout code
uses: actions/checkout@v3
# https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-publish
- name: Build CreateMatrix Tool
run: dotnet publish ./CreateMatrix/CreateMatrix.csproj --self-contained false --output ${{ runner.temp }}/publish
# Execute the compiled version to make sure that the build breaks for a non-0 exit code
- name: Run CreateMatrix Tool
run: ${{ runner.temp }}/publish/CreateMatrix matrix --version=./Make/Version.json --matrix=./Make/Matrix.json --update
# https://github.com/marketplace/actions/git-auto-commit
- name: Commit Version.json and Matrix.json
uses: stefanzweifel/git-auto-commit-action@v4
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
file_pattern: './Make/Version.json ./Make/Matrix.json'
commit_message: Update Version.json and Matrix.json
# Load Matrix.json from file
- name: Load Matrix JSON
id: setmatrix
run: |
# Load JSON from file
JSON=$(cat ./Make/Matrix.json)
echo "JSON:"
echo "$JSON"
# Convert the JSON to a single flat line to avoid having to escape the multiline output
echo "Flat JSON:"
FJSON=$(echo "$JSON" | jq --compact-output)
echo "$FJSON"
echo "matrix=${FJSON}" >> $GITHUB_OUTPUT
# Build and publish docker images
buildpublish:
needs: setmatrix
name: Build and Publish Docker Images
runs-on: ubuntu-latest
strategy:
# https://docs.github.com/en/actions/using-jobs/using-a-matrix-for-your-jobs
matrix:
images: ${{ fromJson(needs.setmatrix.outputs.matrix).images }}
steps:
# https://github.com/marketplace/actions/checkout
- name: Checkout Code
uses: actions/checkout@v3
# https://github.com/marketplace/actions/docker-setup-qemu
- name: Setup QEMU
uses: docker/setup-qemu-action@v2
# https://github.com/marketplace/actions/docker-setup-buildx
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v2
# https://github.com/marketplace/actions/docker-login
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
registry: docker.io
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
# https://github.com/marketplace/actions/build-and-push-docker-images
# https://docs.docker.com/build/ci/github-actions/cache/
# https://github.com/moby/buildkit#github-actions-cache-experimental
- name: Build and Push Images
uses: docker/build-push-action@v4
with:
cache-from: type=gha,scope=${{ matrix.images.cachescope }}
cache-to: type=gha,mode=max,scope=${{ matrix.images.cachescope }}
context: ${{ matrix.images.name }}
file: ${{ matrix.images.name }}/Dockerfile
platforms: linux/amd64
# Push only if not a pull request and branch name matches current branch
push: ${{ (github.event_name != 'pull_request') && (github.ref_name == matrix.images.branch) }}
# Convert tag and args from array to multiline strings
tags: |-
${{ join(matrix.images.tags, '
') }}
build-args: |-
${{ join(matrix.images.args, '
') }}
# Create a custom badge to report the build date
# Run this job at the end of the pipeline
datebadge:
needs: buildpublish
name: Build Date Badge
runs-on: ubuntu-latest
steps:
# Get date from environment as a variable
- id: date
run: |
echo "date=$(date)" >> $GITHUB_OUTPUT
# Create badge
# https://github.com/marketplace/actions/bring-your-own-badge
- name: Build Date Badge
uses: RubbaBoy/BYOB@v1
with:
name: lastbuild
label: Last Build
icon: github
status: ${{ steps.date.outputs.date }}
color: blue
github_token: ${{ secrets.GITHUB_TOKEN }}