How to add GitHub Actions CI to an existing repo
Add GitHub Actions CI to an existing repo with a workflow that runs your project's scripts on pull requests, then require its check before merging.
To add GitHub Actions CI to an existing repo, create .github/workflows/ci.yml with a job that installs dependencies and runs the project's existing checks on pull requests and pushes to the default branch.
Start with one job and make its status check required before merging. Set permissions: contents: read, pin actions to full commit SHAs, and use the same commands developers run locally. GitHub's workflow templates can supply the starting YAML; a coding agent can adapt it after reading the repo's scripts.
How do I add a GitHub Actions workflow to an existing repo?
GitHub's Actions tab offers workflow templates for common languages. Open your repository, select Actions, and choose New workflow if workflows already exist. Find your language's template and select Configure. GitHub's Node.js CI walkthrough documents that route. You can also create the file locally on a branch and open a pull request.
A small npm project can start with the workflow below. It assumes a committed package-lock.json and scripts named lint, typecheck, test, and build. Change those commands to match your project. The runtime version belongs to your project too: this example assumes it supports Node 24.
name: CI
on:
push:
branches: [main]
pull_request:
workflow_dispatch:
permissions:
contents: read
jobs:
ci-ok:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: '24'
package-manager-cache: false
- run: npm ci
- run: npm run lint
- run: npm run typecheck
- run: npm test
- run: npm run buildThe triggers cover pull requests and changes that reach main; replace main with your default branch. workflow_dispatch adds a manual trigger once the workflow is on the default branch. These keys are covered in GitHub's workflow syntax reference. Keep ordinary code checks on pull_request; privileged triggers such as pull_request_target need a separate security review.
Require ci-ok in your branch protection rule or ruleset once it has run at least once. A green workflow alone doesn't prevent a merge with failing checks. Someone with permission to manage repository rules must configure that requirement; GitHub documents how to manage a branch protection rule.
What should a first CI workflow run?
CI should call the project's own scripts or Makefile targets. If developers run make check, use that after installing the required tools. Copying the underlying linter flags into YAML creates another command to maintain, and the local and hosted versions can drift. A check that exists only in CI should get a local script or target so someone can reproduce its failure.
Run the proposed commands locally before adding the workflow. Record failures that already exist, especially when you're asking an agent to do the setup. Otherwise, an old lint error can turn a small CI change into an unplanned cleanup. Decide whether to fix that error in the same change or handle it separately; don't make the workflow ignore it to get a green badge.
Python and Go repos use the same structure with different setup steps. GitHub's Python workflow guide describes actions/setup-python; its Go workflow guide covers actions/setup-go. Select the runtime your project supports, install its dependencies with the project's existing tool, and call its checks. An existing make lint or make test remains useful regardless of language.
Start with an unconditional sequential job if the checks fit comfortably in one runner. It gives you one status to require and a log that stops at the first failed command. Read the actual test counts as well as the exit code: a command that finds no tests can still appear successful. Add a version matrix when supporting several runtimes is a real project requirement.
Path filters need care when checks are required. GitHub explains that a workflow skipped by path filtering can leave its required check pending in troubleshooting required status checks. Keep the first workflow unfiltered. If a monorepo later needs conditional jobs, give its required final check explicit handling for failed or cancelled dependencies.
Should I pin GitHub Actions to a commit SHA?
Full commit SHAs make the selected action revision explicit. A tag can move, including a major tag such as v7. GitHub's secure use reference describes SHA pinning and the need to verify that the commit belongs to the action's own repository. The workflow above uses release commits verified for this article, with their exact tags in comments.
Resolve new pins from the action's official release instead of asking an agent to recall a hash. A plausible-looking SHA is easy to invent and hard to spot in review. Check the release's runner requirements too, especially if you operate self-hosted runners. Pinning an action does not freeze everything installed by your project's dependency commands.
Dependabot can propose action updates so pins keep moving through review. Its GitHub Actions update guide shows the configuration: a github-actions ecosystem entry in .github/dependabot.yml, directory /, and an update schedule. Add that entry to existing Dependabot configuration rather than replacing updates for application dependencies.
How do I add git hooks without breaking existing ones?
Keep a working hook manager while adding CI. Hooks give developers earlier feedback, and the hosted workflow checks pull requests independently. Adding GitHub Actions doesn't require switching from husky or the pre-commit framework. Reuse the commands those hooks already call, with fast staged-file checks before commit and longer tests before push if that fits the team.
Inventory the current hooks before changing their manager. Read the manager's configuration and inspect any hand-written hooks, including the configured core.hooksPath. Preserve each check's purpose when moving it: a staged-file formatter and a whole-repo lint command can behave differently. Test the replacement with an input it should reject and one it should accept before removing the old setup.
An agent needs explicit migration instructions if you choose lefthook. Ask it to port the existing checks first and identify changes that need your approval. Resetting a hook path can disconnect the current manager, so review that transition separately from editing the new configuration.
Can an AI agent set up CI safely?
An agent can write this workflow from a direct request with clear boundaries. Ask it to read existing scripts and workflows, preserve deploy and release jobs, resolve action versions from official releases, and run every command it puts in CI. Review changes to tests and scripts alongside the YAML, since weakening a script can hide a failure while leaving the workflow looking correct.
stacks (@steamfield/stacks) adds a written merge brief and recorded verification steps to that process. A fragment is a named capability you can apply to a repo. With the CLI installed, run this at the repository root:
stacks add ci-github git-hooks --no-launchWith --no-launch, you hand the work to the agent yourself. Open the repository in your coding agent and ask it to read .stacks/BRIEF.md and carry out its instructions, including the merges and verification.
The ci-github fragment ships no workflow file in version 0.7.0. It tells the agent to write .github/workflows/ci.yml from the brief, fold existing CI checks into it, and leave deployment and release workflows alone. The default preset calls for SHA pins; the sandbox preset allows major tags and defers pinning. add itself runs no install or verification steps.
The scripted demo shows an existing Go project with lefthook checks. The agent reads those hooks, adds the fragments, and merges a parked configuration before showing the workflow and running verify. The excerpted transcript includes the captured result from that project; it doesn't execute commands in your repo.
Add GitHub Actions CI and commit checks. Keep the lint and test hooks we already have.
I’ll read the current hooks before adding the fragments.
●Bash(cat lefthook.yml)
●Bash(stacks add ci-github git-hooks --no-launch)
●Bash(cat .stacks/incoming/lefthook.yml)
stacks parked its hook config. I’ll merge its commit-msg job into our existing hooks and write the CI workflow from the brief.
●Bash(sed -n '/^commit-msg:/,$p' .stacks/incoming/lefthook.yml >> lefthook.yml && rm .stacks/incoming/lefthook.yml)
●Bash(cat lefthook.yml)
●Bash(cat .github/workflows/ci.yml)
●Bash(stacks verify)
All 14 recorded checks passed, including the workflow and hooks checks. GitHub’s branch rules still need a person to require ci-ok.
What happens to files the repo already has?
Fragment files are written only where the destination is absent. Identical files are skipped. If the contents differ, stacks preserves the live file and parks the fragment's entire copy under .stacks/incoming/. It doesn't merge live files automatically. The existing repository guide describes those cases and the add brief's merge instructions.
An existing lefthook.yml therefore stays intact while the incoming version waits at .stacks/incoming/lefthook.yml. The agent merges the incoming commit-message job with the current checks. For repos using husky, the pre-commit framework, or hand-written hooks, the fragment asks the agent to port those checks and get your approval before resetting the hook path and removing the old manager.
The agent runs stacks verify after completing the brief. Verify reads the whole manifest, including checks that predate this addition, so existing failures can prevent a green result. A first add creates a manifest containing the fragments' verification steps; it doesn't discover every quality check your repo needs. The same command adds agent guardrails, laid out in Claude Code project structure, file by file. The command reference explains how an existing manifest is preserved.
The CI fragment's file check only confirms that ci.yml contains jobs:, a ci-ok job, and no fill-in markers. It cannot prove that GitHub ran the workflow. Review the verify reference for what a green result establishes, then inspect the first hosted run. Requiring ci-ok in GitHub's branch rules remains a separate action for a person with repository permissions.