From 38034187d07eba2694f943a0678f23b05c4ac147 Mon Sep 17 00:00:00 2001 From: Ivan Subotic <400790+subotic@users.noreply.github.com> Date: Tue, 9 Apr 2024 08:38:20 +0200 Subject: [PATCH] build: fix providing release version to build inside docker (#439) * build: fix providing release version to build inside docker * ci: optimize github actions --- .github/workflows/docker-build.yml | 3 ++ .github/workflows/publish.yml | 15 ++++---- .github/workflows/release-please.yml | 3 -- .github/workflows/test.yml | 2 -- CMakeLists.txt | 54 +++++++++++++++------------- Dockerfile | 6 ++-- Makefile | 6 ++-- justfile | 4 +++ query_version.cmake | 38 ++++++++++++++++++++ 9 files changed, 87 insertions(+), 44 deletions(-) create mode 100644 query_version.cmake diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml index b4f5c116..988eb11b 100644 --- a/.github/workflows/docker-build.yml +++ b/.github/workflows/docker-build.yml @@ -1,6 +1,9 @@ name: docker-build on: + schedule: + # run on midnight every day + - cron: '0 0 * * *' pull_request: jobs: diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index b25e22e5..e579f146 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,16 +1,13 @@ name: publish on: - push: - branches: - - main + # This triggers for any tag or branch creation. We'll filter for tags in the job. create: - # This triggers for any tag or branch creation. We'll filter for tags in the job. jobs: # Build for release and publish amd64 amd64: - if: github.ref == 'refs/heads/main' || (github.event_name == 'create' && startsWith(github.ref, 'refs/tags/')) + if: startsWith(github.ref, 'refs/tags/') runs-on: buildjet-4vcpu-ubuntu-2204 concurrency: group: ${{ github.ref }}-publish-amd64 @@ -33,7 +30,7 @@ jobs: # Build for release and publish aarch64 aarch64: - if: github.ref == 'refs/heads/main' || (github.event_name == 'create' && startsWith(github.ref, 'refs/tags/')) + if: startsWith(github.ref, 'refs/tags/') runs-on: buildjet-4vcpu-ubuntu-2204-arm concurrency: group: ${{ github.ref }}-publish-aarch64 @@ -58,7 +55,7 @@ jobs: manifest: runs-on: ubuntu-latest needs: [ amd64, aarch64 ] - if: (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags')) + if: startsWith(github.ref, 'refs/tags') steps: - uses: actions/checkout@v4 with: @@ -78,13 +75,15 @@ jobs: - uses: actions/checkout@v4 with: fetch-depth: 0 + - run: | + echo "RELEASE_VERSION=$(git describe --tag --dirty --abbrev=7)" >> $GITHUB_ENV - uses: getsentry/action-release@v1 env: SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }} SENTRY_ORG: ${{ secrets.SENTRY_ORG }} SENTRY_PROJECT: ${{ secrets.SENTRY_PROJECT }} with: - version: ${{ github.ref_name }} + version: ${{ env.RELEASE_VERSION }} # Publish documentation docs: diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml index c34f1285..8310bc2e 100644 --- a/.github/workflows/release-please.yml +++ b/.github/workflows/release-please.yml @@ -4,14 +4,11 @@ on: push: branches: - main - create: - # This triggers for any tag or branch creation. We'll filter for tags in the job. jobs: # Do the release-please thing release-please: runs-on: ubuntu-latest - if: github.ref == 'refs/heads/main' || (github.event_name == 'create' && startsWith(github.ref, 'refs/tags')) steps: - uses: google-github-actions/release-please-action@v4 with: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b68a9f5b..919d1232 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,8 +12,6 @@ on: schedule: # run on midnight every day - cron: '0 0 * * *' - push: - branches: [ main ] pull_request: concurrency: group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} diff --git a/CMakeLists.txt b/CMakeLists.txt index bd5aa269..b9418fbb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -318,32 +318,36 @@ if (DOXYGEN_FOUND) ) endif (DOXYGEN_FOUND) - # -# get version from git -# -if (EXISTS ${PROJECT_SOURCE_DIR}/.git) - find_package(Git) - if (GIT_FOUND) - execute_process( - COMMAND ${GIT_EXECUTABLE} describe --tag --dirty --abbrev=7 - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - OUTPUT_VARIABLE BUILD_SCM_TAG - ERROR_QUIET - OUTPUT_STRIP_TRAILING_WHITESPACE) - execute_process( - COMMAND ${GIT_EXECUTABLE} rev-parse HEAD - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - OUTPUT_VARIABLE BUILD_SCM_REVISION - ERROR_QUIET - OUTPUT_STRIP_TRAILING_WHITESPACE) - else (GIT_FOUND) - SET(BUILD_SCM_REVISION 0) - SET(BUILD_SCM_TAG 0) - endif (GIT_FOUND) -endif (EXISTS ${PROJECT_SOURCE_DIR}/.git) - -option(BUILD_SCM_TAG "Build SCM tag" "$ENV{BUILD_TAG}") +# get version from git if not provided externally +# +if (EXT_PROVIDED_VERSION STREQUAL "OFF") + message(STATUS "Getting version from git") + if (EXISTS ${PROJECT_SOURCE_DIR}/.git) + find_package(Git) + if (GIT_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} describe --tag --dirty --abbrev=7 + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE BUILD_SCM_TAG + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + execute_process( + COMMAND ${GIT_EXECUTABLE} rev-parse HEAD + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE BUILD_SCM_REVISION + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + else (GIT_FOUND) + SET(BUILD_SCM_REVISION 0) + SET(BUILD_SCM_TAG 0) + endif (GIT_FOUND) + endif (EXISTS ${PROJECT_SOURCE_DIR}/.git) +else () + message(STATUS "Using externally provided version") + SET(BUILD_SCM_REVISION 0) + SET(BUILD_SCM_TAG ${EXT_PROVIDED_VERSION}) +endif (EXT_PROVIDED_VERSION STREQUAL "OFF") message(STATUS "Build SCM tag: ${BUILD_SCM_TAG}") message(STATUS "Build SCM revision: ${BUILD_SCM_REVISION}") diff --git a/Dockerfile b/Dockerfile index 694b1e0b..d69e31c7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,11 +12,11 @@ WORKDIR /tmp/sipi # Add everything to image. COPY . . -ARG BUILD_TAG -ENV BUILD_TAG=${BUILD_TAG} +# this can be provided when the image is built +ARG VERSION=OFF # Build SIPI and run unit tests. -RUN cmake -S . -B ./build -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release --log-context +RUN cmake -S . -B ./build -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DEXT_PROVIDED_VERSION=$VERSION --log-context RUN cmake --build ./build --parallel 4 RUN cd build && ctest --output-on-failure diff --git a/Makefile b/Makefile index 755d588d..a15f1439 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ docker-build: ## build and publish Sipi Docker image locally --progress auto \ --build-arg SIPI_BASE=$(SIPI_BASE) \ --build-arg UBUNTU_BASE=$(UBUNTU_BASE) \ - --build-arg BUILD_TAG=$(BUILD_TAG) \ + --build-arg VERSION=$(BUILD_TAG) \ -t $(DOCKER_IMAGE) -t $(DOCKER_REPO):latest \ --load \ . @@ -48,7 +48,7 @@ docker-test-build-aarch64: ## locally (unit) test and publish aarch64 Sipi Docke --platform linux/arm64 \ --build-arg SIPI_BASE=$(SIPI_BASE) \ --build-arg UBUNTU_BASE=$(UBUNTU_BASE) \ - --build-arg BUILD_TAG=$(BUILD_TAG) \ + --build-arg VERSION=$(BUILD_TAG) \ -t $(DOCKER_IMAGE)-aarch64 -t $(DOCKER_REPO):latest \ --load \ . @@ -64,7 +64,7 @@ docker-test-build-amd64: ## locally (unit) test and publish x86 Sipi Docker imag --platform linux/amd64 \ --build-arg SIPI_BASE=$(SIPI_BASE) \ --build-arg UBUNTU_BASE=$(UBUNTU_BASE) \ - --build-arg BUILD_TAG=$(BUILD_TAG) \ + --build-arg VERSION=$(BUILD_TAG) \ -t $(DOCKER_IMAGE)-amd64 -t $(DOCKER_REPO):latest \ --load \ . diff --git a/justfile b/justfile index 15a956a8..046c8672 100644 --- a/justfile +++ b/justfile @@ -66,3 +66,7 @@ run: # start SIPI with Valgrind (inside NIX develop shell, e.g., 'nix clang') valgrind: valgrind --leak-check=yes --track-origins=yes ./build/sipi --config=/sipi/config/sipi.config.lua + +# query version of the project +version: + cmake -P query_version.cmake diff --git a/query_version.cmake b/query_version.cmake new file mode 100644 index 00000000..94c4b16e --- /dev/null +++ b/query_version.cmake @@ -0,0 +1,38 @@ +# +# query_version.cmake +# +# This script is used to query the value for the version of the project. +# It starts by trying to retrieve the git tag of the project. If the tag +# is the same as the version found in version.txt, then the this version +# is used. Otherwise, the version is set to the tag plus +# + +# Assuming this script is located in /my_project/query_variable.cmake +cmake_minimum_required(VERSION 3.28) + + +# get version from git +# +if (EXISTS ${CMAKE_CURRENT_LIST_DIR}/.git) + find_package(Git QUIET) + if (Git_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} describe --tag --dirty --abbrev=7 + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} + OUTPUT_VARIABLE BUILD_SCM_TAG + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + execute_process( + COMMAND ${GIT_EXECUTABLE} rev-parse HEAD + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} + OUTPUT_VARIABLE BUILD_SCM_REVISION + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + else (Git_FOUND) + SET(BUILD_SCM_REVISION 0) + SET(BUILD_SCM_TAG 0) + endif (Git_FOUND) +endif (EXISTS ${CMAKE_CURRENT_LIST_DIR}/.git) + +# Output the variable of interest +message("${BUILD_SCM_TAG}")