Publish / Mastodon container images #85
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 / Mastodon container images | |
on: | |
# Run every Monday at 12:00 EDT (16:00 UTC) | |
schedule: | |
- cron: "0 16 * * MON" | |
# Allow manual trigger | |
workflow_dispatch: | |
inputs: | |
delete_layer_cache: | |
type: boolean | |
description: Delete the layer cache generated by previous builds. | |
default: false | |
do_not_rebuild_os: | |
type: boolean | |
description: Do not rebuild the OS. | |
default: false | |
do_not_rebuild_mastodon: | |
type: boolean | |
description: Do not rebuild Mastodon. | |
default: false | |
permissions: | |
actions: write | |
packages: write | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: 'Checkout: Latest commit' | |
uses: actions/checkout@v4 | |
with: | |
submodules: true | |
- name: Get current ticks | |
id: get_current_ticks | |
shell: pwsh | |
run: | | |
$currentDateTimeTicks = [System.DateTimeOffset]::Now.UtcTicks | |
"current_datetime_ticks=$($currentDateTimeTicks)" >> $env:GITHUB_ENV | |
# Clear the layer cache if the 'delete_layer_cache' input is true. | |
- name: Clear layer cache | |
if: ${{ inputs.delete_layer_cache == true }} | |
shell: pwsh | |
env: | |
GH_TOKEN: ${{ github.token }} | |
run: | | |
gh extension install actions/gh-actions-cache | |
$cacheList = gh actions-cache list --branch "main" | |
$cacheKeys = ($cacheList | Select-String -Pattern "^(?'cacheKey'.+?)\t.+$").Matches | ForEach-Object { $PSItem.Groups["cacheKey"].Value } | |
$cacheKeys | ForEach-Object { gh actions-cache delete "$($PSItem)" --confirm } | |
# Cache the current seed for building the OS. | |
- name: Cache OS build seed | |
id: cache_os_build_seed | |
uses: actions/cache@v4 | |
with: | |
path: ./.os-build-seed | |
key: ${{ runner.os }}-os-build-seed | |
# Cache the current seed for building Mastodon. | |
- name: Cache Mastodon build seed | |
id: cache_mastodon_build_seed | |
uses: actions/cache@v4 | |
with: | |
path: ./.build-seed | |
key: ${{ runner.os }}-build-seed | |
# Generate a new seed for building the OS if: | |
# - No seed was previously cached | |
# - The workflow is triggered by a schedule | |
# - The workflow is triggered by a manual trigger and the 'do_not_rebuild_os' input is false | |
- name: Generate OS build seed | |
id: generate_os_build_seed | |
if: ${{ steps.cache_os_build_seed.outputs.cache-hit != 'true' || github.event_name == 'schedule' || inputs.do_not_rebuild_os == false }} | |
shell: pwsh | |
run: | | |
$buildSeed = (New-Guid).Guid | |
$buildSeed > ./.os-build-seed | |
# Generate a new seed for building Mastodon if: | |
# - No seed was previously cached | |
# - The workflow is triggered by a schedule | |
# - The workflow is triggered by a manual trigger and the 'do_not_rebuild_os' input is false | |
# - The workflow is triggered by a manual trigger and the 'do_not_rebuild_mastodon' input is false | |
- name: Generate Mastodon build seed | |
id: generate_build_seed | |
if: ${{ steps.cache_mastodon_build_seed.outputs.cache-hit != 'true' || github.event_name == 'schedule' || inputs.do_not_rebuild_os == false || inputs.do_not_rebuild_mastodon == false }} | |
shell: pwsh | |
run: | | |
$buildSeed = (New-Guid).Guid | |
$buildSeed > ./.build-seed | |
# Set the build seed environment variables. | |
- name: Set build seed environment variables | |
shell: pwsh | |
run: | | |
$mastodonBuildSeed = Get-Content -Path "./.build-seed" -Raw | |
"build_seed=$($mastodonBuildSeed)" >> $env:GITHUB_ENV | |
$osBuildSeed = Get-Content -Path "./.os-build-seed" -Raw | |
"os_build_seed=$($osBuildSeed)" >> $env:GITHUB_ENV | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Log into Azure container registry | |
uses: docker/login-action@v3 | |
with: | |
registry: smallsonlinemstdncontainers.azurecr.io | |
username: ${{ secrets.REGISTRY_USERNAME }} | |
password: ${{ secrets.REGISTRY_PASSWORD }} | |
- name: Log into GitHub container registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Set metadata | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: | | |
smallsonlinemstdncontainers.azurecr.io/ocwsocial-glitch-soc | |
ghcr.io/${{ github.repository_owner }}/ocw-social-mastodon | |
tags: | | |
type=raw,value=latest,enable={{is_default_branch}} | |
type=schedule,pattern={{date 'YYYYMMDD-hhmmss' tz='UTC'}} | |
type=ref,event=branch | |
type=sha | |
- name: Build and push container image to registry | |
uses: docker/build-push-action@v5 | |
with: | |
context: ./ | |
file: ./Dockerfile | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
build-args: | | |
OS_BUILD_SEED=${{ env.os_build_seed }} | |
BUILD_SEED=${{ env.build_seed }} | |
platforms: linux/amd64 | |
cache-from: type=gha | |
cache-to: type=gha,mode=max |