Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update flow and enhance dockerfile #3

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,33 @@ jobs:
uses: mathieudutour/github-tag-action@v6.1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

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

- name: Build release version
run: |
cmake . && make

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and push
uses: docker/build-push-action@v5
with:
push: true
build-args: GIT_COMMIT=${{ github.sha }}
tags: jerilok/cpp-build-test:latest

# This is an unofficial action and could be internilized and be recreated due to security reasons in enterprise companies.
- name: Create a GitHub release
uses: ncipollo/release-action@v1
with:
tag: ${{ steps.tag_version.outputs.new_tag }}
name: Release ${{ steps.tag_version.outputs.new_tag }}
body: ${{ steps.tag_version.outputs.changelog }}
artifacts: "/usr/src/app/main"

13 changes: 9 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
FROM gcc:9.5.0
FROM gcc:9.5.0 as builder

ARG GIT_COMMIT=unspecified
LABEL org.mybigcompany=$GIT_COMMIT
LABEL build.org.mybigcompany=$GIT_COMMIT

WORKDIR /usr/src/app

COPY . .

RUN apt-get update && apt-get install -y cmake lcov
RUN apt-get update && apt-get install -y cmake

RUN cmake . && make

CMD ["./main"]
FROM debian:buster-slim

COPY --from=builder /usr/src/app/main /usr/app/main

WORKDIR /usr/app

CMD ["./main"]
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,32 @@ To build and run the project inside a Docker container, use the provided `Docker
2. Run the Docker container:
`docker run helloworld`

## Docker Key Points:
Multi-Stage Build: This Dockerfile utilizes a two-stage build process. The first stage, named builder, is based on gcc:9.5.0 and is used for compiling the application. The second stage uses a smaller base image (debian:buster-slim in this example) for running the application.

COPY --from=builder: This command copies the compiled application from the builder stage into the second stage. Adjust the source and destination paths according to where your build system outputs the compiled binaries.

Reducing Image Size: The final image does not include the C++ compiler, CMake, or source code, significantly reducing its size.

### Considerations:

Base Image for Runtime: The choice of debian:buster-slim is a balance between size and compatibility. Depending on your application's dependencies, you might opt for another image like alpine for an even smaller footprint. However, ensure that your application and its runtime dependencies are fully compatible with the chosen base image.

Runtime Dependencies: If your application requires additional runtime libraries, you may need to install these in the second stage. Use the apt-get or equivalent package manager commands to install these dependencies, and remember to clean up the cache afterwards to keep the image size small.

## CI/CD Pipeline

This project uses GitHub Actions for continuous integration and deployment. The CI/CD pipeline automates the process of building the project, running tests, generating coverage reports, and pushing the Docker image to Docker Hub.

The pipeline configuration can be found in `.github/workflows/build.yml`.

## Release process

After the merge as it is explained below, the project is getting archived and pushed to Github Release artifacts. That includes all the source codes.
This is not correct and is subjected to a change. But it is not breaking the normal flow because it does push the SHA labeled image to HUB.
Later on we better to change it so it follows a git flow and in a separate stage push the image labled as `latest` and create the release artifactt only containing the `/usr/src/app/main` .


### Workflow

- Make changes and push them into default branch `develop`
Expand Down