AI models can write a working GitHub Actions workflow in one prompt, but “it runs” and “it is safe to merge” are different bars. The prompts that produce usable CI/CD pipelines give the model your real build context, ask for one stage at a time, and spell out the security rules GitHub itself recommends: least-privilege GITHUB_TOKEN permissions, actions pinned to commit SHAs, untrusted input kept out of shell scripts, and production deploys behind an environment with required reviewers. This guide gives you those prompts, explains why each line is there, and shows a before/after workflow.
Why AI-written workflows need more guidance than AI-written code
A CI/CD workflow runs with credentials, so a mistake in it is a security problem, not just a bug. The workflow file decides who can trigger a job, which code it executes, and what the job’s token can write to. A model asked for “a workflow that tests and deploys my Node app” will optimize for something that turns green on the first run. Left alone, it tends to:
- reference actions by floating tags such as
@v4or even a branch name; - omit the
permissionskey, so the job gets whatever the repository default is; - interpolate values like a pull request title directly into a
run:script; - put build and deploy in parallel jobs, or deploy from every branch;
- suggest third-party actions from memory, sometimes ones that are renamed, archived or deprecated (a broader problem covered in why AI coding assistants keep suggesting deprecated APIs).
None of these break the build, which is exactly why they survive review. The fix is to put the guardrails in the prompt so the first draft already follows them.
Step 1: Give the model your pipeline context
The model cannot see your repository settings, your branching model or your deploy target, so tell it. GitHub Actions is built from events (what triggers a run), workflows, jobs, steps and runners; describing your process in those terms gets a much closer first draft. Reuse this block at the top of every prompt below.
## Pipeline context
- Repo: private, GitHub Team plan
- Language / tooling: Node.js 22, npm, Vitest for tests, ESLint
- Lockfile: package-lock.json (committed)
- Branching: feature branches -> PR -> main. Only main deploys.
- Deploy target: AWS (ECS service), region us-east-1
- Cloud auth: GitHub OIDC to an IAM role (no long-lived AWS keys)
- Environments in repo settings: "staging" (no approval),
"production" (required reviewers, main branch only)
- Runner: GitHub-hosted ubuntu-latest
- Definition of "passing": lint clean, all tests pass, build succeeds
The lines that matter most are the ones about credentials and environments. If you tell the model you use OIDC, it will write id-token: write and a role assumption step instead of reading AWS_ACCESS_KEY_ID from secrets. If you tell it that a production environment exists with protection rules, it will reference that environment rather than inventing a manual-approval hack.
Step 2: Generate the build-and-test workflow
Start with the narrowest useful workflow: test on pull requests. It has no deploy credentials, so it is the safest place to get the structure right.
[Pipeline context]
Write .github/workflows/ci.yml that:
1. Triggers on pull_request to main and on push to main.
2. Sets top-level `permissions: contents: read` and grants nothing else.
3. Uses only official actions (actions/*) unless I approve others.
Pin every action to a full-length commit SHA with the version tag
in a trailing comment, e.g. `uses: actions/checkout@<sha> # vX.Y.Z`.
If you do not know the current SHA, write `<SHA-TO-FILL>` and
tell me where to look it up. Never invent a SHA.
4. Installs dependencies with the lockfile (frozen) and caches the
package store.
5. Runs lint, tests and build as separate steps so failures are clear.
6. Sets a job timeout-minutes value appropriate for this project.
7. Adds a concurrency group so a new push to the same PR cancels the
previous in-progress run.
After the YAML, list every assumption you made.
Why each requirement is there:
- Requirement 2. GitHub’s workflow syntax reference says that once you specify any permission, every permission you leave out is set to
none. Declaringcontents: readexplicitly therefore limits the token regardless of the repository default. - Requirement 3. GitHub’s security hardening guide recommends pinning actions to a commit SHA, because a tag can be moved or deleted by someone who gains access to the action’s repository. The “never invent a SHA” clause matters: a model will otherwise produce a plausible 40-character string that points at nothing. The trailing version comment also lets Dependabot update the comment when it bumps the SHA, according to the same guide.
- Requirement 7. The
concurrencykey withcancel-in-progress: truecancels the currently running job in the same group, which saves runner minutes on busy PRs. - The assumptions list surfaces guesses (Node version, cache path, build command) before they become silent bugs.
Step 3: Add the deployment stages separately
Ask for deployment in a second prompt, after the CI workflow works, because deploy jobs are where credentials and approvals live. Asking for everything at once produces a long file in which the risky parts get the least attention.
[Pipeline context]
Here is my working ci.yml:
[paste]
Extend it with deployment:
1. deploy-staging: runs only on push to main, needs the test job,
uses environment "staging".
2. deploy-production: needs deploy-staging, uses environment
"production". Do NOT build a manual approval step yourself; the
environment's required reviewers handle approval.
3. Authenticate to AWS with OIDC. Grant `id-token: write` ONLY on the
deploy jobs, not at workflow level. No AWS keys in secrets.
4. Never echo, print, or write secrets or tokens to files that get
uploaded as artifacts.
5. Add a concurrency group per environment with cancel-in-progress
false, so two production deploys never overlap and a running
deploy is never killed halfway.
6. Build the image once and deploy the same artifact to staging and
production; do not rebuild for production.
Explain the job dependency graph in one sentence per job.
Requirement 2 relies on a GitHub feature rather than YAML tricks. The environments documentation explains that a job referencing an environment must pass its protection rules before it runs or can read that environment’s secrets, and that required reviewers (up to six people or teams) can approve the job. Check plan availability first: for private repositories on Free, Pro or Team plans, the same page says rules such as required reviewers and wait timers are only available on public repositories on those plans. If that applies to you, tell the model, because the right design changes.
Requirement 5 is the opposite of the CI setting on purpose. Cancelling an outdated test run is harmless; cancelling a deploy halfway through can leave a service partially updated.
Step 4: Audit the result, or any existing workflow
Auditing is where AI is most useful on workflows you already have, because it reads every line with the same attention. Ask for findings, not a rewrite, so you can judge each one.
Audit this GitHub Actions workflow. Do not rewrite it.
Check for:
1. Actions not pinned to a full-length commit SHA.
2. Missing or overly broad `permissions` (workflow and job level).
3. Untrusted context values (github.event.pull_request.title, body,
head_ref, issue.title, comment.body, etc.) used directly inside
`run:` scripts instead of via an intermediate env variable.
4. Use of pull_request_target or workflow_run that checks out or
executes code from the pull request.
5. Secrets that could reach logs or uploaded artifacts.
6. Long-lived cloud credentials that could be replaced with OIDC.
7. Deploy jobs without `needs:` on the test job or without an
environment.
8. Missing dependency caching or timeouts.
Output a table: line | issue | severity (high/medium/low) | fix.
If something is fine, do not list it.
```yaml
[paste workflow]
```
Items 3 and 4 are the ones most worth a careful look. GitHub’s hardening guide recommends passing untrusted values to inline scripts through an intermediate environment variable rather than interpolating the expression into the script, because ${{ }} expressions are substituted before the shell runs, so a crafted PR title can become shell code. It also advises avoiding pull_request_target unless you need it and never checking out untrusted pull request code in workflows that use it. For a longer line-by-line review routine, see the checklist for reviewing an AI-generated GitHub Actions workflow.
Before and after: the same request with and without guardrails
The example below is illustrative. The “before” is typical of what a bare prompt like “write a GitHub Actions workflow that tests my Node app and deploys to AWS” returns; the “after” is the shape the prompts above lead to. SHAs are placeholders on purpose.
Before
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- uses: actions/setup-node@v4
- run: npm install
- run: npm test
- run: echo "Testing PR: ${{ github.event.pull_request.title }}"
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- run: ./deploy.sh
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
Problems, in order of severity: deploy has no needs:, so it runs in parallel with tests and deploys broken code; it has no branch condition, so it deploys from every pushed branch; the PR title is interpolated into a shell command; long-lived AWS keys are stored as secrets; actions are on a branch and a floating tag; there is no permissions block; and npm install can update the lockfile, while npm ci installs exactly what it lists.
After
on:
pull_request:
branches: [main]
push:
branches: [main]
permissions:
contents: read
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@<SHA-TO-FILL> # vX.Y.Z
- uses: actions/setup-node@<SHA-TO-FILL> # vX.Y.Z
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run lint
- run: npm test
- name: Log PR title safely
if: github.event_name == 'pull_request'
env:
PR_TITLE: ${{ github.event.pull_request.title }}
run: echo "Testing PR: $PR_TITLE"
deploy-production:
needs: test
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: production
concurrency:
group: deploy-production
cancel-in-progress: false
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@<SHA-TO-FILL> # vX.Y.Z
- uses: aws-actions/configure-aws-credentials@<SHA-TO-FILL> # vX.Y.Z
with:
role-to-assume: ${{ vars.AWS_DEPLOY_ROLE_ARN }}
aws-region: us-east-1
- run: ./deploy.sh
Every change maps to a documented recommendation: least-privilege token, SHA pinning, intermediate env variable for untrusted input, OIDC instead of stored keys, an environment for approval, and serialized deploys. The staging stage is omitted here only to keep the example short.
Common errors in AI-generated workflows and how to fix them
| Symptom | Likely cause | Fix |
|---|---|---|
| Deploy runs even when tests fail | Jobs run in parallel by default; no needs: | Add needs: test to the deploy job |
| “Resource not accessible by integration” | Token lacks a permission the step needs after you restricted permissions | Grant that one permission at job level, not write-all |
| OIDC step fails to get a token | id-token: write missing on the job | Add it to the deploy job’s permissions |
| Action version “not found” | Model invented a SHA or tag | Copy the SHA from the action’s release page in its own repository |
| Secrets empty on PRs from forks | By design, secrets are not passed to fork PR runs | Keep fork PRs to test-only jobs; do not switch to pull_request_target to work around it |
| Production deploy never asks for approval | Environment not referenced, or protection rules unavailable on your plan | Add environment: production and check plan support |
Practical tips for the rest of the pipeline
- Keep pinned actions current. Pinning freezes versions, so add a Dependabot configuration for the
github-actionsecosystem. Note that the hardening guide says Dependabot does not raise vulnerability alerts for SHA-pinned actions, only version-update and security-update pull requests. - Catch secrets before they reach CI. A pre-commit secret scanner stops the problem upstream; Gitleaks’ pre-commit setup covers one option.
- Infrastructure steps. If the pipeline runs Terraform, apply the same stage-by-stage approach with the prompts in AI prompts for Terraform and infrastructure-as-code, and make
planoutput part of the review. - AI agents in CI. If you run a coding agent inside the pipeline, its permissions need the same least-privilege treatment as the token; configuring Claude Code’s dontAsk mode for CI walks through one setup.
- Pricing and minutes. Do not ask a model how many free minutes you have; check GitHub’s billing documentation for your plan.



