Gate pull requests
You need the org policy to hold even when a developer skipped it locally. This page adds
one CI job that replays the lockfile and applies the policy on every pull request, then
makes it a required check. Local bypasses (--no-policy, APM_POLICY_DISABLE=1) do not
reach this job.
You need: an org policy (Set an org policy) and repositories that
commit apm.yml and apm.lock.yaml.
Add the check
Section titled “Add the check”-
Add the workflow to each repository, or to a shared workflow your repositories call:
.github/workflows/apm.yml name: APMon: pull_requestjobs:apm:runs-on: ubuntu-latestpermissions:contents: readsecurity-events: writeenv:GITHUB_APM_PAT: ${{ secrets.APM_PAT }}steps:- uses: actions/checkout@v4- uses: microsoft/apm-action@v1with:setup-only: true- run: apm policy status --check- run: apm install --frozen- run: apm audit --ci --policy org -f sarif -o apm.sarif- if: always()uses: github/codeql-action/upload-sarif@v3with:sarif_file: apm.sarifcategory: apmEach step does one job:
apm policy status --checkfails when no usable policy resolves: missing, unreachable, or no git remote.apm install --frozeninstalls exactly what the lockfile records. It fails whenapm.lock.yamlis missing or out of sync withapm.yml, and runs the install-time policy gate and content scan.apm audit --ci --policy orgruns the Baseline CI checks, replays the lockfile to catch drift, and applies the org policy. It exits 1 on any failure.if: always()uploads SARIF even when the audit failed, so findings show up in GitHub code scanning.
APM_PATneeds read access to your private packages and the policy repository.setup-only: trueinstalls the CLI and nothing else. -
Open a pull request and let the job run once. GitHub registers the check under the job name,
apm. -
Make it required with a ruleset (next section). Do not add
paths:filters to a required workflow: a skipped required check stays pending and blocks merges.
What a red check looks like
Section titled “What a red check looks like”Reproduce both locally with a policy file before you roll out. In CI the source is
org; the output is the same.
A dependency outside the policy. Under enforcement: block:
apm audit --ci --policy ./apm-policy.yml...| [x] | dependency-allowlist | 1 dependency(ies) not in allow list || [+] | drift | no drift detected against lockfile |+--------------------------------------------------------------------------------------------------+
dependency-allowlist details: - github.com/github/awesome-copilot/skills/review-and-refactor: not in allowed sources Run 'apm uninstall microsoft/apm-sample-package' to remove the dependency outside the org allow list, or ask your org admin to allow it (policy source: file:apm-policy.yml).
[x] 1 of 11 check(s) failedThe last line names the policy that denied the dependency and the apm.yml entry to
remove (for a transitive dependency, the direct dependency that brings it in). APM
offers the apm uninstall only when it removes every denied dependency without
dropping a required package; otherwise the line says to ask your org admin. SARIF
results carry the same sentence.
Under enforcement: warn the same row reads [!] ... (enforcement: warn) and the job
passes. Until you switch to block, a green check does not mean the policy holds.
A policy that cannot be loaded. An explicit source that fails to load fails closed:
apm audit --ci --policy ./policy-mirror/apm-policy.yml[x] Policy './policy-mirror/apm-policy.yml' could not be loaded: Policy file not found: ./policy-mirror/apm-policy.yml --policy was passed explicitly, so the audit fails closed instead of skipping enforcement. Fix the policy source above and re-run.With --policy org, a fetch failure fails the audit when the repository’s
apm.yml sets policy.fetch_failure_default: block; without it, the audit warns and
skips policy checks. If the policy file is deleted, --policy org runs no policy checks at all. That
is why the workflow runs apm policy status --check before the audit: it exits 1 in both cases.
Require the check with GitHub rulesets
Section titled “Require the check with GitHub rulesets”An organization ruleset makes the check mandatory across many repositories at once.
- Organization Settings > Repository > Rulesets > New branch ruleset.
- Enforcement status: start with
Evaluate(logs would-be blocks), thenActive. - Bypass list: leave it empty. Anyone listed can merge a red check.
- Target repositories: all, or those with a custom property such as
uses-apm. - Target branches: the default branch.
- Rules: require a pull request, and require status checks to pass with the check
apmfrom GitHub Actions. Enable “require branches to be up to date”.
The check name must match the registered name exactly; copy it from a pull request’s checks tab. Protect the policy repository the same way, with CODEOWNERS for your team.
Azure DevOps and GitLab
Section titled “Azure DevOps and GitLab”The exit code is the gate on every CI system. Run the same three commands and keep
apm.sarif as a build artifact.
steps: - script: | curl -sSL https://aka.ms/apm-unix | sh -s -- --prefix "$HOME/.local" export PATH="$HOME/.local/bin:$PATH" apm policy status --check apm install --frozen apm audit --ci --policy org -f sarif -o apm.sarif env: ADO_APM_PAT: $(ADO_PAT)In Azure Repos, add the pipeline as a Build validation branch policy on the default branch.
apm: image: python:3.12 script: - pip install apm-cli - apm policy status --check - apm install --frozen - apm audit --ci --policy org -f sarif -o apm.sarif artifacts: when: always paths: [apm.sarif]In GitLab, set the merge check Pipelines must succeed. The CI token needs read
access to every apm-policy project in the group path.
When the check fails
Section titled “When the check fails”| Failed check | Fix |
|---|---|
lockfile-exists, ref-consistency, --frozen refused | Run apm install, commit apm.yml and apm.lock.yaml |
content-integrity, drift | A deployed file was edited: run apm export-patch to keep the edit upstream, then delete the file and run apm install to restore the locked file (a plain apm install keeps an edited file and names it, exiting 1 when the edit blocks new content or install cannot prove it wrote the current bytes). content-integrity also fails on critical hidden Unicode; review the file before you strip or replace it |
dependency-allowlist | If the check prints an apm uninstall command, run it. Otherwise use an allowed package, or open a pull request against the policy repository |
dependency-denylist | Use an allowed package, or open a pull request against the policy repository |
apm policy status --check | Discovery failed; see Debug a policy failure |
There is no per-pull-request override. An exception is a change to apm-policy.yml,
reviewed like any other.
Related
Section titled “Related”- Baseline CI checks: every check
--ciruns. apm audit: all flags and output formats.- Run in CI: the single-repository version of this job.
Copyright (c) Microsoft Corporation. General documentation: CC-BY-4.0. Software, code examples and OpenAPM: Apache-2.0. See scope and exclusions.