Mini.Project 01-05. Deploy: Integrated CMake, Docker and CI/CD
Prerequisites
1. CI/CD
C++ Project + CMake + Docker + CI/CD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| /Project:
- "/.github:" # Github CI/CD
- "/workflows:" # Github CI/CD
- "cmake_ci.yml" # Github CI/CD
- "docker_ci.yml" # Github CI/CD
- Dockerfile
- .dockerignore
- CMakeLists.txt
- /cmake:
- CMakeGenLib.cmake
- main.cpp
- /tests:
- test_svstring.cpp
- /lib:
- CMakeLists.txt
- /structlib:
- CMakeLists.txt
- /src:
- SVBase.cpp
- SVString.cpp
- /include:
- SVBase.h
- SVString.h
|
- /.github/workflows/*.yml defines Github Actions workflows for CI/CD
2. What is CI/CD
CI/CD stands for Continuous Integration and Continuous Deployment (or Delivery). It is a set of practices that automate the process of building, testing, and deploying software.
2-1. Continuous Integration (CI)
Continuous Integration is the practice of automatically building and testing code whenever changes are made to the repository.
When a developer pushes code:
- The project is automatically built
- Tests are executed
- Errors are detected early
This helps ensure that new code integrates smoothly with the existing codebase.
2-2. Continuous Deployment / Delivery (CD)
Continuous Deployment (or Delivery) focuses on automatically releasing the application after it has been successfully built and tested.
- Continuous Delivery → The application is ready to be deployed
- Continuous Deployment → The application is automatically deployed
This reduces manual work and speeds up the release process.
3. yml files
3-1. .github/workflows/cmake_ci.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| name: CMake CI
on: [push]
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- run: cmake -S . -B build -G "Visual Studio 17 2022" -A x64
- run: cmake --build build --config Release
- run: ctest --test-dir build -C Release --output-on-failure
|
Trigger
- The workflow runs every time code is pushed
- Ensures that new changes are automatically validated
Job Definition
1
2
3
| jobs:
build:
runs-on: windows-latest
|
- Defines a job named build
- Runs on a Windows environment provided by GitHub Actions
- Uses the latest available Windows runner with Visual Studio installed
Step
1
| - uses: actions/checkout@v4
|
- Clones the repository into the runner
- Makes the project files available for the next steps
Run
1
2
3
| - run: cmake -S . -B build -G "Visual Studio 17 2022" -A x64
- run: cmake --build build --config Release
- run: ctest --test-dir build -C Release --output-on-failure
|
The step is similar or same on cmake process. This is CI for test.
3-2. .github/workflows/docker_ci.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
| name: Docker Publish
on:
push:
branches:
- main
env:
IMAGE_NAME: $/deploy-practice
jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: $
password: $
- name: Extract short SHA
id: vars
run: echo "sha_short=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
- name: Build and Push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: |
$:latest
$:$
|
Environment Variable
1
2
| env:
IMAGE_NAME: $/deploy-practice
|
- Defines the Docker image name
- Uses a secret (DOCKERHUB_USERNAME) for security
Set up Docker Buildx
1
2
| - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
|
- Enables advanced Docker build features
- Required for efficient image building and multi-platform support
Log in to Docker Hub
1
2
3
4
5
| - name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: $
password: $
|
- Authenticates with Docker Hub
- Uses secrets to securely store credentials
1
2
3
| - name: Extract short SHA
id: vars
run: echo "sha_short=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
|
- Extracts the first 7 characters of the commit hash
- Stores it as sha_short
- Used for version tagging
Build and push Docker image
1
2
3
4
5
6
7
8
| - name: Build and Push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: |
$:latest
$:$
|
- Builds the Docker image from the current directory
- Pushes the image to Docker Hub
Two tags are created:
- latest → always points to the newest version
- commit SHA → uniquely identifies the build