From c80fc5bc3268c25b27c4c00784765f374d010a2f Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Thu, 26 Nov 2026 14:05:12 -0800 Subject: [PATCH 1/2] ci: use OIDC only for the private feed and simplify the dev image push OIDC sign-in for the private NuGet feed worked on the first main run after #1729, so remove the AZURE_DEVOPS_PAT fallback from build-and-test and make get-feed-token.sh OIDC-only. Copilot setup steps: drop the private-feed source and PAT. Their restore never sets AccessToNugetFeed (default false), so the private package is never referenced; use the same public NuGet cache as PR builds. Dev image push: read the pushed digest directly with index .RepoDigests 0 and drop the extra ACR re-read of the tag. Every later step deploys by digest, so a tag moving afterwards cannot change what is deployed. Dependabot's private registry still uses the PAT (Dependabot secrets): its OIDC subject is not documented, so it moves in a separate change. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/Build-Test-And-Deploy.yml | 44 ++++----------------- .github/workflows/copilot-setup-steps.yml | 9 ++--- scripts/nuget/get-feed-token.sh | 24 ++--------- 3 files changed, 15 insertions(+), 62 deletions(-) diff --git a/.github/workflows/Build-Test-And-Deploy.yml b/.github/workflows/Build-Test-And-Deploy.yml index a6771b69..8ffce6a7 100644 --- a/.github/workflows/Build-Test-And-Deploy.yml +++ b/.github/workflows/Build-Test-And-Deploy.yml @@ -75,12 +75,7 @@ jobs: environment: "BuildAndUploadImage" permissions: contents: read - id-token: write # OIDC sign-in for the private NuGet feed; unused until AZURE_DEVOPS_FEED_CLIENT_ID is set - env: - # Private-feed auth: OIDC when vars.AZURE_DEVOPS_FEED_CLIENT_ID is set, else AZURE_DEVOPS_PAT. - # Remove the PAT fallback once OIDC has worked on main. - FEED_AUTH_MODE: ${{ vars.AZURE_DEVOPS_FEED_CLIENT_ID != '' && 'oidc' || 'pat' }} - + id-token: write # OIDC sign-in for the private NuGet feed steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 @@ -103,7 +98,6 @@ jobs: ${{ runner.os }}-nuget-feed- - name: Azure login for the private NuGet feed (restore) - if: env.FEED_AUTH_MODE == 'oidc' uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0 with: client-id: ${{ vars.AZURE_DEVOPS_FEED_CLIENT_ID }} @@ -113,15 +107,12 @@ jobs: # Credentials-only user-level NuGet.Config; the repo nuget.config supplies the sources. # It sits outside the Docker build context and is deleted right after restore. - name: Write NuGet feed credentials - env: - AZURE_DEVOPS_PAT: ${{ env.FEED_AUTH_MODE == 'pat' && secrets.AZURE_DEVOPS_PAT || '' }} run: | NUGET_FEED_TOKEN="$(bash scripts/nuget/get-feed-token.sh)" export NUGET_FEED_TOKEN mkdir -p ~/.nuget/NuGet bash scripts/nuget/emit-creds-config.sh > ~/.nuget/NuGet/NuGet.Config chmod 600 ~/.nuget/NuGet/NuGet.Config - echo "Private feed auth mode: ${FEED_AUTH_MODE}" - name: Restore with dotnet run: dotnet restore @@ -185,7 +176,6 @@ jobs: # Sign in again: the GitHub OIDC assertion from the first login may have expired by now. - name: Azure login for the private NuGet feed (container build) - if: env.FEED_AUTH_MODE == 'oidc' uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0 with: client-id: ${{ vars.AZURE_DEVOPS_FEED_CLIENT_ID }} @@ -193,8 +183,6 @@ jobs: allow-no-subscriptions: true - name: Generate Docker NuGet credentials config - env: - AZURE_DEVOPS_PAT: ${{ env.FEED_AUTH_MODE == 'pat' && secrets.AZURE_DEVOPS_PAT || '' }} run: | NUGET_FEED_TOKEN="$(bash scripts/nuget/get-feed-token.sh)" export NUGET_FEED_TOKEN @@ -222,7 +210,7 @@ jobs: run: rm -f "${RUNNER_TEMP}/nuget/NuGet.Config" - name: Sign out of Azure - if: always() && env.FEED_AUTH_MODE == 'oidc' + if: always() run: | az logout || true az account clear || true @@ -421,29 +409,13 @@ jobs: - name: Push Image to Dev Container Registry id: push env: - DEV_ACR: ${{ vars.DEVCONTAINER_REGISTRY }} - IMAGE_TAG: ${{ github.sha }} + IMAGE: ${{ vars.DEVCONTAINER_REGISTRY }}/essentialcsharpweb run: | - REPO="${DEV_ACR}/essentialcsharpweb" - docker push --all-tags "$REPO" - - # Digest of what this job just pushed, from the local image (not re-read through a mutable tag). - DIGEST="$(docker image inspect --format '{{range .RepoDigests}}{{println .}}{{end}}' "${REPO}:${IMAGE_TAG}" \ - | sed -n "s|^${REPO}@||p" | sort -u)" - if ! [[ "$DIGEST" =~ ^sha256:[0-9a-f]{64}$ ]]; then - echo "::error::Could not determine a single pushed digest for ${REPO}:${IMAGE_TAG} (got '${DIGEST}')" - exit 1 - fi - - # The registry's tag must point at the same manifest; anything else means it moved underneath us. - REGISTRY_DIGEST="$(az acr repository show --name "${DEV_ACR%.azurecr.io}" \ - --image "essentialcsharpweb:${IMAGE_TAG}" --query digest --output tsv)" - if [ "$REGISTRY_DIGEST" != "$DIGEST" ]; then - echo "::error::Dev ACR tag ${IMAGE_TAG} points to ${REGISTRY_DIGEST}, but this job pushed ${DIGEST}" - exit 1 - fi - echo "Pushed ${REPO}@${DIGEST}" - echo "digest=${DIGEST}" >> "$GITHUB_OUTPUT" + docker push --all-tags "$IMAGE" + # Digest of what was just pushed; later steps deploy by digest, never by tag. + DIGEST="$(docker image inspect --format '{{index .RepoDigests 0}}' "$IMAGE:$GITHUB_SHA" | cut -d@ -f2)" + [[ "$DIGEST" =~ ^sha256:[0-9a-f]{64}$ ]] || { echo "::error::No pushed digest for $IMAGE:$GITHUB_SHA"; exit 1; } + echo "digest=$DIGEST" >> "$GITHUB_OUTPUT" - name: Run database migrations env: diff --git a/.github/workflows/copilot-setup-steps.yml b/.github/workflows/copilot-setup-steps.yml index 0a4015c0..d83332a1 100644 --- a/.github/workflows/copilot-setup-steps.yml +++ b/.github/workflows/copilot-setup-steps.yml @@ -25,9 +25,6 @@ jobs: uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6.1.0 with: global-json-file: global.json - source-url: https://pkgs.dev.azure.com/intelliTect/_packaging/EssentialCSharp/nuget/v3/index.json - env: - NUGET_AUTH_TOKEN: ${{ secrets.AZURE_DEVOPS_PAT }} - name: Set up Node.js for frontend development uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 @@ -43,10 +40,10 @@ jobs: path: | ~/.nuget/packages ${{ github.workspace }}/**/obj/project.assets.json - # Restored with private-feed credentials: keep it apart from the *-nuget-public- caches PR jobs use. - key: ${{ runner.os }}-nuget-feed-${{ hashFiles('**/packages.lock.json') }} + # Public packages only (AccessToNugetFeed defaults to false), same cache as PR builds. + key: ${{ runner.os }}-nuget-public-${{ hashFiles('**/packages.lock.json') }} restore-keys: | - ${{ runner.os }}-nuget-feed- + ${{ runner.os }}-nuget-public- - name: Set up Docker Buildx uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 diff --git a/scripts/nuget/get-feed-token.sh b/scripts/nuget/get-feed-token.sh index a9e713f4..095d3d88 100644 --- a/scripts/nuget/get-feed-token.sh +++ b/scripts/nuget/get-feed-token.sh @@ -1,30 +1,14 @@ #!/usr/bin/env bash -# Prints a token that can read the private Azure Artifacts feed, masked for GitHub Actions logs. -# -# FEED_AUTH_MODE=oidc: an Entra access token for Azure DevOps from the current `az login` -# (azure/login with OIDC must have run in an earlier step of the same job). -# FEED_AUTH_MODE=pat (default): the legacy personal access token in AZURE_DEVOPS_PAT. +# Prints an Entra access token for Azure DevOps that can read the private Azure Artifacts feed, +# masked for GitHub Actions logs. Requires an earlier azure/login (OIDC) step in the same job. set -euo pipefail # Azure DevOps resource (application) ID; the same for every organization. readonly AZURE_DEVOPS_RESOURCE="499b84ac-1321-427f-aa17-267ca6975798" -mode="${FEED_AUTH_MODE:-pat}" -case "${mode}" in - oidc) - token="$(az account get-access-token --resource "${AZURE_DEVOPS_RESOURCE}" --query accessToken --output tsv)" - ;; - pat) - token="${AZURE_DEVOPS_PAT:-}" - ;; - *) - echo "get-feed-token.sh: unknown FEED_AUTH_MODE '${mode}' (expected oidc or pat)" >&2 - exit 1 - ;; -esac - +token="$(az account get-access-token --resource "${AZURE_DEVOPS_RESOURCE}" --query accessToken --output tsv)" if [[ -z "${token}" ]]; then - echo "get-feed-token.sh: no feed token available for FEED_AUTH_MODE=${mode}" >&2 + echo "get-feed-token.sh: az returned no Azure DevOps token" >&2 exit 1 fi From 384b3fe2daa79e7786e0ed76c4845358f8fa243a Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Thu, 26 Nov 2026 14:20:38 -0800 Subject: [PATCH 2/2] ci: simplify the build and deploy workflow - build-and-test: the host restore, build and tests use placeholder content (AccessToNugetFeed=false), so drop their Azure login and feed credentials steps and share the public NuGet cache with PR builds. Only the container build signs in for the private feed. - Drop the dead pull_request condition on the container build step (the job only runs on push and workflow_dispatch). - deploy-production: drop the cancel-queued-runs script (it cancelled 0 runs in every recent deploy; the deploy-production concurrency group already serializes deploys) and with it the actions: write permission; drop fetch-depth: 0 (tagging HEAD needs no history); drop the post-import tag re-read (later steps deploy by digest). - Remove the Azure logout steps; GitHub-hosted runners are discarded after each job. - pr-build-and-test: one test step with conditional coverage arguments. - frontend-build: check the Node version on one matrix leg only. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/Build-Test-And-Deploy.yml | 153 ++------------------ 1 file changed, 15 insertions(+), 138 deletions(-) diff --git a/.github/workflows/Build-Test-And-Deploy.yml b/.github/workflows/Build-Test-And-Deploy.yml index 8ffce6a7..22bcaca0 100644 --- a/.github/workflows/Build-Test-And-Deploy.yml +++ b/.github/workflows/Build-Test-And-Deploy.yml @@ -32,6 +32,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Verify Node version matches the Docker build + if: matrix.runner == 'ubuntu-latest' shell: bash run: | expected_node_version="$(tr -d '[:space:]' < EssentialCSharp.Web/.nvmrc)" @@ -75,7 +76,7 @@ jobs: environment: "BuildAndUploadImage" permissions: contents: read - id-token: write # OIDC sign-in for the private NuGet feed + id-token: write # OIDC sign-in for the private NuGet feed (container build only) steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 @@ -84,8 +85,8 @@ jobs: with: global-json-file: global.json - # Distinct prefix: this cache holds private-feed packages, so PR jobs (prefix *-nuget-public-) - # must never restore it by prefix fallback. + # The host build and tests use placeholder content (AccessToNugetFeed=false), so they need no + # private-feed credentials and share the public NuGet cache with PR builds. - name: Set up dependency caching for faster builds uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 id: nuget-cache @@ -93,33 +94,12 @@ jobs: path: | ~/.nuget/packages ${{ github.workspace }}/**/obj/project.assets.json - key: ${{ runner.os }}-nuget-feed-${{ hashFiles('**/packages.lock.json') }} + key: ${{ runner.os }}-nuget-public-${{ hashFiles('**/packages.lock.json') }} restore-keys: | - ${{ runner.os }}-nuget-feed- - - - name: Azure login for the private NuGet feed (restore) - uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0 - with: - client-id: ${{ vars.AZURE_DEVOPS_FEED_CLIENT_ID }} - tenant-id: ${{ secrets.AZURE_TENANT_ID }} - allow-no-subscriptions: true - - # Credentials-only user-level NuGet.Config; the repo nuget.config supplies the sources. - # It sits outside the Docker build context and is deleted right after restore. - - name: Write NuGet feed credentials - run: | - NUGET_FEED_TOKEN="$(bash scripts/nuget/get-feed-token.sh)" - export NUGET_FEED_TOKEN - mkdir -p ~/.nuget/NuGet - bash scripts/nuget/emit-creds-config.sh > ~/.nuget/NuGet/NuGet.Config - chmod 600 ~/.nuget/NuGet/NuGet.Config + ${{ runner.os }}-nuget-public- - name: Restore with dotnet - run: dotnet restore - - - name: Remove NuGet feed credentials - if: always() - run: rm -f ~/.nuget/NuGet/NuGet.Config + run: dotnet restore /p:AccessToNugetFeed=false - name: Download frontend build uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 @@ -128,7 +108,7 @@ jobs: path: EssentialCSharp.Web/wwwroot/dist - name: Build with dotnet - run: dotnet build -p:ContinuousIntegrationBuild=True -p:ReleaseDateAttribute=True -p:SkipFrontendBuild=true --configuration Release --no-restore + run: dotnet build -p:ContinuousIntegrationBuild=True -p:ReleaseDateAttribute=True -p:SkipFrontendBuild=true -p:AccessToNugetFeed=false --configuration Release --no-restore - name: Install Playwright Chromium for journey tests run: pwsh dev/EssentialCSharp.Web.AppHost.Tests/bin/Release/net10.0/playwright.ps1 install --with-deps chromium @@ -174,14 +154,14 @@ jobs: cache-dir: buildkit-cache skip-extraction: false - # Sign in again: the GitHub OIDC assertion from the first login may have expired by now. - - name: Azure login for the private NuGet feed (container build) + - name: Azure login for the private NuGet feed uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0 with: client-id: ${{ vars.AZURE_DEVOPS_FEED_CLIENT_ID }} tenant-id: ${{ secrets.AZURE_TENANT_ID }} allow-no-subscriptions: true + # Credentials-only NuGet.Config passed as a BuildKit secret; the repo nuget.config supplies the sources. - name: Generate Docker NuGet credentials config run: | NUGET_FEED_TOKEN="$(bash scripts/nuget/get-feed-token.sh)" @@ -192,7 +172,6 @@ jobs: # Only build for dev registry — prod gets the image via az acr import in deploy-production - name: Build Container Image - if: github.event_name != 'pull_request_target' && github.event_name != 'pull_request' uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 with: tags: ${{ vars.DEVCONTAINER_REGISTRY }}/essentialcsharpweb:${{ github.sha }},${{ vars.DEVCONTAINER_REGISTRY }}/essentialcsharpweb:latest @@ -209,12 +188,6 @@ jobs: if: always() run: rm -f "${RUNNER_TEMP}/nuget/NuGet.Config" - - name: Sign out of Azure - if: always() - run: | - az logout || true - az account clear || true - - name: Upload artifact uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: @@ -252,7 +225,7 @@ jobs: path: | ~/.nuget/packages ${{ github.workspace }}/**/obj/project.assets.json - # No private-feed packages here; *-nuget-public- never matches the credentialed *-nuget-feed- caches. + # Public packages only (AccessToNugetFeed=false); the same cache main's host build uses. key: ${{ runner.os }}-nuget-public-${{ hashFiles('**/packages.lock.json') }} restore-keys: | ${{ runner.os }}-nuget-public- @@ -293,13 +266,8 @@ jobs: core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env['ACTIONS_RUNTIME_TOKEN']); core.exportVariable('ACTIONS_RESULTS_URL', process.env['ACTIONS_RESULTS_URL']); - - name: Run .NET tests with coverage - if: matrix.collect_coverage - run: dotnet test --no-build --configuration Release --report-trx --coverage --results-directory ${{ runner.temp }} - - name: Run .NET tests - if: ${{ !matrix.collect_coverage }} - run: dotnet test --no-build --configuration Release + run: dotnet test --no-build --configuration Release ${{ matrix.collect_coverage && format('--report-trx --coverage --results-directory {0}', runner.temp) || '' }} - name: Upload Playwright failure captures if: ${{ failure() && matrix.collect_coverage }} @@ -488,15 +456,6 @@ jobs: EssentialCSharp.Web.E2ETests/bin/Release/net10.0/playwright-artifacts if-no-files-found: ignore - - name: Logout of Azure CLI - if: always() - uses: azure/CLI@9eb25b8360668fb0ecbafa808d40e2197b2f5f52 # v3.0.0 - with: - inlineScript: | - az logout - az cache purge - az account clear - deploy-production: if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' runs-on: ubuntu-latest @@ -510,12 +469,9 @@ jobs: permissions: id-token: write contents: write # needed for git deploy tag - actions: write steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - fetch-depth: 0 - name: Azure Login uses: azure/login@a641126d1b8aa4d1fa005f4f92df94a3a4c4c906 # v3.1.0 @@ -525,35 +481,22 @@ jobs: subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} # Server-side copy from dev ACR to prod ACR — no artifact download needed. - # Promotes by the digest deploy-development pushed (dev tags are mutable) and fails unless prod - # ends up with that exact manifest. Later steps deploy and verify against this built digest. + # Promotes by the digest deploy-development pushed (dev tags are mutable); later steps deploy by digest. # PREREQUISITE: prod OIDC identity must have AcrPull on the dev ACR (Terraform RBAC). - name: Import image from dev ACR to prod ACR id: import env: DEV_ACR: ${{ vars.DEVCONTAINER_REGISTRY }} PROD_ACR: ${{ vars.PRODCONTAINER_REGISTRY }} - IMAGE_TAG: ${{ github.sha }} BUILT_DIGEST: ${{ needs.deploy-development.outputs.image-digest }} run: | - if ! [[ "$BUILT_DIGEST" =~ ^sha256:[0-9a-f]{64}$ ]]; then - echo "::error::deploy-development did not provide a valid image digest (got '${BUILT_DIGEST}')" - exit 1 - fi + [[ "$BUILT_DIGEST" =~ ^sha256:[0-9a-f]{64}$ ]] || { echo "::error::deploy-development did not provide a valid image digest (got '$BUILT_DIGEST')"; exit 1; } az acr import \ --name "${PROD_ACR%.azurecr.io}" \ --source "${DEV_ACR}/essentialcsharpweb@${BUILT_DIGEST}" \ - --image "essentialcsharpweb:${IMAGE_TAG}" \ + --image "essentialcsharpweb:${GITHUB_SHA}" \ --image "essentialcsharpweb:latest" \ --force - PROD_DIGEST=$(az acr repository show \ - --name "${PROD_ACR%.azurecr.io}" \ - --image "essentialcsharpweb:${IMAGE_TAG}" \ - --query "digest" -o tsv) - if [ "$PROD_DIGEST" != "$BUILT_DIGEST" ]; then - echo "::error::Prod ACR tag ${IMAGE_TAG} points to '${PROD_DIGEST}', expected the built digest ${BUILT_DIGEST}" - exit 1 - fi echo "digest=${BUILT_DIGEST}" >> "$GITHUB_OUTPUT" - name: Run database migrations @@ -601,63 +544,6 @@ jobs: # --retry-all-errors ensures HTTP 5xx (cold-start 503s) also trigger retries curl --fail --retry 10 --retry-delay 15 --retry-all-errors --max-time 30 "https://$FQDN/health" - - name: Cancel pending deployment workflow runs - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - with: - script: | - // Cancel pending workflow runs queued from earlier commits. - // Uses context.workflow instead of hardcoded filename for resilience. - let totalCancelledCount = 0; - let page = 1; - - try { - while (true) { - const { data } = await github.rest.actions.listWorkflowRuns({ - owner: context.repo.owner, - repo: context.repo.repo, - workflow_id: context.workflow, - status: 'queued', - per_page: 100, - page: page - }); - const runs = data.workflow_runs; - - if (runs.length === 0) { - break; - } - - for (const run of runs) { - if (run.id === context.runId) { - core.debug(`Skipping current run #${run.id}`); - continue; - } - - try { - core.info(`Cancelling queued run #${run.id} (commit: ${run.head_sha.substring(0, 7)}, branch: ${run.head_branch}, created: ${run.created_at})`); - await github.rest.actions.cancelWorkflowRun({ - owner: context.repo.owner, - repo: context.repo.repo, - run_id: run.id - }); - totalCancelledCount++; - } catch (error) { - core.warning(`Failed to cancel run #${run.id}: ${error.message}`); - } - } - - // Last page has fewer results than requested (pagination) - if (runs.length < 100) { - break; - } - page++; - } - } catch (error) { - core.warning(`Failed to list workflow runs: ${error.message}`); - core.info('Continuing with deployment despite cancellation script error'); - } - - core.info(`Cancelled ${totalCancelledCount} pending workflow run(s)`); - - name: Tag commit as deployed run: | git config user.email "github-actions[bot]@users.noreply.github.com" @@ -665,12 +551,3 @@ jobs: # -f allows re-tagging the same SHA on workflow re-runs git tag -f "deployed/prod/${{ github.sha }}" git push origin "deployed/prod/${{ github.sha }}" --force - - - name: Logout of Azure CLI - if: always() - uses: azure/CLI@9eb25b8360668fb0ecbafa808d40e2197b2f5f52 # v3.0.0 - with: - inlineScript: | - az logout - az cache purge - az account clear