Master GitHub Actions with hands-on labs and exercises. Learn how to automate workflows, run tests, deploy applications, and more using GitHub's powerful automation platform. This repository has everything you need to get started with continuous integration and continuous deployment.
In this lab, you will create a GitHub Actions workflow to build and dockerize a React Tic-Tac-Toe game. The workflow will initially focus on building and pushing a Docker image without caching. Later, caching will be introduced to demonstrate its optimization benefits.
Duration: 45–60 minutes
.github/workflows/
and create a new file: react-tic-tac-toe-build-dockerize.yml
.Add the following workflow trigger configuration:
name: React Tic-Tac-Toe Build and Dockerize
on:
workflow_dispatch:
push:
paths:
- '.github/workflows/react-tic-tac-toe-build-dockerize.yml'
- 'src/react/react-tic-tac-toe/**'
Define environment variables for the Docker image and context path. These variables simplify workflow maintenance and ensure consistency.
env:
DOCKER_IMAGE: prasadhonrao/react-tic-tac-toe
CONTEXT_PATH: ./src/react/react-tic-tac-toe
Set up Docker Hub credentials as repository secrets:
DOCKER_USERNAME
: Your Docker Hub username.DOCKER_PASSWORD
: Your Docker Hub password.Add a build
job to:
Insert this code into your workflow file:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
submodules: true
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: npm ci
working-directory: $
- name: Build React App
run: npm run build
working-directory: $
Add a dockerize
job to:
Ensure the dockerize
job depends on the build
job to prevent running it prematurely.
dockerize:
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: $
password: $
- name: Build and Push Docker Image
run: |
cd $
TAG=$
docker build -t $:$TAG -t $:latest .
docker push $:$TAG
docker push $:latest
main
branch.To optimize the workflow, we’ll cache the Node.js dependencies.
Add a cache step before installing dependencies:
- name: Restore npm cache
uses: actions/cache@v3
with:
path: ~/.npm
key: $-node-$/package-lock.json') }}
restore-keys: |
$-node-
Update the build
job to include the caching step:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
submodules: true
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Restore npm cache
uses: actions/cache@v3
with:
path: ~/.npm
key: $-node-$/package-lock.json') }}
restore-keys: |
$-node-
- name: Install dependencies
run: npm ci
working-directory: $
- name: Build React App
run: npm run build
working-directory: $
Here’s the final workflow file with caching:
name: React Tic-Tac-Toe Build and Dockerize
on:
workflow_dispatch:
push:
paths:
- '.github/workflows/react-tic-tac-toe-build-dockerize.yml'
- 'src/react/react-tic-tac-toe/**'
env:
DOCKER_IMAGE: prasadhonrao/react-tic-tac-toe
CONTEXT_PATH: ./src/react/react-tic-tac-toe
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
submodules: true
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Restore npm cache
uses: actions/cache@v3
with:
path: ~/.npm
key: $-node-$/package-lock.json') }}
restore-keys: |
$-node-
- name: Install dependencies
run: npm ci
working-directory: $
- name: Build React App
run: npm run build
working-directory: $
dockerize:
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: $
password: $
- name: Build and Push Docker Image
run: |
cd $
TAG=$
docker build -t $:$TAG -t $:latest .
docker push $:$TAG
docker push $:latest