Github Actions is a system for running workflows on Github's infrastructure to "do stuff when things happen". More specifically:
- Your Github Actions workflows live in the
.github/workflowsdirectory in your repository. - Github Actions will trigger when an event happens and kick off a workflow that consists of one or more jobs.
Let's take a look at a very simple workflow that will run our unit tests.
name: Unit Tests
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node
uses: actions/setup-node@v3
- run: npm ci
- run: npm testOkay, so what's going on here:
name: Signifies the name of the worklow, which you can see in the screenshot.on: What kind of event are we listening for? In this case, we're listening for two events:- A
pushto the main branch. - A
pull_requestthat targets the main branch.
- A
jobs: A list of jobs that will run in parallel. Right now, we only have one and it's calledbuild-and-test.stepseach job has one or more steps. These steps run synchronously (one after the other). Right now, we have the following steps:actions/checkout@v3: This one is super important. It checks out the repository. For most actions where you might want to run tests, build your application, deploy your application, etc., you're probably going to need the repository, right?actions/setup-node@v3: We gave this one the cute name "Setup Node" as well. But, this one does what it says on the tin. It installs Node.js onto whatever container your action is running in.run: I'll get into this in a bit, but really, you have two major types of things you can do:runcommands anduseactions. In this case, we're runningnpm ciand thennpm test.
npm ci is a variation on npm install, but it has a few important cavaets:
- It insists on you already having a
package-lock.json. - It will error out instead of updating
package-lock.jsonif something doesn't match. - It install all of the dependencies. I.e. you can't just use it to install one depenedency like
npm ci @testing-library/reactor something. - It blows away
node_modulesif present. - It doesn't write to
package.json.
- Right now
build-and-testonly tests. Add a step that runsnpm run buildto the action - Bonus 1:: Add a name to the step where we check out the repository.
- Bonus 2:: Run
npm run buildin parallel withnpm test.

