From 4128bd8c528a08477a2cd632a21910ac608fd00f Mon Sep 17 00:00:00 2001 From: Eric Jalal Date: Mon, 19 Feb 2024 10:44:28 +0200 Subject: [PATCH 1/8] Replace bash script with action --- .github/workflows/release-notes.yml | 41 +++++++---------------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/.github/workflows/release-notes.yml b/.github/workflows/release-notes.yml index 20b82c9..dc25704 100644 --- a/.github/workflows/release-notes.yml +++ b/.github/workflows/release-notes.yml @@ -16,36 +16,15 @@ jobs: with: fetch-depth: 0 - - name: Generate list of merges - id: get-merges - run: | - git fetch --prune --unshallow - LATEST_TAG=$(git describe --tags $(git rev-list --tags --max-count=1)) - echo "Latest tag: $LATEST_TAG" - if [[ -z "$LATEST_TAG" ]]; then - MERGES=$(git log --merges --pretty=format:"- %b") - else - MERGES=$(git log $LATEST_TAG..HEAD --merges --pretty=format:"- %b") - fi - if [[ -z "$MERGES" ]]; then - echo "No new merges to release." - echo "merges=No new merges since last release." >> $GITHUB_ENV - else - echo "merges<> $GITHUB_ENV - echo "$MERGES" >> $GITHUB_ENV - echo "EOF" >> $GITHUB_ENV - fi - # This could be moved to a separate github action for clarity - - name: Create Release - if: env.merges != 'No new merges since last release.' - uses: softprops/action-gh-release@v1 + - name: Bump version and push tag + id: tag_version + uses: mathieudutour/github-tag-action@v6.1 with: - tag_name: ${{ github.run_number }} - name: Release ${{ github.run_number }} - body: | - ${{ env.merges }} - draft: false - prerelease: false - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + github_token: ${{ secrets.GITHUB_TOKEN }} + - 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 }} From 1b18578fba1ae0e2d7d7beb63078f27e27ab4de8 Mon Sep 17 00:00:00 2001 From: Eric Jalal Date: Tue, 20 Feb 2024 13:04:46 +0200 Subject: [PATCH 2/8] Make the dockerfile multilayer for lightest running container --- Dockerfile | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index f0d1a1b..f33a99c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] From 7ee4bb796a723756c26743b45c7dcb6de4188dde Mon Sep 17 00:00:00 2001 From: Eric Jalal Date: Tue, 20 Feb 2024 13:07:18 +0200 Subject: [PATCH 3/8] Update README - Docker --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index a94b292..9eacbd5 100644 --- a/README.md +++ b/README.md @@ -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` From 9056c9419cf8dde7124fdf4e4b6320c4d57a10e0 Mon Sep 17 00:00:00 2001 From: Eric Jalal Date: Tue, 20 Feb 2024 13:23:46 +0200 Subject: [PATCH 4/8] Apply gitflow to the workflows - Only include compiled artifact in the release - Tag as latest the docker image pushed in this stage --- .../{release-notes.yml => release.yml} | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) rename .github/workflows/{release-notes.yml => release.yml} (50%) diff --git a/.github/workflows/release-notes.yml b/.github/workflows/release.yml similarity index 50% rename from .github/workflows/release-notes.yml rename to .github/workflows/release.yml index dc25704..bfd467a 100644 --- a/.github/workflows/release-notes.yml +++ b/.github/workflows/release.yml @@ -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" From aa1e055115dba78efa9530e9ead1f857a82c9862 Mon Sep 17 00:00:00 2001 From: Eric Jalal Date: Tue, 20 Feb 2024 13:23:46 +0200 Subject: [PATCH 5/8] Apply gitflow to the workflows - Only include compiled artifact in the release - Tag as latest the docker image pushed in this stage --- .github/workflows/release-notes.yml | 30 ----------------- .github/workflows/release.yml | 52 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 30 deletions(-) delete mode 100644 .github/workflows/release-notes.yml create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/release-notes.yml b/.github/workflows/release-notes.yml deleted file mode 100644 index dc25704..0000000 --- a/.github/workflows/release-notes.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: Generate Release Notes - -on: - push: - branches: - - main - -jobs: - release-notes: - runs-on: ubuntu-latest - permissions: - contents: write - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Bump version and push tag - id: tag_version - uses: mathieudutour/github-tag-action@v6.1 - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - - 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 }} - diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..dcd8824 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,52 @@ +name: Generate Release Notes + +on: + push: + branches: + - main + +jobs: + release: + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Bump version and push tag + id: tag_version + 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: | + apt-get install -y cmake \ + 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" + From 2d064532b79430033901d95159fd7617eb1f3280 Mon Sep 17 00:00:00 2001 From: Eric Jalal Date: Tue, 20 Feb 2024 13:23:46 +0200 Subject: [PATCH 6/8] Apply gitflow to the workflows - Only include compiled artifact in the release - Tag as latest the docker image pushed in this stage --- .github/workflows/release-notes.yml | 30 ----------------- .github/workflows/release.yml | 50 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 30 deletions(-) delete mode 100644 .github/workflows/release-notes.yml create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/release-notes.yml b/.github/workflows/release-notes.yml deleted file mode 100644 index dc25704..0000000 --- a/.github/workflows/release-notes.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: Generate Release Notes - -on: - push: - branches: - - main - -jobs: - release-notes: - runs-on: ubuntu-latest - permissions: - contents: write - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Bump version and push tag - id: tag_version - uses: mathieudutour/github-tag-action@v6.1 - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - - 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 }} - diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..efd22c3 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,50 @@ +name: Generate Release Notes + +on: + push: + +jobs: + release: + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Bump version and push tag + id: tag_version + 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: | + sudo apt-get install -y cmake + 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" + From b54a433abfecdb3275815342429e55cf159338cb Mon Sep 17 00:00:00 2001 From: Eric Jalal Date: Tue, 20 Feb 2024 13:44:58 +0200 Subject: [PATCH 7/8] Select gcc compilter version --- .github/workflows/release.yml | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index efd22c3..e9ca925 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -2,6 +2,8 @@ name: Generate Release Notes on: push: + branches: + - main jobs: release: @@ -21,7 +23,16 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 + # Some programmers use env matrix but this case it is just two repeated variable and I decided to keep it simple + - name: Set up compiler + run: | + sudo apt-get update + sudo apt-get install -y gcc-10 g++-10 + - name: Build release version + env: + CC: gcc-10 + CXX: g++-10 run: | sudo apt-get install -y cmake cmake . && make @@ -46,5 +57,5 @@ jobs: 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" + artifacts: "main" From 3e24d2cf847f375ec0bf918cdf22164c3fd91f51 Mon Sep 17 00:00:00 2001 From: Eric Jalal Date: Tue, 20 Feb 2024 13:44:58 +0200 Subject: [PATCH 8/8] Select gcc compilter version --- .github/workflows/release.yml | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index efd22c3..e9ca925 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -2,6 +2,8 @@ name: Generate Release Notes on: push: + branches: + - main jobs: release: @@ -21,7 +23,16 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 + # Some programmers use env matrix but this case it is just two repeated variable and I decided to keep it simple + - name: Set up compiler + run: | + sudo apt-get update + sudo apt-get install -y gcc-10 g++-10 + - name: Build release version + env: + CC: gcc-10 + CXX: g++-10 run: | sudo apt-get install -y cmake cmake . && make @@ -46,5 +57,5 @@ jobs: 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" + artifacts: "main"