Build Node.js App Docker Image and Push to Docker Private Repo With GitHub Actions
In this article, we will build and test the code with npm, build the Docker image, push it into the Docker hub, and use GitHub secrets to store the credentials.
Join the DZone community and get the full member experience.
Join For FreeThe following code is a sample Node server implemented with the Express.js framework. Tests for the app are written through the Mocha framework.
In this article, we will build and test the code with npm (suggested GitHub workflow). Once all the tests pass, build the Docker image, push it into the Docker hub, and use GitHub secrets to store the credentials.
Instructions to Build and Test a Node.js Application and Push Into the Docker Hub
1. Select the GitHub Repository, click on the fork button on the top right corner and select the account where the repo needs to be forked.
2. Under your repository name, click Settings.
3. In the left sidebar, click Secrets.
4. Type a name for your secrets in the "Name" input box.
5. Type the value for your secrets.
6. Click Add Secret.
7. Click the Actions tab.
8. GitHub Actions automatically suggest workflows or you may select one based on your requirements. Then, click set up this workflow.
9. This automatically creates a workflow in YAML format. Customize that workflow based on your requirements.
name: Node CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x]
steps:
- uses: actions/checkout
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node
with:
node-version: ${{ matrix.node-version }}
- name: Test the Testcases
run: |
npm install
npm run build --if-present
npm test
- name: Login to Docker Hub
run: docker login --username "${{ secrets.Docker_username }}" --password "${{ secrets.Docker_password }}"
- name: Build the Docker Image
run: docker build . --file Dockerfile --tag krishnaprasadkv/nodejs
- name: Push the Docker Image
run: docker push krishnaprasadkv/nodejs
env:
CI: true
10. After you've customized the workflow, click Start commit. Add a commit message and click Commit changes.
11. Automatically start CI. Click the Actions tab and select Running workflow.
12. Click that running workflow to see every step live Streaming logs.
13. Once the job is completed, you can search the logs based on the keywords if you want.
14. We can use that Docker image for further deployment.
Opinions expressed by DZone contributors are their own.
Comments