git
Block 3.1: CI / CD & GitHub Actions
Ensuring the quality of all code that makes it into the main
branch.
Automatically deploy the latest state upon merge.
Save / export files from your CI / CD pipeline.
Note
Be mindful when exporting / storing artifacts as they usually count towards a storage allowance.
GitHub’s CI/CD system is called GitHub Actions and organized into workflows
which contain one or more jobs
.
job
performs multiple tasksjobs
can be chained / combined in a workflow
jobs
Repository: https://GitHub.com/world-wide-lab/world-wide-lab
act
(mediocre experiences so far).github/
Background: https://giphy.com/gifs/leonardo-dicaprio-inception-inceptionedit-4nF0gVfYYIGdi
.github
✨GitHub CI / CD pipelines are almost completely configured via .yml
files in .github/workflows
.
.github
holds special files
.github/workflows/
for CI/CDgit
.yml
✨# .github/workflows/spellcheck.yml
name: Spellcheck Action
# When should this action be run?
on: push
jobs:
build:
# Name of this job
name: Spellcheck
# Which container / platform to run on?
runs-on: ubuntu-latest
# The different steps in this job
steps:
# These can either be direct commands or use existing actions
# https://github.com/actions/checkout/tree/v3/
- uses: actions/checkout@v3
# https://github.com/rojopolis/spellcheck-github-actions/tree/v0/
- uses: rojopolis/spellcheck-github-actions@v0
name: Spellcheck
# .github/workflows/create-animation.yml
on:
workflow_dispatch:
jobs:
create_anim:
runs-on: ubuntu-latest
steps:
- uses: Platane/snk@v3
with:
# github user name to read the contribution graph from (**required**)
# using action context var `github.repository_owner` or specified user
github_user_name: ${{ github.repository_owner }}
# list of files to generate.
# one file per line. Each output can be customized with options as query string.
outputs: |
dist/github-snake.svg
dist/github-snake.gif
# Upload generated files
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: animations
path: |
dist
resources/CI-CD-examples/
(keep this open!)create-animation.yml
into .github/workflows/
in your git-example
repositoryRendering the slides for this course! ✨
# .github/workflows/deploy.yml
name: Render and Deploy
on:
workflow_dispatch:
push:
branches: main
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: pages
cancel-in-progress: false
jobs:
render:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Set up Quarto
uses: quarto-dev/quarto-actions/setup@v2
with:
tinytex: true
- name: Render Slides
uses: quarto-dev/quarto-actions/render@v2
- name: Render README / index.html
run: quarto render README.qmd --to=html
- name: Move index.html to output
run: |
mv index.html output/index.html
mv README_files output/README_files
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: output
# Deployment job
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
needs: render
runs-on: ubuntu-latest
name: Deploy
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
Rendering documentation for a software project.
# .github/workflows/deploy-docs.yml
name: Build and Deploy Documentation
on:
push:
branches: [main]
paths:
- 'docs/**'
- '.github/workflows/deploy-docs.yml'
# Changes to reference packages
- 'packages/client/**'
- 'packages/integration-jsPsych/**'
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: pages
cancel-in-progress: false
jobs:
# Build job
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0 # Not needed if lastUpdated is not enabled
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: npm
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Install root dependencies
run: npm ci
- name: Install docs dependencies
working-directory: ./docs
run: npm ci
- name: Build with VitePress
working-directory: ./docs
run: npm run build
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: docs/.vitepress/dist
# Deployment job
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
needs: build
runs-on: ubuntu-latest
name: Deploy
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
You can require checks to pass before people are allowed to merge changes into a particular branch.
resources/CI-CD-examples/
spellcheck.yml
into .github/workflows/
in your git-example
repositorySomething’s missing: the spellcheck configuration ⚙️
.spellcheck.yml
from resources/CI-CD-examples/extra/
into the root of git-example
Let’s protect our main branch and not allow in any typos anymore!
Settings
> Branches
and add a rule to “Require status checks to pass before merging”cookbook.md
, make sure you have horrible spelling!I’ve never written a workflow file from scratch, you can either start with a starter template or get matching ones for specific tasks.
ubuntu:20.04
, postgres:14
, ...
➡️ GitHub CI / CD jobs can run in Docker containers
Any Questions?