Hopp til innhold

What is GitHub Actions?

A way to automate things in GitHub. Automatically run workflows to enable continuous integration / continuous delivery and others.

Why?

  • Testing
  • Building
  • Deployment
  • Consistent results

Anatomy of a workflow

Name
A workflow has a name.


Trigger event

This decides when the workflow is triggered.

Other trigger events are:

  • A test
  • Release
  • Push
  • Many more

Jobs
A workflow consists of one or multiple jobs that can run in succession or in parallel. A job can be dependent on another job successfully finishing.

For example, you can have a `test` job and a `lint` job running in parallel because they are not dependent on one another. And then you can have a `deploy` job that depends on both the `test` and ´lint` jobs successfully running. If either of `lint` or `test` jobs fail the `deploy` job will not run.

One job
A job consists of some properties and one or more steps. An important property is `runs-on`. Runs-on defines the runner the job will run on. For example, ubuntu-latest. This can be any type of runner that GitHub offers, or even a self-hosted runner.

The first step in a job will normally check out the source code from the repo we are in:

The following steps depends on what the workflow should do. In this example we will build and deploy the application.
The next step will install npm modules:

Now it’s time to build our project:

And then finally we will deploy it:

Testing
You probably want to automate running your tests as part of your CI/CI pipeline. As previously mentioned, you can run your tests in a job:

To make successful running of actions a requirement for your pull requests to be able to merge in to master you need to set this up in your GitHub repository.
Here we require that the project builds properly before being able to merge
This setting can be found in Settings -> Branches -> Branch protection rules.

Triggers

Workflows can be triggered by different events.

Common events are:

  • pull_request
  • push
  • release
  • workflow_dispatch

pull_request
Will run when a pull request is created or updated. This could be tests and linting running as requirements for the pull request to be merged.

push
This runs when a push is made to the specified branch, in this example: master branch.

release
Runs when a new release is published

workflow_dispatch
Workflow can be triggered manually.

This can be useful if you want to deploy a branch at will to your test environment.

Summary

GitHub Actions can be used to run tests, build your projects and deploy to your environments. Workflows consists of jobs and steps that can be triggered by various events. For example pull requests or a push to master. This saves time compared to doing testing, building and deployment manually.

Flere artikler