to add CI to a github repository:
- create
.githubdirectory in the root of your repository - create
workflowsdirectory inside.github - create
<workflow_name>.ymlinside of.github/workflows
-
examples of workflow names:
ci.yml,build.yml,deploy.yml -
good CI pipeline typically includes:
- unit tests
- integration tests
- styling checks
- linting checks
- security checks
- workflows: starts when an event occurs in github repository
- made of multiple jobs
- event: activity that triggers a workflow run
- e.g. pull requests, push
- job: set of steps that run on the same runner
- in the example below, the workflow contains a single job called "Tests"
- set of steps
- each job runs on a separate runner
- step: single task that can run commands
- e.g. installing dependencies, running tests
- execute sequentially within the same job
- in the example below, we have 3 steps:
- set up Golang
- check out the code
- force failure of the CI job
- runner: server that runs the workflows
example of workflow:
name: CI
on:
pull_request:
branches: [main]
jobs:
tests:
name: Tests
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.23.0'
- name: Force Failure
run: (exit 1)example of ci workflow example of cd workflow
-
name: name of the workflow -
on: contains the events that trigger the workflow<event_name>: name of the event that triggers the workflow (e.g.pull_request,push)branches: [<branch_name>]: defines branches where the workflow will run on
-
jobs: contains jobs that will run in parallel<job_name>: job's namename: label for a step within a job (used in GitHub Actions' UI)id: unique identifier for a step within a jobruns-on: defines the job's runner (e.g.ubuntu-latest)env: set environment variables for runner (used for shell commands withrun:)DATABASE_URL: ${{ secrets.DATABASE_URL }}: set environment variable with value from github repository's secrets
steps: contains all steps in a job- name: name of single stepuses: defines action that will be used, along with the version (e.g.actions/checkout@v4)with: adds configuration options to an action (input to an action)<config>: can beenv,go-version,working-directory, etc
run: runs a shell command
-
examples of official actions:
uses: actions/checkout@v4: copies your github repo into the runneruses: actions/setup-go@v5: installs golang into the runner
-
a step succeeds when exiting with status code of
0and fails if it exits with a status code that isn't0 -
Dynamic badge: show the current status of your CI workflow. They automatically update to reflect:
- ✅ Green: Passing - all checks succeeded
- ❌ Red: Failing - one or more checks failed
- 🟡 Yellow: In progress - workflow is currently running
To add a badge to your README.md:
- Replace
<OWNER>with your GitHub username - Replace
<REPOSITORY>with your repository name - Replace
<WORKFLOW_FILE>with your workflow filename (e.g. ci.yml)
- examples:
