Easy CI/CD with Github Actions


Easy CI/CD with Github Actions

Setting up continuous integration and deployment can be complicated, but it doesn’t have to be that way. Github Actions are a great way to dip your toes into automating your testing and builds.

Remember, automated testing and deployment still relies on well written tests and well defined infrastructure, it just helps to remove some of the human error. 

What are Github Actions, and How Are They Useful?

Github Actions provide a way to run a workflow, triggered by events on Github. This means we can run our test suite whenever a pull request is opened, or trigger a deployment when a pull request is merged into your main branch.

One of the big differentiating features of Github Actions is the marketplace, which gives you access to over 9000 different building blocks that can be used to kick start your setup.

Setting up Github Actions

First, create a     `.github/workflows` folder in the root directory of your repository.

To create new actions, you create YAML files inside of that directory. Each YAML file defines what triggers to listen for, and what jobs to run when those triggers are met.

Here is an example that would build a React app, that uses yarn.


# .github/workflows/pr.yml
name: Pull Request

on:
  pull_request: # Trigger this job when a pull request is made into main
    branches: [ main ]

jobs:
  web:
    runs-on: ubuntu-latest # Create build environment
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js 14.x
      uses: actions/setup-node@v2
      with:
        node-version: 14.x # Choose node version
    - run: npm install -g yarn
    - run: yarn ci # Run the “ci” command defined in your package.json

And that’s it! If you commit this file to your repository, it will be automatically detected by Github. These actions can do as little or as much as you want.

For example, you could use custom scripts to spin up new instances and resources, run migrations, and run smoke tests before deployment, or you could do something as simple as syncing your static files to an S3 bucket.

Don’t forget to leverage the marketplace, to turbocharge your actions. Want to sync to S3? Easy. Want to configure your AWS credentials, for use with CDK? That’s easy too.

But How Do I Test It?

If you have ever set up a CI/CD before, then you likely know the pains of making a small change, pushing them, waiting for the CI/CD to build, and looking at the results.

Luckily, Nektos/act makes testing github actions trivial. Install act, and then run

act pull_request


That will use Docker to set up a container that matches Github Action’s environment, and then it will run your pull_request action, that we created above, in pr.yml.

Seriously, that’s all the setup you need to do, to run your application locally.

Summary

We just set up a very simple CI in only a few minutes, with the ability to leverage a large marketplace of pre-written scripts, and we can test it locally, on any platform.

Who knew it could be this simple?