JS monorepos in prod 7: Steady Integration and Steady Deployment with GitHub Actions

Faheem

The worth of CI/CD lies within the potential to regulate and coordinate adjustments and have addition in a number of, iterative releases whereas concurrently having a number of providers being actively developed in parallel. Within the earlier article of this sequence, we confirmed easy methods to implement continuous integration using Travis CI. On this article, we illustrate easy methods to obtain the identical consequence utilizing one other steady integration answer, GitHub Actions.

This text is a part of a sequence relative to JS monorepos finest practices:

Create your first CI workflow with GitHub and GitHub Actions

GitHub Actions means that you can automate, customise, and execute your software program growth workflows immediately in your GitHub repository. GitHub Actions are event-driven, which means you could run a sequence of instructions after a specified occasion has occurred. As an example, each time somebody pushes a decide to a department, you routinely run all unit assessments related to the department. You solely want an current GitHub repository to create and run a GitHub Actions workflow.

On the root of your repository, create a brand new file within the .github/workflows listing named node.js.yml. Subsequent, copy the next YAML contents into the node.js.yml file.

title: Node.js CI
on: push
jobs:
  construct:
    runs-on: ubuntu-newest
    steps:
    - makes use of: actions/checkout@v2
    - makes use of: actions/setup-node@v1
      with:
        node-version: '14.x'
    - run: yarn
    - run: yarn run take a look at

Let’s take a better take a look at what this workflow really does and what it’s composed of:

  • occasions: actions that set off the workflow. They’re represented by the on property. In our case, any push on any department will set off this workflow;
  • jobs: set of steps that execute on the identical runner. Right here, we solely run unit assessments;
  • steps: activity that may run instructions in a job. Our workflow consists of a number of steps:
    • clone the distant repository;
    • setup a selected NodeJS model (14 in our case);
    • set up dependencies;
    • execute unit assessments.
  • actions: instructions which can be mixed into steps to create a job. As an example, run executes shell instructions. The makes use of key phrase will get default actions outlined in this repository and executes them.

Committing the workflow file in your repository triggers the push occasion and runs your workflow.

View your workflow outcomes

In your GitHub repository, click on on the Actions tab. Click on on the workflow on the left sidebar. You possibly can see the standing of your workflow in addition to detailed logs of the run.




Build status success

Steady Supply with GitHub Actions

Now that you just’ve arrange automated testing on each construct, you may wish to automate deployments. As defined within the earlier article, the lerna publish from-git command is useful if you wish to simply deploy your packages.

Create a brand new file within the .github/workflows folder referred to as publish.yml. Copy the next piece of code into the file.

title: Node.js CD
on:
  launch:
    sorts: [created]
jobs:
  construct:
    runs-on: ubuntu-newest
    steps:
    - makes use of: actions/checkout@v2
    - makes use of: 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 and techniques.NPM_TOKEN }}

The workflow above runs when the launch occasion triggers. The workflow publishes the package deal to the npm registry if CI assessments go. To carry out authenticated operations in opposition to the npm registry in your workflow, you’ll must retailer your npm authentication token as a secret. In the event you already deployed packages out of your machine, the NPM_TOKEN worth may be learn out of your native .npmrc and reported to your challenge settings. Extract your npm credential:

cat ~/.npmrc | egrep '^//registry.npmjs.org' | sed 's/.*=//'

In any other case, you’ll be able to navigate to the npm registry web site and generate a brand new token manually (recommanded).




npm new token




npm token list

Copy the worth and paste it to Settings > Secrets and techniques > New repository secret within the GitHub repository.




Github add secret

A brand new entry is created.




Github view secret

This workflow builds on current Node.js instruments to simplify the method of publishing to the npm Registry. Your package deal is now accessible for the whole Open Supply group to take a look at.

Cheatsheet

  • On the root of your repository, create a brand new file within the .github/workflows listing named node.js.yml. Subsequent, copy the next YAML contents into the node.js.yml file.
    title: Node.js CI
    on: push
    jobs:
      construct:
        runs-on: ubuntu-newest
        steps:
        - makes use of: actions/checkout@v2
        - makes use of: actions/setup-node@v1
          with:
            node-version: '14.x'
        - run: yarn
        - run: yarn run take a look at
  • Create a brand new file within the .github/workflows folder referred to as publish.yml. Copy the next piece of code into the file.
    title: Node.js CD
    on:
      launch:
        sorts: [created]
    jobs:
      construct:
        runs-on: ubuntu-newest
        steps:
        - makes use of: actions/checkout@v2
        - makes use of: 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 and techniques.NPM_TOKEN }}
  • Get your NPM_TOKEN.
    cat ~/.npmrc | egrep '^//registry.npmjs.org' | sed 's/.*=//'

    Copy the worth and paste it to ‘Settings > Secrets and techniques > New repository secret’ within the GitHub repository.

Conclusion

CI utilizing GitHub Actions affords workflows that construct code in your repository and run unit assessments very simply. You possibly can publish your work to a registry as a part of your steady integration workflow. GitHub Actions is on the market with each GitHub affords. With GitHub Free for person accounts, you’ve got entry to 2,000 GitHub Actions minutes which is enough for many Open Supply initiatives.

Leave a Comment