CI / Start
Your first workflow
Your repository has tests, and nothing runs them when you push. Follow these steps to give each pull request into main a green check before anyone merges it.
5 min read
A workflow is a TypeScript file in .source/workflows/. The forge runs its jobs for the pushes and pull requests that it names.
You need:
- Write access to the repository on TanStack Source. Step 5 needs admin access.
- The
srcCLI, logged in. Install the npm package@tanstack-source/cli, which has thesrcbinary. Install and set up the CLI shows how. - A pnpm repository with vitest tests. For other tools, change the commands in step 1.
1. Write the workflow
Create .source/workflows/ci.ts:
import { defineWorkflow, pullRequest, push, report, setup } from '@tanstack-source/ci'
export default defineWorkflow('ci', {
on: [pullRequest({ branches: ['main'] }), push({ branches: ['main'] })],
})
.job('test', {
egress: ['registry.npmjs.org'],
steps: [
setup.pnpm(),
{ name: 'Install', run: 'pnpm install --frozen-lockfile' },
{
name: 'Test',
do: async (ctx) => {
const { code } = await ctx.sh.nothrow`pnpm vitest run --reporter=default --reporter=junit --outputFile.junit=junit.xml`
await report({ file: 'junit.xml', format: 'junit' }).do(ctx)
if (code !== 0) throw new Error(`vitest exited with code ${code}`)
},
},
],
})
.requireAll('gate', { needs: ['test'] })
What each part does:
on: a run starts for each pull request intomain, and for each push tomain. A merge of a pull request also starts a push run on the resulting commit inmain.egress: the job reaches only these hosts, on port 443.pnpm installneedsregistry.npmjs.org.setup.pnpm()andInstall: pnpm comes frompackageManagerinpackage.json, andInstallinstalls the packages of the lockfile.- The test step:
report()adds each failed test, with its file and line, to the findings of the run. It runs before the step fails, so the findings of a failed test run get to the forge. gate: it passes only whentestpassed. It runs no steps and needs no container. So one required check ongatecovers the whole workflow. See Require all jobs with one check.
Tip
For types in your editor, add the package as a dev dependency: pnpm add -D @tanstack-source/ci. The forge does not need it, because its runner brings its own copy.
2. Run it on your machine
src ci plan
src ci run ci test
src ci plan loads each workflow file and shows its jobs, or the errors with the file to fix:
ci (.source/workflows/ci.ts)
on: pull request (branches: main), push (branches: main)
jobs:
test
gate: requires all of test
1 workflow, 2 jobs.
src ci run ci test runs the job on your machine. When it prints Job test passed., go to the next step. If it fails, Run CI jobs on your machine shows how to fix it there.
3. Push it to main
Push the workflow to the default branch before you open a pull request:
git add .source/workflows/ci.ts
git commit -m "ci: add the ci workflow"
git push origin main
A pull request run takes the secrets, permissions and egress of each job from the base branch. So a job that is new in a pull request runs with none of them, and pnpm install cannot reach the registry.
Note
If a branch rule refuses the push, merge the workflow through a pull request. The run of that pull request fails, because its jobs are new there.
4. See the run
main is the default branch, so the push starts a run of ci at the trust level protected.
In the browser, open the CI tab of the repository (/@<owner>/<repo>/ci). Open the run to read the log, the findings and the summary of each job. The log shows each step by its name, like Install, with its result and how long it took.
The log opens at its end, at the newest lines, because the output that you want is usually at the bottom. Each open step shows its last 5,000 lines first. To load the older lines of a step, scroll up to the top of the step. On a phone, the log is part of the page, which you scroll as usual: tap Show older lines at the top of a step to load them.
In a terminal, list the runs. src ci uses your src login and the repository of your git remote:
src ci runs --branch main
ID STATUS WORKFLOW EVENT REF COMMIT
7c1e2d3f-4a5b-4c6d-8e7f-9a0b1c2d3e4f success ci push main 4e5f6a7
To use the forge without a login, set the forge variables first:
export TSS_FORGE_URL=https://forge.example/api/v1
export TSS_TOKEN=<your personal access token>
export TSS_REPO=acme/app
Tip
If a job failed, src ci view <run> --log-failed prints the logs of the failed jobs. To let an agent fix a failed run of a pull request, turn on the step Fix failing CI of the Keep green agent.
5. Make the check required
A branch rule blocks a merge into main until ci/gate passes on the head commit of the pull request. A required check is <workflow>/<job>.
src ci rule set main --require-pull --check ci/gate
Set the branch rule main: requires a pull request; required checks ci/gate.
Branch rules has the settings page and every option of a rule.
6. Open a pull request
- Create a branch, change a file, and push the branch.
- Open a pull request into
main. - Wait until the
cirun of the pull request ends.
In the right column of the pull request, the merge box says Ready to merge. Under the merge box, Checks has a green mark and the ci workflow. The workflow passed, so it is folded. Click it to see its jobs.
Note
A required check gets a row in the merge box only while it failed or did not finish. So ci/gate has no row now.
The CLI shows the same result:
src ci checks 1
Pull request #1 into main, head 9f8e7d6
NAME STATE REASON
ci/gate passed Passed on 9f8e7d6
May merge: yes
Done
Your pull request has a green check, and the merge button is available. The list of pull requests shows the same green check next to its title. From now on, a pull request into main merges only when ci/gate passed on its head commit.