Join us
@sandygudie ・ Nov 11,2021 ・ 2 min read ・ 3861 views ・ Originally posted on sandygoody.medium.com
In this tutorial, I’m going to illustrate how to handle Continuous Integration and deployment using GitHub actions.
Goals: Using GitHub Actions to automate:
- Building and pushing docker image to DockerHub,
- Deploy updates to Vercel for every push and pull request.
Prerequisite
- A React App
- A Vercel Account
- DockerHub Account
- Basic knowledge of Docker, Vercel, and GitHub
You can get the final codes on GitHub, Let’s get right in;
Step 1. Check React App is working
You could decide to create a new React project or use an existing React project. For the tutorial, I will be using an existing Ecommerce App.
Step 2. Setup for Docker
FROM node:14-alpine AS development
ENV NODE_ENV development
# Add a work directory
WORKDIR /app
# Cache and Install dependencies
COPY package.json .
COPY package-lock.json .
RUN npm install
# Copy app files
COPY . .
# Expose port
EXPOSE 3000
# Start the app
CMD [ "npm", "start" ]
Read more on building and running docker image on docker desktop
Step 3. Workflow for GitHub Action
# This is a basic workflow to help you get started with Actions
name: CI-CD
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [main]
pull_request:
branches: [main]
# # Allows you to run this workflow manually from the Actions tab
# workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains multiple jobs
build_test:
# The type of runner that the job will run on
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x]
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- name: setup node
uses: actions/setup-node@master
with:
node-version: ${{ matrix.node-version }}
# install applicaion dependencies
- name: Install dependencies
run: |
npm install
# build and test the apps
- name: build
run: |
npm run build
npm test
push_to_Docker_Hub:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# build docker image and push to docker hub
# only if the app build and test successfully
needs: [build_test]
steps:
- name: checkout repo
uses: actions/checkout@v2
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.REACTUSERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v2
with:
context: ./
file: ./Dockerfile
push: true
tags: ${{ secrets.REACTUSERNAME }}/shopping-app-react:latest
- name: Run the image in a container
uses: addnab/docker-run-action@v3
with:
image: ${{ secrets.REACTUSERNAME }}/shopping-app-react:latest
run: |
echo "runing the docker image"
push_to_vercel:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: amondnet/vercel-action@v20
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
with:
vercel-token: ${{ secrets.VERCEL_TOKEN }}
vercel-org-id: ${{ secrets.ORG_ID}}
vercel-project-id: ${{ secrets.PROJECT_ID}}
vercel-args: "--prod"
For Docker image:
For Vercel:
If all is rightly done, you should have your workflow on GitHub running and completed
After that, go ahead to check your DockerHub and Vercel for the latest updates.
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.