Automated pipelines have become an essential part of the software development process. They help ensure that code is tested and deployed consistently, saving time and reducing the possibility of errors. Here, we'll show you how to create an automated pipeline using GitHub Actions that is simple and efficient, and that ensures that committed code to the main branch is only merged if it passes through the configured pipeline.
Step 1: Set up the GitHub Actions environment
First, we need to understand that GitHub recognizes that you have a configuration for actions when you create the ".github/workflows" folder in your repository. You can do this in two ways, either through GitHub's GUI by clicking on the "Actions" tab and selecting "Set up a workflow yourself". This will allow you to create a new pipeline configuration file. Or you can manually create the directory in your repository and commit the change. The "main.yml" file will be your pipeline.
Step 2: Define the trigger for the pipeline
The next step is to define the trigger for your automated pipeline. This is done using the "push" event, which triggers the pipeline whenever a commit is made to the main branch of your repository. To define the trigger for the pipeline, add the following code to the "main.yml" file:
on:
push:
branches:
- main
Step 3: Define the jobs of the pipeline
Now it's time to define the jobs that will be executed by the pipeline. A job is a specific task that should be performed by the pipeline, such as unit or integration tests, code builds, etc. You can define several jobs for your automated pipeline.
To keep your code healthy, we suggest defining at least two jobs: one for unit tests and one for integration tests. Here is an example of how to define a unit test job:
jobs:
unit-tests:
runs-on: ubuntu-latest
steps:
- name: Use Node.js
uses: actions/checkout@v2
- name: Install dependencies
run: npm install
- name: Running unit tests
run: npm run test:unit
This job performs the following steps:
- Checkout code.
- Install the necessary dependencies.
- Run unit tests.
Going deeper into the pipeline
Personally, I like pipelines that merge into my main branch only if all tests pass. Here's an example pipeline that gives me that:
name: project-name
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: 16.x
- name: Install dependencies
run: npm install
- name: Checking lint
run: npm run lint
- name: Running unit tests
run: npm run test:unit
- name: Start application
run: npm start &
- name: Wait for application to start
run: sleep 10s
- name: Test health-check endpoint
run: curl http://localhost:3333/api/health-check
- name: Check for errors
if: ${{ job.status != 'success' }}
run: exit 1
- name: Merge to main
if: ${{ job.status == 'success' }}
uses: ad-m/github-push-action@v0.6.0
with:
branch: main
github_token: ${{ secrets.ACCESS_TOKEN }}
However, in this case we need to configure a permission setting in GitHub Actions.
Settings -> Actions -> General -> Workflow permissions and choose read and write permissions
And voila, you now have a setup to ensure good commits in your code πππ.
Refs
Top comments (0)