
JS monorepos in prod 7: Continuous integration and continuous deployment with GitHub Actions
The value of CI/CD lies in the ability to control and coordinate changes and additions of features in multiple, iterative releases while multiple services are actively developed in parallel. In the previous article in this series, we showed how to implement continuous integration with Travis CI. In this article, we illustrate how to achieve the same result with a different continuous integration solution, GitHub Actions.
This article is part of a series on JS monorepos best practices:
Create your first CI workflow with GitHub and GitHub Actions
GitHub Actions lets you automate, customize, and run your software development workflows right in your GitHub repository. GitHub actions are event-driven, meaning you can run a series of commands after a specified event occurs. For example, every time someone commits to a branch, you automatically run all the unit tests associated with the branch. You only need an existing GitHub repository to create and run a GitHub Actions workflow.
In the root of your archive, create a new file in .github/workflows
directory named node.js.yml
. Then copy the following YAML content into node.js.yml
file.
name: Node.js CI
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '14.x'
- run: yarn
- run: yarn run test
Let's take a closer look at what this workflow actually does and what it consists of:
- event: activities that trigger the workflow. They are represented by
on
property. In our case, any push on any branch will trigger this workflow; - Job: set of steps running on the same runner. Here we only run unit tests;
- step: task that can run commands in a job. Our workflow consists of several steps:
- clone the remote repository;
- set a specific NodeJS version (14 in our case);
- install dependencies;
- perform unit tests.
- actions: commands that combine to step to create one Job. For example,
run
run shell commands. Theuses
keywords get default actions defined in this repository and executes them.
Committing the workflow file to your repository triggers push
event and run your workflow.
View your workflow results
On your GitHub repository, click Actions tab. Click the workflow on the left sidebar. You can see the status of your workflow as well as detailed logs of the run.
Continuous Delivery with GitHub Actions
Now that you've set up automated testing on each release, you might want to automate deployments. As explained in the previous article, lerna publish from-git
command is handy if you want to easily deploy your packages.
Create a new file in .github/workflows
folder named publish.yml
. Copy the following code snippet into the file.
name: Node.js CD
on:
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14.x'
registry-url: 'https://registry.npmjs.org'
- run: yarn
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
The workflow above runs when release
event trigger. The workflow publishes the package to npm registry if the CI tests are passed. To perform authenticated operations against the npm registry in your workflow, you must store your npm authentication token as a secret. If you have already deployed packages from your computer, NPM_TOKEN
value can be read from your local .npmrc
and reported to your project settings. Extract your npm credentials:
cat ~/.npmrc | egrep '^//registry.npmjs.org' | sed 's/.*=//'
Otherwise, you can navigate to the npm registry website and generate a new token manually (recommended).
Copy the value and paste it in Settings > Secrets > New repository secret
in the GitHub repository.
A new record is created.
This workflow builds on existing Node.js tools to simplify the process of publishing to the npm registry. Your package is now available for the entire Open Source community to view.
Cheating clap
- In the root of your archive, create a new file in
.github/workflows
directory namednode.js.yml
. Then copy the following YAML content intonode.js.yml
file.name: Node.js CI on: push jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: node-version: '14.x' - run: yarn - run: yarn run test
- Create a new file in
.github/workflows
folder namedpublish.yml
. Copy the following code snippet into the file.name: Node.js CD on: release: types: [created] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: '14.x' registry-url: 'https://registry.npmjs.org' - run: yarn - run: yarn publish env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- Get yours
NPM_TOKEN
.cat ~/.npmrc | egrep '^//registry.npmjs.org' | sed 's/.*=//'
Copy the value and paste it into ‘Settings > Secrets > New Repository Secret' of the GitHub repository.
Conclusion
CI using GitHub Actions offers workflows that build code in your repository and run unit tests very easily. You can publish your work to a registry as part of your continuous integration workflow. GitHub Actions are available with all GitHub offerings. With GitHub Free for user accounts, you have access to 2,000 GitHub Actions minutes, which is enough for most Open Source projects.
#monorepos #prod #Continuous #integration #continuous #deployment #GitHub #Actions
Source link