Github Actions

Github Actions makes it easy to test, build, and deploy your code into the cloud.

Workflow

A workflow is a configurable automated process that will run one or more jobs. Workflows are defined by a YAML file checked in to your repository and will run when triggered by an event in your repository, or they can be triggered manually, or at a defined schedule.

Events

Events are specific activity that triggers a workflow run. For example, activity such as pushing code to a repository or opening a pull request. You can configure a workflow to run when specific activity on GitHub happens. Common events are push, pull_request, schedule, etc.

Example: push event. Runs your workflow when you push a commit or tag, or when you create a repository from a template.

1
2
on:
push

Jobs

A workflow run is made up of one or more jobs, which run in parallel by default.

Use jobs.<job_id> to give your job a unique identifier. To run jobs sequentially, you can define dependencies on other jobs using the jobs.<job_id>.needs keyword.

Dependant jobs will run after the jobs they depend on have completed successfully. If a job fails, the dependant jobs will not run.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
jobs:
setup:
runs-on: ubuntu-latest
steps:
- run: ./setup_server.sh
build:
needs: setup
runs-on: ubuntu-latest
steps:
- run: ./build_server.sh
test:
needs: build
runs-on: ubuntu-latest
steps:
- run: ./test_server.sh

Runners

A runner is a server that runs your jobs. You can use a GitHub-hosted runner, or you can host your own runner. Each job in a workflow run runs on a fresh instance of the runner.

1
runs-on: ubuntu-latest

Steps

Each job in a workflow run is made up of one or more steps. Steps are a sequence of tasks that execute commands. Steps can run commands, run setup tasks, or run an action in your repository, a public repository, or an action published in a Docker registry.

Example

In your repository on GitHub, create a workflow file called github-actions-demo.yml in the .github/workflows directory.

github-actions-demo.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
name: CI

# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the "main" branch
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v4

# Runs a single command using the runners shell
- name: Run a one-line script
run: echo Hello, world!

# Runs a set of commands using the runners shell
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.

Commit the file to the main branch of your repository. This will trigger the workflow to run on the next push to the repository.

References: