Lesson 8: CI without leaking secrets¶
Goal. Run a smoke flow and a feature suite in GitHub Actions, with the secret coming from the environment and a log that is safe to keep.
You will need¶
- apic installed (lesson 1)
apic demorunning in another terminal, or start it now:
apic demo --out apic-demo
- A GitHub repository you can push a workflow to, for the last step. Every command in the workflow is also run here, locally, so the lesson works without one.
Steps¶
1. What the job has to do¶
A CI job for an API project has four jobs of its own: install apic, check the files before sending anything, run the requests, and report. And it has one rule: no secret in the workflow file, on the runner's disk, or in the log. apic was built to make that the easy path:
APIC_VAR_<name>supplies a variable from the environment, so a secret travels from${{ secrets.… }}to the request without touching a file.--redactmasks header values, bodies, query values and captured values in the output, so the log can be stored.--jsonand the exit codes give the step something to act on.- Nothing prompts, ever, so a job never hangs waiting for input.
2. Validate as the pull-request gate¶
apic validate parses every file and reports problems without sending a
request, which makes it a cheap first step. With --format github, each
finding becomes an annotation on the pull request at the exact line and
column:
apic validate -C apic-demo --format github
✓ 4 files, 24 requests, no problems
On a project with a problem the line is a workflow command, which Actions turns into an annotation:
::error file=todos.http,line=3,col=11,endLine=3,endColumn=15,title=apic%3A unknown-selector::assert "stauts == 200": unknown selector "stauts"
Errors exit 2 and fail the step; warnings alone do not.
3. Run with the secret from the environment¶
The demo's password lives in http-client.private.env.json, which is
gitignored in a real project and so does not exist on the runner. In CI
it arrives as an environment variable instead, and --redact keeps it
out of the log:
APIC_VAR_password=s3cret apic run login whoami -C apic-demo --json --redact
{"ok":true,"request":{"name":"login","file":"auth.http","line":9,"method":"POST","url":"http://localhost:8089/auth/login","headers":{"Content-Type":"***"},"body":"***"},"response":{"status":200,"status_text":"OK","headers":{"content-length":"***","content-type":"***","date":"***"},"body":"***","duration_ms":1,"size":30},"captures":{"token":"***"},"asserts":[{"expr":"status == ***","pass":true,"actual":"***","expected":"***"}]}
{"ok":true,"request":{"name":"whoami","file":"auth.http","line":20,"method":"GET","url":"http://localhost:8089/me","headers":{"Authorization":"***"}},"response":{"status":200,"status_text":"OK","headers":{"content-length":"***","content-type":"***","date":"***"},"body":"***","duration_ms":0,"size":30},"asserts":[{"expr":"status == ***","pass":true,"actual":"***","expected":"***"},{"expr":"body.$.email endsWith ***","pass":true,"actual":"***","expected":"***"}]}
What survives redaction is exactly what a pipeline needs: ok, the
status, the timing, and whether each assertion passed. What does not
survive is anything a person could copy out of the log. APIC_VAR_password
beat the private file because the shell sits above every file in the
precedence order.
Prove the secret really came from the environment by passing a wrong one:
APIC_VAR_password=wrong apic run login -C apic-demo --json --redact || echo "exit $?"
{"ok":false,"errors":["capture token: nothing at body.$.access_token"],"request":{…},"response":{"status":401,…},"asserts":[{"expr":"status == ***","pass":false,"actual":"***","expected":"***"}]}
exit 1
Exit 1, and the step fails. A network error would be 3, a missing
variable 2; the exit codes are the contract.
4. The feature suite as a test report¶
apic test writes JUnit XML, the format every CI dashboard reads, and
--redact applies to it too:
apic test -C apic-demo --format junit --output report.xml --redact
head -2 report.xml
rm report.xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="apic" tests="15" skipped="0" failures="0" errors="0" time="0.027">
(Fifteen with the features from lesson 7 and its exercise; eight from the shipped file alone.) A reporter action turns that file into a check with one line per scenario on the pull request.
5. The workflow¶
Everything above, in one file. It installs a pinned apic with the
setup-apic action, starts the demo API inside the job so the workflow is
self-contained, and runs the three steps with the secret from
${{ secrets.API_PASSWORD }}:
# .github/workflows/api.yml
name: API
on: [push, pull_request]
permissions:
contents: read
checks: write # for the test reporter
jobs:
api:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: dataGriff/api-caller/setup-apic@v0
with:
version: v0.1.2 # the release you tested against; "latest" floats
# The API under test. A real project points --env at a deployed
# environment instead and drops these two steps.
- run: apic demo --out apic-demo --port 8089 &
- run: until curl -fs http://localhost:8089/health > /dev/null; do sleep 0.2; done
- name: Validate the request files
run: apic validate -C apic-demo --format github
- name: Smoke flow
run: apic run login whoami todos.http -C apic-demo --keep-going --json --redact
env:
APIC_VAR_password: ${{ secrets.API_PASSWORD }}
- name: Feature suite
if: always()
run: apic test -C apic-demo --format junit --output report.xml --redact
env:
APIC_VAR_password: ${{ secrets.API_PASSWORD }}
- uses: dorny/test-reporter@v1
if: always()
with:
name: API features
path: report.xml
reporter: java-junit
To try it, copy the file into any repository, add a repository secret
named API_PASSWORD with the value s3cret, and push. The smoke step
goes red on the deliberate not-found failure in todos.http, which is
the point: a failed assertion fails the build, and the log shows which one
without showing a single value. Delete todos.http from the targets, or
the not-found request, and it goes green.
Three things to notice in the file. The demo is started in the
background with & and the job waits for /health before using it. The
password never appears: it goes from a repository secret to an
environment variable to the request. And if: always() runs the suite
and the reporter even when the smoke step failed, so one broken request
does not hide the rest of the report.
Where the runner is not GitHub Actions, install.sh does what the action
does:
curl -fsSL https://raw.githubusercontent.com/dataGriff/api-caller/main/install.sh | sh
Checkpoint¶
The redacted, JSON, environment-fed run from step 3 is the whole lesson in
one line. It prints "ok":true for both requests and nothing a person
could reuse:
APIC_VAR_password=s3cret apic run login whoami -C apic-demo --json --redact | grep -c '"ok":true'
2
Exercise¶
Run the smoke flow against two environments in one workflow, with a
matrix. Lesson 2's exercise gave the demo a staging environment; if you
skipped it, add one now with the same baseUrl, user set to alice,
and a staging block in the private file.
Solution
In the workflow, the smoke step becomes a matrix job:
jobs:
smoke:
strategy:
matrix:
env: [local, staging]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: dataGriff/api-caller/setup-apic@v0
with:
version: v0.1.2
- run: apic demo --out apic-demo --port 8089 &
- run: until curl -fs http://localhost:8089/health > /dev/null; do sleep 0.2; done
# The demo writes only `local`; give the job a `staging` that
# shares the same API, as lesson 2 did by hand.
- run: |
printf '{ "$shared": { "baseUrl": "http://localhost:8089" }, "local": {}, "staging": {} }' > apic-demo/http-client.env.json
- run: apic run login whoami -C apic-demo --env ${{ matrix.env }} --json --redact
env:
APIC_VAR_password: ${{ secrets.API_PASSWORD }}
Locally, the same loop:
for env in local staging; do
APIC_VAR_password=s3cret apic run login whoami -C apic-demo --env "$env" --json --redact | grep -c '"ok":true'
done
2
2
The password is the same secret for both here; a real project uses one
secret per environment (secrets.STAGING_PASSWORD) or a GitHub
environment per matrix row, and --env picks the baseUrl.
Going further¶
- A smoke test in CI that never leaks secrets in the cookbook
- Reports and CI in the testing guide
- Verifying a release, for the paranoid runner
apic validateformats, including SARIF for code scanning
Episode script
Length. 10 minutes, mostly a workflow file and the Actions UI.
Cold open (0:00). A CI log with a bearer token in it, blurred. "This is how most API tests leak. Ten minutes to a pipeline that cannot."
Talking points.
- The four jobs of a CI job and the one rule.
validate --format github: an annotation on a pull request, live.- The redacted run: what survives, what does not; the wrong password and exit 1.
- The JUnit report and the reporter check.
- The workflow file, top to bottom, then the Actions run: red on
not-found, the annotation, the report. Remove the request, green. install.shfor any other CI.- Checkpoint, then the matrix exercise.
Shot list. The workflow file in an editor; the GitHub Actions run page and the pull-request checks tab, prerecorded. One terminal for steps 2 to 4. No tape for this lesson.
Chapters. 0:00 The leak · 0:40 Four jobs, one rule · 1:30
Validate as the gate · 2:40 The secret from the environment ·
4:40 The JUnit report · 5:40 The workflow · 8:20 Other CI ·
8:50 Checkpoint and exercise.
Description. From the episode template.