GitHub Actions
GitHub Actions is a CI/CD platform that automates building, testing, and deploying code. You can set up workflows that build and test every pull request in your repository, and optionally deploy changes after those pull requests are merged, triggered automatically based on your chosen rules.
Automating testing
A common usecase of automation is to trigger automatic testing when pushing changes and creating pull requests.
The example below is taken from the CodeRefinery lesson on Continuous Integration.
MATLAB has multiple pre-defined GitHub Actions available to use in your workflows.
This workflow uses Codecov to analyse the coverage report (free service for public repositories).
⮕ Learn more about Codecov
Automating documentation generation
GitHub Pages is a static site hosting service that takes HTML, CSS, and JavaScript files straight from a repository on GitHub, optionally runs the files through a build process, and publishes a website.
In order to deploy the documentation generated by the workflow below, you need to navigate to Settings -> Pages in your repository and set:
- Source: “Deploy from a branch”
- Branch:
gh-pagesfromroot
It is a best practice to only deploy new documentation to the gh-pages branch upon a Pull Request to the main branch. This avoids mismatches between the available source code and the documentation.
Workflows for building Python packages
You can automate publishing a new version of your Python package with a GitHub Action. Notice that in the workflow below, the trigger is the creation of a new release on GitHub.
This action should only run after all other workflows (such as testing and building) have passed.
Additional concepts
Variables and secrets
Secrets are variables that you create in an organization, repository, or repository environment. The secrets that you create are available to use in GitHub Actions workflows. GitHub Actions can only read a secret if you explicitly include the secret in a workflow.
⮕ More information on using secrets in GitHub Actions.
Matrix strategy
A matrix strategy lets you use variables in a single job definition to automatically create multiple job runs that are based on the combinations of the variables. For example, you can use a matrix strategy to test your code in multiple versions of a language or on multiple operating systems.
A job will run for each possible combination of the variables. In the example below, the workflow will run 12 jobs, one for each combination of the os and python-version variables.
jobs:
example_matrix:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: ['3.10', '3.11', '3.12', '3.13']
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}⮕ More information on using a matrix for your jobs.
Artifacts
Artifacts are files or sets of files that are produced during the execution of a workflow and need to be stored or shared between jobs in a workflow.
To upload an artifact, you typically add a step in your workflow:
steps:
- name: Upload build output
uses: actions/upload-artifact@v4
with:
name: build-output
path: <path/to/build/output>Artifacts can be downloaded in subsequent jobs of the same workflow run using the actions/download-artifact action. This is done with a step like:
steps:
- name: Download build output
uses: actions/download-artifact@v4
with:
name: build-output
path: <path/to/download>By default, artifacts are retained for 90 days, but this can be configured per workflow or at the repository/organization level.
Sonar
To automate checking your code quality, you can also make use of a third-party service. SonarQube Cloud (previously known as SonarCloud) is a cloud-based code analysis service designed to detect coding issues in many different programming languages. The free plan allows you to analyze an unlimited number of public repositories. Private projects will not be importable on this plan.
- Make your repository public.
- Link your GitHub repository via their login page.
- Follow the instructions to set up the code analysis.
You can also integrate SonarQube Cloud code analysis in GitHub Actions. Typically, you would create a new workflow file, for example .github/workflows/sonar.yml, and configure triggers to your needs. You will need to set up a token in GitHub Secrets and configure what needs to be analyzed in sonar-project.properties.
The basic usage step-by-step is described in their GitHub repository.