Files
macos_security/.github/workflows/container-publish.yml
2026-09-10 13:33:27 -04:00

234 lines
9.2 KiB
YAML

name: Build and Publish Container
on:
workflow_run:
workflows: ["CLI Smoke Tests", "Generate Baselines and Increment Build"]
types: [completed]
branches: [main, dev_27]
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}-${{ github.event.workflow_run.head_branch || github.ref_name }}
cancel-in-progress: false
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository_owner }}/mscp_2.0
jobs:
# ---------------------------------------------------------------------------
# Gate the run once, and resolve the branch / revision / tag inputs that the
# per-arch build and the merge job both need.
# ---------------------------------------------------------------------------
prepare:
runs-on: ubuntu-latest
# On the primary repo: trigger after Generate Baselines completes on a real (non-bump) commit.
# On forks: trigger after CLI Smoke Tests completes (Generate Baselines is a no-op there).
if: >
github.event_name == 'workflow_dispatch' ||
(github.event.workflow_run.conclusion == 'success' && (
(github.event.workflow_run.name == 'Generate Baselines and Increment Build' &&
github.repository == 'usnistgov/macos_security' &&
!contains(github.event.workflow_run.head_commit.message, '[skip ci]')) ||
(github.event.workflow_run.name == 'CLI Smoke Tests' &&
github.repository != 'usnistgov/macos_security')
))
permissions:
contents: read
actions: read # required to query workflow run status
outputs:
branch: ${{ steps.branch.outputs.branch }}
is_main: ${{ steps.branch.outputs.is_main }}
image_tag: ${{ steps.branch.outputs.image_tag }}
revision: ${{ steps.branch.outputs.revision }}
revision_short: ${{ steps.branch.outputs.revision_short }}
steps:
- name: Verify lint passed
if: github.event_name != 'workflow_dispatch' && github.event.workflow_run.name == 'CLI Smoke Tests'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SHA: ${{ github.event.workflow_run.head_sha }}
run: |
repo="${{ github.repository }}"
wf_id=$(gh api "repos/$repo/actions/workflows" --jq '.workflows[] | select(.name == "Lint Code Base") | .id')
result=$(gh api "repos/$repo/actions/workflows/$wf_id/runs?head_sha=$SHA&per_page=1" --jq '.workflow_runs[0].conclusion // empty')
if [[ "$result" != "success" ]]; then
echo "Lint Code Base did not pass (conclusion: ${result:-not found})"
exit 1
fi
- name: Determine source branch and revision
id: branch
env:
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
run: |
branch="${HEAD_BRANCH:-${{ github.ref_name }}}"
revision="${HEAD_SHA:-${{ github.sha }}}"
{
echo "branch=${branch}"
echo "revision=${revision}"
echo "revision_short=${revision:0:7}"
} >> "$GITHUB_OUTPUT"
if [[ "${branch}" == "main" ]]; then
echo "is_main=true" >> "$GITHUB_OUTPUT"
echo "image_tag=latest" >> "$GITHUB_OUTPUT"
else
echo "is_main=false" >> "$GITHUB_OUTPUT"
echo "image_tag=${branch}" >> "$GITHUB_OUTPUT"
fi
# ---------------------------------------------------------------------------
# Build each architecture natively and in parallel, push each by digest only.
# ---------------------------------------------------------------------------
build:
needs: prepare
strategy:
fail-fast: false
matrix:
include:
- platform: linux/amd64
runner: ubuntu-latest
arch: amd64
- platform: linux/arm64
runner: ubuntu-24.04-arm # free for public repos; native arm64 (no QEMU)
arch: arm64
runs-on: ${{ matrix.runner }}
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7
with:
ref: ${{ needs.prepare.outputs.revision }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@d7f5e7f509e45cec5c76c4d5afdd7de93d0b3df5 # v4.1.0
- name: Log in to GitHub Container Registry
uses: docker/login-action@650006c6eb7dba73a995cc03b0b2d7f5ca915bee # v4.2.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push by digest
id: build
uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7.3.0
with:
context: .
file: .github/container/Dockerfile
platforms: ${{ matrix.platform }}
labels: |
org.opencontainers.image.revision=${{ needs.prepare.outputs.revision }}
build-args: |
BUILD_SHA=${{ needs.prepare.outputs.revision }}
IMAGE_REPO=${{ github.repository }}
IMAGE_TAG=${{ needs.prepare.outputs.image_tag }}
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
# Separate cache scope per arch so the two builds do not evict each other.
cache-from: type=gha,scope=buildkit-${{ matrix.arch }}
cache-to: type=gha,mode=max,scope=buildkit-${{ matrix.arch }}
# Keep each arch a plain manifest so the merge step can assemble the index.
provenance: false
- name: Export digest
run: |
mkdir -p "${{ runner.temp }}/digests"
digest="${{ steps.build.outputs.digest }}"
touch "${{ runner.temp }}/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: digests-${{ matrix.arch }}
path: ${{ runner.temp }}/digests/*
if-no-files-found: error
retention-days: 1
# ---------------------------------------------------------------------------
# Assemble the multi-arch image index, tag it, then sign and attest it.
# ---------------------------------------------------------------------------
merge:
needs: [prepare, build]
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write # required for cosign keyless signing via OIDC
attestations: write
steps:
- name: Download digests
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
path: ${{ runner.temp }}/digests
pattern: digests-*
merge-multiple: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@d7f5e7f509e45cec5c76c4d5afdd7de93d0b3df5 # v4.1.0
- name: Log in to GitHub Container Registry
uses: docker/login-action@650006c6eb7dba73a995cc03b0b2d7f5ca915bee # v4.2.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@80c7e94dd9b9319bd5eb7a0e0fe9291e23a2a2e9 # v6.1.0
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=main,enable=${{ needs.prepare.outputs.is_main == 'true' }}
type=raw,value=latest,enable=${{ needs.prepare.outputs.is_main == 'true' }}
type=raw,value=${{ needs.prepare.outputs.branch }},enable=${{ needs.prepare.outputs.is_main != 'true' }}
type=raw,value=sha-${{ needs.prepare.outputs.revision_short }}
- name: Create multi-arch manifest list and push
working-directory: ${{ runner.temp }}/digests
run: |
docker buildx imagetools create \
$(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *)
- name: Capture pushed image digest
id: digest
run: |
digest=$(docker buildx imagetools inspect \
"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}" \
--format '{{json .Manifest.Digest}}' | tr -d '"')
echo "digest=${digest}" >> "$GITHUB_OUTPUT"
- name: Install cosign
uses: sigstore/cosign-installer@6f9f17788090df1f26f669e9d70d6ae9567deba6 # v4.1.2
- name: Sign image with cosign (keyless via GitHub OIDC)
env:
DIGEST: ${{ steps.digest.outputs.digest }}
TAGS: ${{ steps.meta.outputs.tags }}
COSIGN_REGISTRY_USERNAME: ${{ github.actor }}
COSIGN_REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
run: |
images=""
for tag in ${TAGS}; do
images+="${tag}@${DIGEST} "
done
cosign sign --yes ${images}
- name: Generate build provenance attestation
uses: actions/attest@a1948c3f048ba23858d222213b7c278aabede763 # v4.1.1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
subject-digest: ${{ steps.digest.outputs.digest }}
push-to-registry: true