DEV Community

Cover image for How to configure CI/CD for your Node.js application
Olalekan Oladiran
Olalekan Oladiran

Posted on

How to configure CI/CD for your Node.js application

Requirements

Introduction

In this section, you'll configure GitHub Actions to automate:

  • Building and testing your Docker image
  • Pushing the validated image to Docker Hub You'll complete these key steps:
  • Create a new repository on GitHub.
  • Define the GitHub Actions workflow.
  • Run the workflow.

Step one: Create the repository

Create a GitHub repository, configure the Docker Hub credentials, and push your source code.

  1. Create a new repository Image description Image description
  2. Open the repository Settings, and go to Secrets and variables > Actions. Image description Image description
  3. Create a new Repository variable named DOCKER_USERNAME and your Docker ID as a value. Image description Image description
  4. Create a new Personal Access Token (PAT) for Docker Hub. You can name this token docker-tutorial. Make sure access permissions include Read and Write. Image description Image description Image description Image description
  5. Add the PAT as a Repository secret in your GitHub repository, with the name DOCKERHUB_TOKEN Image description Image description
  6. In your local repository on your machine, run the following command to change the origin to the repository you just created. Make sure you change your-username to your GitHub username and your-repository to the name of the repository you created.

git remote set-url origin https://github.com/your-username/your-repository.git

  1. Run the following commands to stage, commit, and push your local repository to GitHub.
git add -A
git commit -m "my commit"
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Image description
You can also confirmed that it has been pushed to your repo in your GitHub.
Image description

Step two: Set up the workflow

Set up your GitHub Actions workflow for building, testing, and pushing the image to Docker Hub.

  1. Go to your repository on GitHub and then select the Actions tab. Image description
  2. Select set up a workflow yourself. This takes you to a page for creating a new GitHub actions workflow file in your repository, under .github/workflows/main.yml by default. Image description
  3. In the editor window, copy and paste the following YAML configuration.
name: ci

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ vars.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Build and test
        uses: docker/build-push-action@v6
        with:
          target: test
          load: true

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          platforms: linux/amd64,linux/arm64
          push: true
          target: prod
          tags: ${{ vars.DOCKER_USERNAME }}/${{ github.event.repository.name }}:latest
Enter fullscreen mode Exit fullscreen mode

Image description

Step three: Run the workflow

  1. Select Commit changes... and push the changes to the main branch. After pushing the commit, the workflow starts automatically. Image description Image description
  2. Go to the Actions tab. It displays the workflow. Selecting the workflow shows you the breakdown of all the steps. Image description
  3. When the workflow is complete, go to your repositories on Docker Hub. If you see the new repository in that list, it means the GitHub Actions successfully pushed the image to Docker Hub. Image description N.B: I encountered error when I run my job due to my repo name, it suppose to be in lowercases, hence, I changed my repo name from Node-application to node-application.

Thanks for staying till the end

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤ī¸ or a friendly comment on this post if you found it helpful!

Okay