Skip to content

Build and Publish Docker Images #44

Build and Publish Docker Images

Build and Publish Docker Images #44

name: Build and Publish Docker Images
on:
workflow_dispatch:
inputs:
build_all:
description: 'Build all steps (includes main build unless skipped)'
type: boolean
default: false
build_base:
description: 'Build base image'
type: boolean
default: false
build_build:
description: 'Build build image'
type: boolean
default: false
build_sm:
description: 'Build SM image'
type: boolean
default: false
skip_release_main_build:
description: 'Skip release of a main build'
type: boolean
default: false
release_as_latest:
description: 'Release as latest'
type: boolean
default: false
permissions:
contents: read
packages: write
env:
REGISTRY: ghcr.io
BASE_IMAGE_NAME: ${{ github.repository_owner }}/streammaster-builds
FINAL_IMAGE_NAME: ${{ github.repository_owner }}/streammaster
jobs:
semantic-release:
runs-on: ubuntu-latest
permissions:
contents: write
outputs:
new_release_published: ${{ steps.semantic.outputs.new_release_published }}
new_release_version: ${{ steps.semantic.outputs.new_release_version }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: |
set -e
npm ci
- name: Copy release config
run: |
set -e
if [[ "${{ inputs.release_as_latest }}" == "true" ]]; then
cp release.config.release.cjs release.config.cjs
else
cp release.config.norelease.cjs release.config.cjs
fi
- name: Semantic Release
id: semantic
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -e
# Delete the local tag if it exists
git tag -l "v*" | xargs -r git tag -d
# Fetch all tags from remote
git fetch --tags
# Run semantic-release with --dry-run first to check if a release would be created
if npx semantic-release --dry-run; then
npx semantic-release
else
# If no new release would be created, set the outputs manually
echo "new_release_published=false" >> $GITHUB_OUTPUT
# Get the latest tag as the version
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ ! -z "$LATEST_TAG" ]; then
echo "new_release_version=${LATEST_TAG#v}" >> $GITHUB_OUTPUT
fi
fi
setup:
needs: semantic-release
runs-on: ubuntu-latest
outputs:
version: ${{ steps.gitversion.outputs.semVer }}
branchName: ${{ steps.gitversion.outputs.branchName }}
buildMeta: ${{ steps.gitversion.outputs.buildMetadata }}
build_base: ${{ inputs.build_base || inputs.build_all }}
build_build: ${{ inputs.build_build || inputs.build_all }}
build_sm: ${{ inputs.build_sm || inputs.build_all }}
skip_release_main_build: ${{ inputs.skip_release_main_build }}
release_as_latest: ${{ inputs.release_as_latest }}
semantic_version: ${{ needs.semantic-release.outputs.new_release_version }}
is_release: ${{ needs.semantic-release.outputs.new_release_published }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install GitVersion
uses: gittools/actions/gitversion/setup@v0
with:
versionSpec: "5.x"
- name: Determine Version
id: gitversion
uses: gittools/actions/gitversion/execute@v0
test:
needs: [setup]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Run Tests
run: |
docker buildx build \
--platform linux/amd64 \
-f Dockerfile.tests \
--progress=plain \
--no-cache \
.
build:
needs: [setup, semantic-release, test]
runs-on: ubuntu-latest
env:
VERSION: ${{ needs.setup.outputs.semantic_version || needs.setup.outputs.version }}
BRANCH_NAME: ${{ needs.setup.outputs.branchName }}
BUILD_BASE: ${{ needs.setup.outputs.build_base }}
BUILD_BUILD: ${{ needs.setup.outputs.build_build }}
BUILD_SM: ${{ needs.setup.outputs.build_sm }}
SKIP_RELEASE_MAIN_BUILD: ${{ needs.setup.outputs.skip_release_main_build }}
RELEASE_AS_LATEST: ${{ needs.setup.outputs.release_as_latest || needs.setup.outputs.is_release }}
steps:
- uses: actions/checkout@v4
- name: Download AssemblyInfo.cs
if: needs.semantic-release.outputs.new_release_published == 'true'
uses: actions/download-artifact@v4
with:
name: assembly-info
path: StreamMaster.API/
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GHCR
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Base - Build and Push
if: ${{ env.BUILD_BASE == 'true' }}
run: |
docker buildx build --platform linux/amd64,linux/arm64 -t ${{ env.REGISTRY }}/${{ env.BASE_IMAGE_NAME }}:${{ env.VERSION }}-base -f Dockerfile.base --push .
- name: Build - Build and Push
if: ${{ env.BUILD_BUILD == 'true' }}
run: |
docker buildx build --platform linux/amd64,linux/arm64 -t ${{ env.REGISTRY }}/${{ env.BASE_IMAGE_NAME }}:${{ env.VERSION }}-build -f Dockerfile.build --push .
- name: SM - Build and Push
if: ${{ env.BUILD_SM == 'true' }}
run: |
set -e
echo "FROM --platform=\$BUILDPLATFORM ${{ env.REGISTRY }}/${{ env.BASE_IMAGE_NAME }}:${{ env.VERSION }}-build AS build" > Dockerfile.sm
cat Dockerfile.sm.template >> Dockerfile.sm
docker buildx build --platform linux/amd64,linux/arm64 -t ${{ env.REGISTRY }}/${{ env.BASE_IMAGE_NAME }}:${{ env.VERSION }}-sm -f Dockerfile.sm --push .
- name: Final - Build and Push
if: ${{ env.SKIP_RELEASE_MAIN_BUILD != 'true' }}
run: |
set -e
echo "FROM ${{ env.REGISTRY }}/${{ env.BASE_IMAGE_NAME }}:${{ env.VERSION }}-sm AS sm" > Dockerfile
echo "FROM ${{ env.REGISTRY }}/${{ env.BASE_IMAGE_NAME }}:${{ env.VERSION }}-base AS base" >> Dockerfile
cat Dockerfile.template >> Dockerfile
docker buildx build --platform linux/amd64,linux/arm64 -t ${{ env.REGISTRY }}/${{ env.FINAL_IMAGE_NAME }}:${{ env.VERSION }} -t ${{ env.REGISTRY }}/${{ env.FINAL_IMAGE_NAME }}:${{ env.RELEASE_AS_LATEST == 'true' && 'latest' || env.BRANCH_NAME }} -f Dockerfile --push .