From a65a9c2dab50c195c84689c66775579824ef29fa Mon Sep 17 00:00:00 2001 From: Justin Kolberg Date: Tue, 29 Oct 2024 21:01:12 -0700 Subject: [PATCH] ci: only use workflow_id for building packages Signed-off-by: Justin Kolberg --- .github/workflows/_reusable_build_package.yml | 146 +++++------------- .github/workflows/build_packages.yml | 141 +---------------- .github/workflows/release.yml | 75 +++++++++ CMakeLists.txt | 21 ++- Makefile | 50 ++++-- README.md | 1 + ci/github-actions/cmake/action.yml | 20 ++- ci/github-actions/make/action.yml | 8 +- components/otelcol-sumo.cmake | 16 +- docker/entrypoint.sh | 21 +-- docker/install-deps.sh | 21 +++ external_projects.cmake | 1 - external_projects/otelcol_sumo.cmake | 25 --- packages.cmake | 123 ++++----------- settings/common.cmake | 2 +- settings/otc.cmake | 54 ++----- utils.cmake | 1 + utils/detect_version.cmake | 32 ++++ utils/variables.cmake | 9 ++ version.cmake | 51 ++---- version_artifact.cmake | 109 +++++++++++++ 21 files changed, 440 insertions(+), 487 deletions(-) create mode 100644 .github/workflows/release.yml delete mode 100644 external_projects.cmake delete mode 100644 external_projects/otelcol_sumo.cmake create mode 100644 utils/detect_version.cmake create mode 100644 version_artifact.cmake diff --git a/.github/workflows/_reusable_build_package.yml b/.github/workflows/_reusable_build_package.yml index c7b71034..dc3d562d 100644 --- a/.github/workflows/_reusable_build_package.yml +++ b/.github/workflows/_reusable_build_package.yml @@ -3,12 +3,6 @@ name: '_reusable_build_package' on: workflow_call: inputs: - otc_version: - required: true - type: string - otc_sumo_version: - required: true - type: string otc_build_number: required: false type: string @@ -18,10 +12,6 @@ on: workflow_id: required: false type: string - use_release_artifacts: - required: false - type: boolean - default: false runs_on: required: true type: string @@ -80,7 +70,7 @@ jobs: - name: Download packagecloud-go tool run: | baseURL="https://github.com/amdprophet/packagecloud-go/releases/download" - version="0.1.5" + version="0.2.2" file="packagecloud-go_${version}_linux_amd64.tar.gz" curl -Lo /tmp/packagecloud-go.tar.gz $baseURL/$version/$file @@ -90,7 +80,6 @@ jobs: sudo mv /tmp/packagecloud /usr/local/bin - name: Workflow URL for sumologic-otel-collector - if: ${{ !inputs.use_release_artifacts && inputs.workflow_id != '' }} run: | org="SumoLogic" repo="sumologic-otel-collector" @@ -111,10 +100,6 @@ jobs: if: runner.os != 'Linux' run: mkdir build - - name: Use GitHub Artifacts for binaries - if: ${{ !inputs.use_release_artifacts && inputs.workflow_id != '' }} - run: echo "OTC_ARTIFACTS_SOURCE=github-artifacts" >> $GITHUB_ENV - # TODO: Go back to using Apple-Actions/import-codesign-certs once https://github.com/Apple-Actions/import-codesign-certs/pull/58 is merged - name: Import macOS Code-Signing Certificates if: ${{ runner.os == 'macOS' && env.MACOS_SIGNING_ENABLED == 'true' }} @@ -128,13 +113,28 @@ jobs: /usr/bin/security set-key-partition-list -S apple-tool:,apple: -k ${{ secrets.apple_developer_certificate_password }} signing_temp.keychain /usr/bin/security list-keychains -d user -s signing_temp.keychain login.keychain + - name: Fetch version artifact + if: runner.os == 'Linux' + uses: ./ci/github-actions/cmake + with: + args: -P version_artifact.cmake + gh_token: ${{ secrets.gh_artifacts_token }} + workflow_id: ${{ inputs.workflow_id }} + work_dir: . + + - name: Fetch version artifact + if: runner.os != 'Linux' + env: + GH_TOKEN: ${{ secrets.gh_artifacts_token }} + GH_WORKFLOW_ID: ${{ inputs.workflow_id }} + run: cmake -P version_artifact.cmake + - name: Build Makefile id: cmake-linux if: runner.os == 'Linux' uses: ./ci/github-actions/cmake with: - otc_version: ${{ inputs.otc_version }} - otc_sumo_version: ${{ inputs.otc_sumo_version }} + gh_token: ${{ secrets.gh_artifacts_token }} otc_build_number: ${{ inputs.otc_build_number }} target: ${{ inputs.cmake_target }} workflow_id: ${{ inputs.workflow_id }} @@ -143,11 +143,11 @@ jobs: id: cmake-other if: runner.os != 'Linux' env: - OTC_VERSION: ${{ inputs.otc_version }} - OTC_SUMO_VERSION: ${{ inputs.otc_sumo_version }} + GH_TOKEN: ${{ secrets.gh_artifacts_token }} + GH_WORKFLOW_ID: ${{ inputs.workflow_id }} OTC_BUILD_NUMBER: ${{ inputs.otc_build_number }} - TARGET: ${{ inputs.cmake_target }} PRODUCTBUILD_IDENTITY_NAME: ${{ secrets.productbuild_identity_name }} + TARGET: ${{ inputs.cmake_target }} working-directory: build run: cmake ../ @@ -162,62 +162,17 @@ jobs: run: > echo path="$PACKAGE_NAME" >> $GITHUB_OUTPUT - - name: Set simple otc-bin outputs - id: bin - if: ${{ !inputs.use_release_artifacts && inputs.workflow_id != '' }} - env: - OTC_BIN: >- - ${{ - steps.cmake-linux.outputs.otc-bin || - steps.cmake-other.outputs.otc-bin - }} - OTC_CONFIG_BIN: >- - ${{ - steps.cmake-linux.outputs.otc-config-bin || - steps.cmake-other.outputs.otc-config-bin - }} - run: > - echo otelcol_sumo_path="$OTC_BIN" >> $GITHUB_OUTPUT && - echo otelcol_config_path="$OTC_CONFIG_BIN" >> $GITHUB_OUTPUT - - # Download the artifacts required to build the package target. If - # inputs.workflow_id is empty then this will be skipped and CMake will - # attempt to fetch the artifacts from a GitHub Release matching - # otc_version and otc_sumo_version. - - name: Download otelcol-sumo artifact from sumologic-otel-collector - uses: dawidd6/action-download-artifact@v3.1.4 - if: ${{ !inputs.use_release_artifacts && inputs.workflow_id != '' }} - with: - github_token: ${{ secrets.gh_artifacts_token }} - repo: SumoLogic/sumologic-otel-collector - run_id: ${{ inputs.workflow_id }} - workflow: dev_builds.yml - workflow_conclusion: success - name: ${{ steps.bin.outputs.otelcol_sumo_path }} - path: ./build/gh-artifacts - if_no_artifact_found: fail - - - name: Download otelcol-config artifact from sumologic-otel-collector - uses: dawidd6/action-download-artifact@v3.1.4 - if: inputs.workflow_id != '' - with: - github_token: ${{ secrets.gh_artifacts_token }} - repo: SumoLogic/sumologic-otel-collector - run_id: ${{ inputs.workflow_id }} - workflow: dev_builds.yml - workflow_conclusion: success - name: ${{ steps.bin.outputs.otelcol_config_path }} - path: ./build/gh-artifacts - if_no_artifact_found: fail - - name: Build package if: runner.os == 'Linux' uses: ./ci/github-actions/make with: + gh_token: ${{ secrets.gh_artifacts_token }} target: package - name: Build package if: runner.os != 'Linux' + env: + GH_TOKEN: ${{ secrets.gh_artifacts_token }} working-directory: build run: make package @@ -233,7 +188,7 @@ jobs: uses: ./ci/github-actions/make with: target: publish-package - packagecloud-token: ${{ secrets.PACKAGECLOUD_TOKEN }} + packagecloud_token: ${{ secrets.PACKAGECLOUD_TOKEN }} test_package: runs-on: ${{ inputs.runs_on }} @@ -258,14 +213,11 @@ jobs: runs-on: ${{ inputs.runs_on }} name: Build (WiX) ${{ inputs.fips && 'FIPS' || '' }} if: inputs.build_tool == 'wix' - env: - PRODUCT_VERSION: ${{ inputs.otc_version }}.${{ inputs.otc_build_number }} steps: - name: Checkout uses: actions/checkout@v4 - name: Workflow URL for sumologic-otel-collector - if: ${{ !inputs.use_release_artifacts && inputs.workflow_id != '' }} run: | org="SumoLogic" repo="sumologic-otel-collector" @@ -278,54 +230,39 @@ jobs: - name: Add msbuild to PATH uses: microsoft/setup-msbuild@v2 - - name: Use GitHub Artifacts for binaries - if: ${{ !inputs.use_release_artifacts && inputs.workflow_id != '' }} - run: echo "OTC_ARTIFACTS_SOURCE=github-artifacts" >> $GITHUB_ENV + - name: Fetch version artifact + env: + GH_TOKEN: ${{ secrets.gh_artifacts_token }} + GH_WORKFLOW_ID: ${{ inputs.workflow_id }} + shell: cmd + run: cmake -P version_artifact.cmake + + - name: Determine version from binary + id: determine_version + working-directory: ./build/version_detection + run: > + version=$(./otelcol-sumo.exe --version | + sed -E -n 's/.* v([0-9]+\.[0-9]+\.[0-9]+)\-sumo.*/\1/p') && + echo product_version="${version}.${{ inputs.otc_build_number }}" >> $GITHUB_OUTPUT - name: Determine artifact names run: | echo "OTC_WORKFLOW_ARTIFACT_NAME=otelcol-sumo-${{ inputs.fips && 'fips-' || '' }}windows_${{ inputs.goarch }}.exe" >> $GITHUB_ENV - echo "OTC_RELEASE_ARTIFACT_NAME=otelcol-sumo-${{ inputs.otc_version }}-sumo-${{inputs.otc_sumo_version }}-${{ inputs.fips && 'fips-' || '' }}windows_${{ inputs.goarch }}.exe" >> $GITHUB_ENV echo "OTC_BUILD_INPUT_NAME=otelcol-sumo-windows_${{ inputs.goarch }}.exe" >> $GITHUB_ENV - # Download the artifacts required to build the package target. If - # inputs.workflow_id is empty then this will be skipped and an attempt - # will be made to fetch the artifacts from a GitHub Release matching - # otc_version and otc_sumo_version. - - name: Download artifact from workflow + # Download the artifacts required to build the package target. + - name: Download otelcol-sumo artifact from workflow uses: dawidd6/action-download-artifact@v3.1.4 - if: ${{ !inputs.use_release_artifacts && inputs.workflow_id != '' }} with: github_token: ${{ secrets.gh_artifacts_token }} repo: SumoLogic/sumologic-otel-collector run_id: ${{ inputs.workflow_id }} - workflow: dev_builds.yml workflow_conclusion: success name: ${{ env.OTC_WORKFLOW_ARTIFACT_NAME }} path: ./build/artifacts if_no_artifact_found: fail - # Download the artifacts, required to build the package target, from a - # GitHub Release. - - name: Download artifact from GitHub Release - uses: robinraju/release-downloader@v1.10 - if: ${{ inputs.use_release_artifacts && inputs.workflow_id == '' }} - with: - repository: SumoLogic/sumologic-otel-collector - tag: v${{ inputs.otc_version }}-sumo-${{ inputs.otc_sumo_version }} - fileName: ${{ env.OTC_RELEASE_ARTIFACT_NAME }} - out-file-path: build/artifacts - - - name: Rename GitHub Release artifact - if: ${{ inputs.use_release_artifacts && inputs.workflow_id == '' }} - working-directory: ./build/artifacts - env: - MV_FROM: ${{ env.OTC_RELEASE_ARTIFACT_NAME }} - MV_TO: ${{ env.OTC_BUILD_INPUT_NAME }} - run: mv -n "$MV_FROM" "$MV_TO" - - name: Rename GitHub Workflow artifact - if: ${{ !inputs.use_release_artifacts && inputs.workflow_id != '' }} working-directory: ./build/artifacts env: MV_FROM: ${{ env.OTC_WORKFLOW_ARTIFACT_NAME }} @@ -337,6 +274,7 @@ jobs: working-directory: ./msi/wix env: PLATFORM: ${{ inputs.package_arch }} + PRODUCT_VERSION: ${{ steps.determine_version.outputs.product_version }} run: | msbuild.exe -p:Configuration=Release \ -p:Platform=$PLATFORM \ diff --git a/.github/workflows/build_packages.yml b/.github/workflows/build_packages.yml index a7f313d0..fa562fbd 100644 --- a/.github/workflows/build_packages.yml +++ b/.github/workflows/build_packages.yml @@ -4,37 +4,13 @@ # upgrades from one build to the next. name: 'Build packages' -# Sets the name of the CI run based on whether the run was triggered by a push -# or remotely with or without workflow_run_id or use_release_artifacts set. The -# name used for push events is the full commit message as I have not been able -# to find a way to only show the commit title (first 72 characters of commit -# message) - Justin K. +# Sets the name of the CI run based on whether the run was triggered with or +# without a workflow_id set. run-name: > ${{ - (inputs.use_release_artifacts && - inputs.otc_version != '' && - inputs.otc_sumo_version != '') && - format('Build for GitHub Release: {0}-sumo-{1}, Version: {0}-sumo-{1}', inputs.otc_version, inputs.otc_sumo_version) + inputs.workflow_id == '' && format('Build for Remote Workflow: latest-main') || - (inputs.use_release_artifacts && - (inputs.otc_version == '' || - inputs.otc_sumo_version == '')) && - 'Build for GitHub Release: latest' - || - (inputs.workflow_id != '' && - inputs.otc_version != '' && - inputs.otc_sumo_version != '') && - format('Build for Remote Workflow: {0}, Version: {1}-sumo-{2}', inputs.workflow_id, inputs.otc_version, inputs.otc_sumo_version) - || - (inputs.workflow_id == '' && - inputs.otc_version != '' && - inputs.otc_sumo_version != '') && - format('Build for Remote Workflow: latest-main, Version: {0}-sumo-{1}', inputs.otc_version, inputs.otc_sumo_version) - || - inputs.workflow_id != '' && - format('Build for Remote Workflow: {0}, Version: unknown', inputs.workflow_id) - || - github.event.head_commit.message + inputs.workflow_id != '' && format('Build for Remote Workflow: {0}', inputs.workflow_id) }} on: @@ -52,29 +28,11 @@ on: are being built for. required: false type: string - otc_version: - description: | - Version of otelcol-sumo to package in A.B.C format. - required: false - type: string - otc_sumo_version: - description: | - Sumo version of otelcol-sumo to package. E.g. the X in A.B.C-sumo-X. - required: false - type: string release: description: Publish draft release type: boolean required: false default: false - use_release_artifacts: - description: | - Fetch artifacts from a GitHub Release instead of the artifacts from a - GitHub CI run. Both otc_version and otc_sumo_version are required if - this is set to true. - type: boolean - required: false - default: false jobs: determine_workflow: @@ -107,65 +65,6 @@ jobs: - name: Output Remote Workflow URL run: echo ::notice title=Remote Workflow URL::https://github.com/SumoLogic/sumologic-otel-collector/actions/runs/${{ steps.workflow.outputs.id }} - # Determines the latest version which will be used to fetch artifacts from a - # GitHub Release and as the version of the packages being built. This is - # skipped if the otc_version and otc_sumo_version inputs have been set. - determine_version: - runs-on: ubuntu-latest - name: Determine version - outputs: - otc_version: ${{ steps.versions.outputs.otc_version }} - otc_sumo_version: ${{ steps.versions.outputs.otc_sumo_version }} - steps: - - name: Checkout - uses: actions/checkout@v4 - - # Determine the latest release version if either otc_version or - # otc_sumo_version are empty. - - name: Determine latest release version - id: release - if: > - inputs.otc_version == '' && - inputs.otc_sumo_version == '' - env: - GH_TOKEN: ${{ github.token }} - run: | - TAG_NAME=$(gh release view -R SumoLogic/sumologic-otel-collector --jq ".tagName" --json "tagName") - echo "tag_name=$TAG_NAME" >> "$GITHUB_OUTPUT" - - - name: Determine version core from release - id: version-core - if: > - inputs.otc_version == '' && - inputs.otc_sumo_version == '' - env: - VERSION_TAG: ${{ steps.release.outputs.tag_name }} - run: > - ./ci/get_version.sh otc_version > /tmp/otc_version && - cat /tmp/otc_version && - echo "version=$(cat /tmp/otc_version)" >> $GITHUB_OUTPUT - - - name: Determine sumo version from release - id: sumo-version - if: > - inputs.otc_version == '' && - inputs.otc_sumo_version == '' - env: - VERSION_TAG: ${{ steps.release.outputs.tag_name }} - run: > - ./ci/get_version.sh otc_sumo_version > /tmp/otc_sumo_version && - cat /tmp/otc_sumo_version && - echo "version=$(cat /tmp/otc_sumo_version)" >> $GITHUB_OUTPUT - - - name: Set output versions - id: versions - run: | - echo "otc_version=${{ inputs.otc_version || steps.version-core.outputs.version }}" >> $GITHUB_OUTPUT - echo "otc_sumo_version=${{ inputs.otc_sumo_version || steps.sumo-version.outputs.version }}" >> $GITHUB_OUTPUT - - - name: Output App Version - run: echo ::notice title=App Version::${{ steps.versions.outputs.otc_version }}-sumo-${{ steps.versions.outputs.otc_sumo_version }} - # Builds a package for each target in the matrix. The target must be an # existing file name (without extension) in the targets directory when # build_tool is cmake. @@ -174,14 +73,10 @@ jobs: uses: ./.github/workflows/_reusable_build_package.yml needs: - determine_workflow - - determine_version with: - otc_version: ${{ needs.determine_version.outputs.otc_version }} - otc_sumo_version: ${{ needs.determine_version.outputs.otc_sumo_version }} otc_build_number: ${{ github.run_number }} cmake_target: ${{ matrix.target }} workflow_id: ${{ needs.determine_workflow.outputs.workflow_id }} - use_release_artifacts: ${{ inputs.use_release_artifacts || false }} runs_on: ${{ matrix.runs_on }} goarch: ${{ matrix.goarch }} package_arch: ${{ matrix.package_arch }} @@ -271,13 +166,12 @@ jobs: runs-on: ubuntu-latest needs: - build_packages - - determine_version permissions: contents: write if: inputs.release env: - OTC_APP_VERSION: v${{ needs.determine_version.outputs.otc_version }}-sumo-${{ needs.determine_version.outputs.otc_sumo_version }} - RELEASE_TAG_NAME: v${{ needs.determine_version.outputs.otc_version }}-${{ github.run_number }} + OTC_APP_VERSION: v${{ needs.build_packages.outputs.otc_version }}-sumo-${{ needs.build_packages.outputs.otc_sumo_version }} + RELEASE_TAG_NAME: v${{ needs.build_packages.outputs.otc_version }}-${{ github.run_number }} steps: - name: Download all packages stored as artifacts uses: actions/download-artifact@v4 @@ -308,26 +202,3 @@ jobs: ref: 'refs/tags/${{ env.RELEASE_TAG_NAME }}', sha: tagRequest.data.sha }) - - - uses: ncipollo/release-action@v1 - with: - name: v${{ needs.determine_version.outputs.otc_version }}-${{ github.run_number }} - commit: ${{ github.sha }} - tag: ${{ env.RELEASE_TAG_NAME }} - - draft: true - generateReleaseNotes: true - prerelease: false - - allowUpdates: true - omitBodyDuringUpdate: true - omitNameDuringUpdate: true - - artifacts: "artifacts/*/*" - artifactErrorsFailBuild: true - replacesArtifacts: true - - body: | - This release packages [${{ env.OTC_APP_VERSION }}](https://github.com/SumoLogic/sumologic-otel-collector/releases/tag/${{ env.OTC_APP_VERSION }}). - - The changelog below is for the package itself, rather than the Sumo Logic Distribution for OpenTelemetry Collector. diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..08946603 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,75 @@ +name: 'Publish release' + +run-name: > + ${{ format('Publish Release for Workflow: {0}', inputs.workflow_id) }} + +on: + workflow_dispatch: + inputs: + workflow_id: + description: | + Workflow Run ID from this repository to use as a release. + +jobs: + publish_release: + name: Publish Release + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Not implemented + run: > + echo "This job is not functional yet" && exit 1 + + - name: Download all packages stored as artifacts + uses: actions/download-artifact@v4 + with: + path: artifacts/ + + # Create the release tag separately to add a message to it + # NOTE: As per the github API, we need to create the tag object first, and then the git reference + - name: Create release tag + uses: actions/github-script@v7 + with: + script: | + const tagRequest = await github.rest.git.createTag({ + owner: context.repo.owner, + repo: context.repo.repo, + tag: '${{ env.RELEASE_TAG_NAME }}', + message: `App Version: ${{ env.OTC_APP_VERSION }}`, + object: context.sha, + type: 'commit', + tagger: { + name: "${{ github.actor }}", + email: "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com", + }, + }) + github.rest.git.createRef({ + owner: context.repo.owner, + repo: context.repo.repo, + ref: 'refs/tags/${{ env.RELEASE_TAG_NAME }}', + sha: tagRequest.data.sha + }) + + - uses: ncipollo/release-action@v1 + with: + name: v${{ needs.build_packages.outputs.otc_version }}-${{ github.run_number }} + commit: ${{ github.sha }} + tag: ${{ env.RELEASE_TAG_NAME }} + + draft: true + generateReleaseNotes: true + prerelease: false + + allowUpdates: true + omitBodyDuringUpdate: true + omitNameDuringUpdate: true + + artifacts: "artifacts/*/*" + artifactErrorsFailBuild: true + replacesArtifacts: true + + body: | + This release packages [${{ env.OTC_APP_VERSION }}](https://github.com/SumoLogic/sumologic-otel-collector/releases/tag/${{ env.OTC_APP_VERSION }}). + + The changelog below is for the package itself, rather than the Sumo Logic Distribution for OpenTelemetry Collector. diff --git a/CMakeLists.txt b/CMakeLists.txt index bee55ed6..b4f8918e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,26 @@ # Require CMake >= 3.24.1 cmake_minimum_required(VERSION 3.24.1 FATAL_ERROR) +# Required environment variables +if(NOT DEFINED ENV{GH_TOKEN}) + message(FATAL_ERROR "GH_TOKEN environment variable must be set") +endif() + +if(NOT DEFINED ENV{GH_WORKFLOW_ID}) + message(FATAL_ERROR "GH_WORKFLOW_ID environment variable must be set") +endif() + +set(GH_TOKEN "$ENV{GH_TOKEN}") +set(GH_WORKFLOW_ID "$ENV{GH_WORKFLOW_ID}") + # Required and optional programs. Attempts to find required and optional # programs used to build the packages. find_program(PACKAGECLOUD_PROGRAM packagecloud REQUIRED) +find_program(GH_PROGRAM gh REQUIRED) + +# Include utils +set(UTILS_DIR "${CMAKE_SOURCE_DIR}/utils") +include("${CMAKE_SOURCE_DIR}/utils.cmake") # Set version information include("${CMAKE_SOURCE_DIR}/version.cmake") @@ -16,17 +33,13 @@ include(CPackComponent) set(ASSETS_DIR "${CMAKE_SOURCE_DIR}/assets") set(COMPONENTS_DIR "${CMAKE_SOURCE_DIR}/components") set(DISTRIBUTIONS_DIR "${CMAKE_SOURCE_DIR}/distributions") -set(EXTERNAL_PROJECTS_DIR "${CMAKE_SOURCE_DIR}/external_projects") set(SETTINGS_DIR "${CMAKE_SOURCE_DIR}/settings") set(TARGETS_DIR "${CMAKE_SOURCE_DIR}/targets") set(TEMPLATES_DIR "${CMAKE_SOURCE_DIR}/templates") set(TEMPLATES_OUTPUT_DIR "${CMAKE_BINARY_DIR}/templates_output") -set(UTILS_DIR "${CMAKE_SOURCE_DIR}/utils") # Include CMake files -include("${CMAKE_SOURCE_DIR}/utils.cmake") include("${CMAKE_SOURCE_DIR}/components.cmake") -include("${CMAKE_SOURCE_DIR}/external_projects.cmake") include("${CMAKE_SOURCE_DIR}/templates.cmake") include("${CMAKE_SOURCE_DIR}/packages.cmake") include("${CMAKE_SOURCE_DIR}/distributions.cmake") diff --git a/Makefile b/Makefile index 5dce5e65..f959c4c3 100644 --- a/Makefile +++ b/Makefile @@ -2,30 +2,55 @@ mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) mkfile_dir ?= $(patsubst %/,%,$(dir $(mkfile_path))) build_dir ?= $(mkfile_dir)/build -OTC_ARTIFACTS_SOURCE ?= github-artifacts - -ifeq ($(strip $(PACKAGECLOUD_TOKEN)),) -$(error "PACKAGECLOUD_TOKEN must be set") -endif +require-%: + @: $(if ${${*}},,$(error Required environment variable was not found: $*)) + @echo 'Required environment variable was found: $*' .PHONY: docker-image docker-image: docker buildx bake --load -.PHONY: build -build: docker-image -build: +.PHONY: version-artifact +version-artifact: require-GH_TOKEN +version-artifact: require-GH_WORKFLOW_ID +version-artifact: docker-image +version-artifact: + docker run \ + -e GH_TOKEN="$(GH_TOKEN)" \ + -e GH_WORKFLOW_ID="$(GH_WORKFLOW_ID)" \ + -e WORK_DIR="/src" \ + -v "$(mkfile_dir):/src" \ + -v "$(build_dir):/build" \ + otelcol-sumo/cmake \ + cmake -P version_artifact.cmake + +.PHONY: generate +generate: require-TARGET +generate: require-OTC_BUILD_NUMBER +generate: require-GH_TOKEN +generate: require-GH_WORKFLOW_ID +generate: version-artifact +generate: docker run \ -e TARGET="$(TARGET)" \ - -e OTC_VERSION="$(OTC_VERSION)" \ - -e OTC_SUMO_VERSION="$(OTC_SUMO_VERSION)" \ -e OTC_BUILD_NUMBER="$(OTC_BUILD_NUMBER)" \ - -e OTC_ARTIFACTS_SOURCE="$(OTC_ARTIFACTS_SOURCE)" \ + -e GH_TOKEN="$(GH_TOKEN)" \ + -e GH_WORKFLOW_ID="$(GH_WORKFLOW_ID)" \ -v "$(mkfile_dir):/src" \ -v "$(build_dir):/build" \ otelcol-sumo/cmake \ cmake /src +.PHONY: build +build: generate +build: + docker run \ + -e GH_TOKEN="$(GH_TOKEN)" \ + -v "$(mkfile_dir):/src" \ + -v "$(build_dir):/build" \ + otelcol-sumo/cmake \ + make + .PHONY: package package: build package: @@ -36,12 +61,13 @@ package: make package .PHONY: publish-package +publish-package: require-PACKAGECLOUD_TOKEN publish-package: package publish-package: docker run \ + -e PACKAGECLOUD_TOKEN="$(PACKAGECLOUD_TOKEN)" \ -v "$(mkfile_dir):/src" \ -v "$(build_dir):/build" \ - -e PACKAGECLOUD_TOKEN="$(PACKAGECLOUD_TOKEN)" \ otelcol-sumo/cmake \ make publish-package diff --git a/README.md b/README.md index b4bc4fce..701107f6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ + # sumologic-otel-collector-packaging ## Building diff --git a/ci/github-actions/cmake/action.yml b/ci/github-actions/cmake/action.yml index 56e71f5d..1b8d628d 100644 --- a/ci/github-actions/cmake/action.yml +++ b/ci/github-actions/cmake/action.yml @@ -1,27 +1,35 @@ name: 'Run CMake' inputs: - otc_version: - required: true - type: string - otc_sumo_version: - required: true + args: + required: false type: string + default: '../' otc_build_number: required: false type: string target: required: true type: string + work_dir: + required: false + type: string workflow_id: required: false type: string + gh_token: + required: false + type: string runs: using: 'docker' image: '../../../Dockerfile' env: + GH_TOKEN: ${{ inputs.gh_token }} + GH_WORKFLOW_ID: ${{ inputs.workflow_id }} + OTC_BUILD_NUMBER: ${{ inputs.otc_build_number }} TARGET: ${{ inputs.target }} + WORK_DIR: ${{ inputs.work_dir }} args: - cmake - - ../ + - ${{ inputs.args }} diff --git a/ci/github-actions/make/action.yml b/ci/github-actions/make/action.yml index 85391fd1..dd330327 100644 --- a/ci/github-actions/make/action.yml +++ b/ci/github-actions/make/action.yml @@ -4,7 +4,10 @@ inputs: target: required: true type: string - packagecloud-token: + gh_token: + required: false + type: string + packagecloud_token: required: false type: string @@ -12,7 +15,8 @@ runs: using: 'docker' image: '../../../Dockerfile' env: - PACKAGECLOUD_TOKEN: ${{ inputs.packagecloud-token }} + GH_TOKEN: ${{ inputs.gh_token }} + PACKAGECLOUD_TOKEN: ${{ inputs.packagecloud_token }} args: - make - ${{ inputs.target }} diff --git a/components/otelcol-sumo.cmake b/components/otelcol-sumo.cmake index 147949fe..53d8359a 100644 --- a/components/otelcol-sumo.cmake +++ b/components/otelcol-sumo.cmake @@ -189,18 +189,18 @@ endmacro() macro(install_otc_binary) require_variables( "OTC_BIN_DIR" - "OTC_BINARY" - "SOURCE_OTC_BINARY_PATH" + "OTC_BIN" + "REMOTE_OTC_BIN_PATH" ) install( - FILES "${SOURCE_OTC_BINARY_PATH}" + FILES "${REMOTE_OTC_BIN_PATH}" DESTINATION "${OTC_BIN_DIR}" PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE - RENAME "${OTC_BINARY}" + RENAME "${OTC_BIN}" COMPONENT otelcol-sumo ) endmacro() @@ -209,18 +209,18 @@ endmacro() macro(install_otc_config_binary) require_variables( "OTC_BIN_DIR" - "OTC_CONFIG_BINARY" - "SOURCE_OTC_CONFIG_BINARY_PATH" + "OTC_CONFIG_BIN" + "REMOTE_OTC_CONFIG_BIN_PATH" ) install( - FILES "${SOURCE_OTC_CONFIG_BINARY_PATH}" + FILES "${REMOTE_OTC_CONFIG_BIN_PATH}" DESTINATION "${OTC_BIN_DIR}" PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE - RENAME "${OTC_CONFIG_BINARY}" + RENAME "${OTC_CONFIG_BIN}" COMPONENT otelcol-sumo ) endmacro() diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 838b1dea..47cb0877 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -1,27 +1,18 @@ #!/bin/sh -l set -e -if [ "${INPUT_OTC_VERSION}" != "" ]; then - OTC_VERSION="$(echo "$INPUT_OTC_VERSION" | xargs)" - export OTC_VERSION -fi - -if [ "${INPUT_OTC_SUMO_VERSION}" != "" ]; then - OTC_SUMO_VERSION="$(echo "$INPUT_OTC_SUMO_VERSION" | xargs)" - export OTC_SUMO_VERSION -fi - if [ "${INPUT_OTC_BUILD_NUMBER}" != "" ]; then OTC_BUILD_NUMBER="$(echo "$INPUT_OTC_BUILD_NUMBER" | xargs)" export OTC_BUILD_NUMBER fi -if [ "${INPUT_WORKFLOW_ID}" != "" ]; then - export OTC_ARTIFACTS_SOURCE="github-artifacts" -fi - mkdir -p build -cd build || exit + +if [ -n "${WORK_DIR}" ]; then + cd "${WORK_DIR}" || exit +else + cd build || exit +fi # shellcheck disable=SC2068 $@ diff --git a/docker/install-deps.sh b/docker/install-deps.sh index 23ff7f82..c03f7b21 100755 --- a/docker/install-deps.sh +++ b/docker/install-deps.sh @@ -4,6 +4,7 @@ set -euxo pipefail targetarch="$1" +GITHUB_CLI_VERSION="2.60.1" PACKAGECLOUD_GO_VERSION="0.1.5" # Convert between Docker CPU architecture names and other names such as Go's @@ -16,6 +17,25 @@ else GOARCH="amd64" fi +function install_github_cli() { + base_url="https://github.com/cli/cli/releases/download" + version="${GITHUB_CLI_VERSION}" + file="gh_${version}_linux_${GOARCH}.tar.gz" + url="${base_url}/v${version}/${file}" + + # Download GitHub CLI + curl -Lo /tmp/github-cli.tar.gz "${url}" + + # Verify that the file is gzip compressed data to prevent cases where + # outages, site changes, etc. result in the downloaded file being HTML text. + file /tmp/github-cli.tar.gz | grep "gzip compressed data" + + # Install GitHub CLI + mkdir /tmp/github-cli + tar -C /tmp/github-cli -zxf /tmp/github-cli.tar.gz --strip-components=1 + mv /tmp/github-cli/bin/gh /usr/local/bin/gh +} + function install_packagecloud_go() { base_url="https://github.com/amdprophet/packagecloud-go/releases/download" version="${PACKAGECLOUD_GO_VERSION}" @@ -34,4 +54,5 @@ function install_packagecloud_go() { mv /tmp/packagecloud /usr/local/bin/packagecloud } +install_github_cli install_packagecloud_go diff --git a/external_projects.cmake b/external_projects.cmake deleted file mode 100644 index 7c4604f8..00000000 --- a/external_projects.cmake +++ /dev/null @@ -1 +0,0 @@ -include("${EXTERNAL_PROJECTS_DIR}/otelcol_sumo.cmake") diff --git a/external_projects/otelcol_sumo.cmake b/external_projects/otelcol_sumo.cmake deleted file mode 100644 index 7771e719..00000000 --- a/external_projects/otelcol_sumo.cmake +++ /dev/null @@ -1,25 +0,0 @@ -# An external project to download a remote artifact from a GitHub Release. -# src: name of the remote artifact -# dest: destination file name which src will be renamed to -# tag: name of tag for the GitHub Release used to download artifacts from -function(create_otelcol_sumo_target src dest tag download_dir) - set(base_url "https://github.com/SumoLogic/sumologic-otel-collector") - set(file_url "${base_url}/releases/download/${tag}/${src}") - - ExternalProject_Add("${src}" - URL "${file_url}" - DOWNLOAD_DIR "${download_dir}" - DOWNLOAD_NAME "${dest}" - DOWNLOAD_NO_EXTRACT true - # We only care about downloading; disable all other commands - CONFIGURE_COMMAND "" - UPDATE_COMMAND "" - PATCH_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - TEST_COMMAND "" - ) - - message(STATUS "The ${src} artifact will be fetched from:") - message(STATUS "\t${file_url}") -endfunction() diff --git a/packages.cmake b/packages.cmake index 65f091ad..c0bdd8a2 100644 --- a/packages.cmake +++ b/packages.cmake @@ -6,18 +6,41 @@ function(append_to_publish_targets) set_property(GLOBAL PROPERTY _all_publish_targets "${tmp}") endfunction() +function(download_github_artifact _slug _workflow_id _remote_filename _target) + if(TARGET ${_target}) + message(STATUS "Target already exists: ${_remote_filename}") + else() + message(STATUS "Creating target: ${_remote_filename}") + set(_cmd_str "${GH_PROGRAM} run download -R ${_gh_slug} ${_gh_workflow} -n ${_remote_filename}") + separate_arguments(_cmd UNIX_COMMAND "${_cmd_str}") + add_custom_command( + OUTPUT ${_remote_filename} + COMMAND ${_cmd} + COMMENT "Downloading ${_remote_filename} from GitHub" + VERBATIM + ) + add_custom_target(${_target} ALL DEPENDS ${_remote_filename}) + endif() +endfunction() + # Build CPackConfig, create a target for building the package and add the target # to the list of all package targets macro(build_cpack_config) require_variables( "CPACK_PACKAGE_FILE_NAME" + "GH_WORKFLOW_ID" "PACKAGE_FILE_EXTENSION" - "OTC_BINARY" - "OTC_CONFIG_BINARY" - "SOURCE_OTC_BINARY" - "SOURCE_OTC_CONFIG_BINARY" + "OTC_BIN" + "OTC_CONFIG_BIN" + "REMOTE_OTC_BIN" ) + set(_gh_slug "SumoLogic/sumologic-otel-collector") + set(_gh_workflow "${GH_WORKFLOW_ID}") + + download_github_artifact(${_gh_slug} ${_gh_workflow} ${REMOTE_OTC_BIN} ${OTC_BIN}) + download_github_artifact(${_gh_slug} ${_gh_workflow} ${REMOTE_OTC_CONFIG_BIN} ${OTC_CONFIG_BIN}) + # Set a GitHub output with a name matching ${target_name}-pkg and a value # equal to the filename of the package that will be built. This provides # GitHub Actions with the package filename so that it can be uploaded as a @@ -25,96 +48,6 @@ macro(build_cpack_config) set(package_file_name "${CPACK_PACKAGE_FILE_NAME}.${PACKAGE_FILE_EXTENSION}") set_github_output("package_name" "${package_file_name}") - # Set a GitHub output with a name matching ${target_name}-otc-bin and a value - # equal to the name of the otelcol-sumo binary artifact that we want GitHub - # Actions to fetch from the sumologic-otel-collector's GitHub Workflow - # artifacts. This is only used when the OTC_ARTIFACTS_SOURCE environment - # variable is set to "github-artifacts" which disables fetching artifacts from - # a GitHub Release. - if(DEFINED ENV{OTC_ARTIFACTS_SOURCE}) - if($ENV{OTC_ARTIFACTS_SOURCE} STREQUAL "github-artifacts") - require_variables( - "GH_ARTIFACTS_DIR" - "GH_OUTPUT_OTC_BIN" - "GH_OUTPUT_OTC_CONFIG_BIN" - ) - - file(MAKE_DIRECTORY "${GH_ARTIFACTS_DIR}") - file(CHMOD_RECURSE "${GH_ARTIFACTS_DIR}" - DIRECTORY_PERMISSIONS - OWNER_WRITE OWNER_READ OWNER_EXECUTE - GROUP_WRITE GROUP_READ GROUP_EXECUTE - WORLD_WRITE WORLD_READ WORLD_EXECUTE - ) - set_github_output("otc-bin" "${GH_OUTPUT_OTC_BIN}") - set_github_output("otc-config-bin" "${GH_OUTPUT_OTC_CONFIG_BIN}") - else() - message(FATAL_ERROR - "Unsupported value for OTC_ARTIFACTS_SOURCE environment variable: $ENV{OTC_ARTIFACTS_SOURCE}" - ) - endif() - - # Create a target, if the target does not yet exist, for copying the - # otelcol-sumo binary from the gh-actions directory to the artifacts - # directory - if(TARGET "${SOURCE_OTC_BINARY}") - message(STATUS "Target already exists: ${SOURCE_OTC_BINARY}") - else() - message(STATUS "Creating target: ${SOURCE_OTC_BINARY}") - file(MAKE_DIRECTORY "${SOURCE_OTC_BINARY_DIR}") - add_custom_target("${SOURCE_OTC_BINARY}" - ALL - COMMAND ${CMAKE_COMMAND} -E copy ${GH_ARTIFACT_OTC_BINARY_PATH} ${SOURCE_OTC_BINARY_PATH} - VERBATIM - ) - endif() - - # Create a target, if the target does not yet exist, for copying the - # otelcol-config binary from the gh-actions directory to the artifacts - # directory - if(TARGET "${SOURCE_OTC_CONFIG_BINARY}") - message(STATUS "Target already exists: ${SOURCE_OTC_CONFIG_BINARY}") - else() - message(STATUS "Creating target: ${SOURCE_OTC_CONFIG_BINARY}") - file(MAKE_DIRECTORY "${SOURCE_OTC_CONFIG_BINARY_DIR}") - add_custom_target("${SOURCE_OTC_CONFIG_BINARY}" - ALL - COMMAND ${CMAKE_COMMAND} -E copy ${GH_ARTIFACT_OTC_CONFIG_BINARY_PATH} ${SOURCE_OTC_CONFIG_BINARY_PATH} - VERBATIM - ) - endif() - else() - # Create a target for downloading the otelcol-sumo binary from GitHub - # Releases if the target does not exist yet - if(TARGET "${SOURCE_OTC_BINARY}") - message(STATUS "Target already exists: ${SOURCE_OTC_BINARY}") - else() - message(STATUS "Creating target: ${SOURCE_OTC_BINARY}") - require_variables("OTC_GIT_TAG") - create_otelcol_sumo_target( - "${SOURCE_OTC_BINARY}" - "${OTC_BINARY}" - "${OTC_GIT_TAG}" - "${SOURCE_OTC_BINARY_DIR}" - ) - endif() - - # Create a target for downloading the otelcol-config binary from GitHub - # Releases if the target does not exist yet - if(TARGET "${SOURCE_OTC_CONFIG_BINARY}") - message(STATUS "Target already exists: ${SOURCE_OTC_CONFIG_BINARY}") - else() - message(STATUS "Creating target: ${SOURCE_OTC_CONFIG_BINARY}") - require_variables("OTC_GIT_TAG") - create_otelcol_sumo_target( - "${SOURCE_OTC_CONFIG_BINARY}" - "${OTC_CONFIG_BINARY}" - "${OTC_GIT_TAG}" - "${SOURCE_OTC_CONFIG_BINARY_DIR}" - ) - endif() - endif() - set(_package_file "${CPACK_PACKAGE_FILE_NAME}.${PACKAGE_FILE_EXTENSION}") set(_package_output "${CMAKE_BINARY_DIR}/${_package_file}") @@ -142,7 +75,7 @@ endmacro() # repository for a specific distribution. function(create_packagecloud_publish_target _pc_user _pc_repo _pc_distro _pkg_name) set(_pc_output "${_pkg_name}-${_pc_repo}/${_pc_distro}") - separate_arguments(_packagecloud_push_cmd UNIX_COMMAND "packagecloud push --skip-exists ${_pc_user}/${_pc_repo}/${_pc_distro} ${_pkg_name}") + separate_arguments(_packagecloud_push_cmd UNIX_COMMAND "${PACKAGECLOUD_PROGRAM} push --skip-exists ${_pc_user}/${_pc_repo}/${_pc_distro} ${_pkg_name}") add_custom_command(OUTPUT ${_pc_output} COMMAND ${_packagecloud_push_cmd} DEPENDS ${_pkg_name} diff --git a/settings/common.cmake b/settings/common.cmake index 632d2bab..6619f007 100644 --- a/settings/common.cmake +++ b/settings/common.cmake @@ -9,5 +9,5 @@ macro(set_common_settings) #set(CPACK_COMPONENTS_GROUPING ALL_COMPONENTS_IN_ONE) set(ARTIFACTS_DIR "${CMAKE_BINARY_DIR}/artifacts") - set(GH_ARTIFACTS_DIR "${CMAKE_BINARY_DIR}/gh-artifacts") + set(ACL_LOG_FILE_PATHS "/var/log") endmacro() diff --git a/settings/otc.cmake b/settings/otc.cmake index 348cac56..7dd5b6a6 100644 --- a/settings/otc.cmake +++ b/settings/otc.cmake @@ -7,11 +7,6 @@ macro(set_otc_settings) "package_arch" ) - # OTC_GIT_TAG is used to determine which GitHub Release to fetch artifacts - # from. If ENV{OTC_ARTIFACTS_SOURCE} is set as then OTC_GIT_TAG will be - # ignored and the artifacts will be fetched from another source. - set(OTC_GIT_TAG "v${OTC_VERSION}-sumo-${OTC_SUMO_VERSION}") - ## # Destination file & directory names & paths # @@ -20,8 +15,8 @@ macro(set_otc_settings) ## # File names - set(OTC_BINARY "otelcol-sumo") - set(OTC_CONFIG_BINARY "otelcol-config") + set(OTC_BIN "otelcol-sumo") + set(OTC_CONFIG_BIN "otelcol-config") set(OTC_SUMOLOGIC_CONFIG "sumologic.yaml") set(OTC_SYSTEMD_CONFIG "otelcol-sumo.service") @@ -38,42 +33,29 @@ macro(set_otc_settings) set(OTC_LOG_DIR "var/log/otelcol-sumo") # File paths - set(OTC_BIN_PATH "${OTC_BIN_DIR}/${OTC_BINARY}") + set(OTC_BIN_PATH "${OTC_BIN_DIR}/${OTC_BIN}") set(OTC_SUMOLOGIC_CONFIG_PATH "${OTC_CONFIG_DIR}/${OTC_SUMOLOGIC_CONFIG}") ## - # Source file & directory names & paths + # Local & remote file & directory names & paths # # Specifies the names & paths of local & remote files required to build the # packages. ## - # File names - set(SOURCE_OTC_BINARY "otelcol-sumo-${OTC_VERSION}-sumo-${OTC_SUMO_VERSION}") - set(SOURCE_OTC_CONFIG_BINARY "otelcol-config-${OTC_VERSION}-sumo-${OTC_SUMO_VERSION}") - set(GH_OUTPUT_OTC_BIN "otelcol-sumo") - set(GH_OUTPUT_OTC_CONFIG_BIN "otelcol-config") + # Remote file names + set(REMOTE_OTC_BIN "otelcol-sumo") + set(REMOTE_OTC_CONFIG_BIN "otelcol-config") if(fips) - set(SOURCE_OTC_BINARY "${SOURCE_OTC_BINARY}-fips") - set(SOURCE_OTC_CONFIG_BINARY "${SOURCE_OTC_CONFIG_BINARY}-fips") - set(GH_OUTPUT_OTC_BIN "${GH_OUTPUT_OTC_BIN}-fips") - set(GH_OUTPUT_OTC_CONFIG_BIN "${GH_OUTPUT_OTC_CONFIG_BIN}-fips") + set(REMOTE_OTC_BIN "${REMOTE_OTC_BIN}-fips") + set(REMOTE_OTC_CONFIG_BIN "${REMOTE_OTC_CONFIG_BIN}-fips") endif() - set(SOURCE_OTC_BINARY "${SOURCE_OTC_BINARY}-${goos}_${goarch}") - set(SOURCE_OTC_CONFIG_BINARY "${SOURCE_OTC_CONFIG_BINARY}-${goos}_${goarch}") - set(GH_OUTPUT_OTC_BIN "${GH_OUTPUT_OTC_BIN}-${goos}_${goarch}") - set(GH_OUTPUT_OTC_CONFIG_BIN "${GH_OUTPUT_OTC_CONFIG_BIN}-${goos}_${goarch}") + set(REMOTE_OTC_BIN "${REMOTE_OTC_BIN}-${goos}_${goarch}") + set(REMOTE_OTC_CONFIG_BIN "${REMOTE_OTC_CONFIG_BIN}-${goos}_${goarch}") - # Directories - set(SOURCE_OTC_BINARY_DIR "${ARTIFACTS_DIR}/${SOURCE_OTC_BINARY}") - set(SOURCE_OTC_CONFIG_BINARY_DIR "${ARTIFACTS_DIR}/${SOURCE_OTC_CONFIG_BINARY}") - - # File paths - set(SOURCE_OTC_BINARY_PATH "${SOURCE_OTC_BINARY_DIR}/${OTC_BINARY}") - set(SOURCE_OTC_CONFIG_BINARY_PATH "${SOURCE_OTC_CONFIG_BINARY_DIR}/${OTC_CONFIG_BINARY}") - set(GH_ARTIFACT_OTC_BINARY_PATH "${GH_ARTIFACTS_DIR}/${GH_OUTPUT_OTC_BIN}") - set(GH_ARTIFACT_OTC_CONFIG_BINARY_PATH "${GH_ARTIFACTS_DIR}/${GH_OUTPUT_OTC_CONFIG_BIN}") - set(ACL_LOG_FILE_PATHS "/var/log") + # Remote file paths + set(REMOTE_OTC_BIN_PATH "${CMAKE_BINARY_DIR}/${REMOTE_OTC_BIN}") + set(REMOTE_OTC_CONFIG_BIN_PATH "${CMAKE_BINARY_DIR}/${REMOTE_OTC_CONFIG_BIN}") ## # Other @@ -99,12 +81,4 @@ macro(set_otc_settings) set(CPACK_RESOURCE_FILE_LICENSE "${ASSETS_DIR}/LICENSE") set(CPACK_PACKAGE_DESCRIPTION_FILE "${ASSETS_DIR}/description") set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "An agent to send logs, metrics and traces to Sumo Logic") - - # Set target dependencies of the cpack target for this package. The - # otelcol-sumo binary matching the GOOS, GOARCH and FIPS flag are required for - # OTC packages. - set(target_dependencies - "${SOURCE_OTC_BINARY}" - "${SOURCE_OTC_CONFIG_BINARY}" - ) endmacro() diff --git a/utils.cmake b/utils.cmake index c1f340e3..029b4766 100644 --- a/utils.cmake +++ b/utils.cmake @@ -3,3 +3,4 @@ include("${UTILS_DIR}/cpack-state.cmake") include("${UTILS_DIR}/property.cmake") include("${UTILS_DIR}/build-validation.cmake") include("${UTILS_DIR}/targets.cmake") +include("${UTILS_DIR}/detect_version.cmake") diff --git a/utils/detect_version.cmake b/utils/detect_version.cmake new file mode 100644 index 00000000..68f1c4aa --- /dev/null +++ b/utils/detect_version.cmake @@ -0,0 +1,32 @@ +function(detect_version _artifact_bin _working_dir) + # Construct version command + set(_cmd_str "./${_artifact_bin} --version") + separate_arguments(_cmd UNIX_COMMAND ${_cmd_str}) + + message(STATUS "Setting working directory: ${_working_dir}") + message(STATUS "Running version command: ${_cmd_str}") + + # Get output of otelcol-sumo --version + execute_process(COMMAND ${_cmd} + WORKING_DIRECTORY ${_working_dir} + COMMAND_ERROR_IS_FATAL ANY + OUTPUT_VARIABLE _version_output + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + + message(STATUS "Version output: ${_version_output}") + + string(REGEX MATCH ".* v([0-9]+\.[0-9]+\.[0-9]+)\-sumo\-([0-9]+)\-.*" _ ${_version_output}) + + if(NOT CMAKE_MATCH_COUNT EQUAL 2) + message(FATAL_ERROR "Could not parse version information from version output") + endif() + + set(_otc_version "${CMAKE_MATCH_1}") + set(_sumo_version "${CMAKE_MATCH_2}") + set(_otc_version "${_otc_version}" PARENT_SCOPE) + set(_sumo_version "${_sumo_version}" PARENT_SCOPE) + + message(STATUS "Detected OTC version: ${_otc_version}") + message(STATUS "Detected Sumo version: ${_sumo_version}") +endfunction() diff --git a/utils/variables.cmake b/utils/variables.cmake index 0177e229..9efeb1fd 100644 --- a/utils/variables.cmake +++ b/utils/variables.cmake @@ -6,3 +6,12 @@ function(require_variables varNames) endif() endforeach() endfunction() + +# Print out all variables (for debugging) +function(print_all_variables) + get_cmake_property(_variableNames VARIABLES) + list (SORT _variableNames) + foreach (_variableName ${_variableNames}) + message(STATUS "${_variableName}=${${_variableName}}") + endforeach() +endfunction() diff --git a/version.cmake b/version.cmake index 98501f29..a90df64b 100644 --- a/version.cmake +++ b/version.cmake @@ -1,47 +1,20 @@ -# OTC_VERSION represents the base version of otelcol-sumo. This value is used to -# fetch the otelcol-sumo binary from GitHub and as the version of the packages -# produced by this project. -# E.g. the A.B.C in A.B.C-sumo-X -if(NOT DEFINED ENV{OTC_VERSION}) - message(FATAL_ERROR "OTC_VERSION environment variable must be set") -endif() - -set(OTC_VERSION "$ENV{OTC_VERSION}") - -if(NOT "${OTC_VERSION}" MATCHES "^[0-9]+\\.[0-9]+\\.[0-9]+$") - message(FATAL_ERROR - "OTC_VERSION contains an invalid version: ${OTC_VERSION}\n" - "Must be in the form X.Y.Z" - ) -endif() - -message(STATUS "OTC Version is set to: ${OTC_VERSION}") - -# OTC_SUMO_VERSION represents the "sumo" version of otelcol-sumo. It is -# primarily used for fetching the otelcol-sumo binary from GitHub but it will -# also be used for the PACKAGE_RELEASE version (e.g. the X in A.B.C-X) if -# OTC_BUILD_NUMBER is not specified. -# E.g. the X in A.B.C-sumo-X -if(NOT DEFINED ENV{OTC_SUMO_VERSION}) - message(FATAL_ERROR "OTC_SUMO_VERSION environment variable must be set") -endif() - -set(OTC_SUMO_VERSION "$ENV{OTC_SUMO_VERSION}") - -if(NOT ("${OTC_SUMO_VERSION}" MATCHES "^[0-9]+$" OR "${OTC_SUMO_VERSION}" MATCHES "^[0-9]+-rc.[0-9]+$")) - message(FATAL_ERROR - "OTC_SUMO_VERSION contains an invalid version: ${OTC_SUMO_VERSION}\n" - ) -endif() +# Determine the OTC version and OTC Sumo version from the output of a previously +# downloaded build of the otelcol-sumo binary. +set(_version_artifact_bin "otelcol-sumo") +set(_version_detection_dir "${CMAKE_BINARY_DIR}/version_detection") +detect_version("${_version_artifact_bin}" "${_version_detection_dir}") -message(STATUS "OTC Sumo Version is set to: ${OTC_SUMO_VERSION}") +set(OTC_VERSION "${_otc_version}") +set(OTC_SUMO_VERSION "${_sumo_version}") # OTC_BUILD_NUMBER represents the PACKAGE_RELEASE version used for incremental # changes to the packaging code. This should contain a unique, unsigned integer -# that increments with each build to allow upgrades from one package to another. -# A CI job number will typically be used for this value. +# that increments with each build to allow upgrades from one package version to +# another. +# A CI job number will typically be used for this value as it will naturally +# increase with each new package build. # E.g. the X in A.B.C-X or A.B.C.X -if(DEFINED ENV{OTC_BUILD_NUMBER}) +if(DEFINED ENV{OTC_BUILD_NUMBER} AND NOT "$ENV{OTC_BUILD_NUMBER}" STREQUAL "") set(BUILD_NUMBER "$ENV{OTC_BUILD_NUMBER}") else() set(BUILD_NUMBER "${OTC_SUMO_VERSION}") diff --git a/version_artifact.cmake b/version_artifact.cmake new file mode 100644 index 00000000..a821032d --- /dev/null +++ b/version_artifact.cmake @@ -0,0 +1,109 @@ +## +# This file is a CMake script and should be run in CMake's script mode. +# E.g. cmake -P fetch_artifacts.cmake +# +# Its purpose is to fetch otelcol-sumo matching the host system's operating +# system & CPU architecture from a given GitHub Actions workflow. This +# otelcol-sumo binary is used to determine the version for packages and is not +# included in the generated packages. +## + +# Require CMake >= 3.24.1 +cmake_minimum_required(VERSION 3.24.1 FATAL_ERROR) + +# Required environment variables +if(NOT DEFINED ENV{GH_TOKEN}) + message(FATAL_ERROR "GH_TOKEN environment variable must be set") +endif() + +if(NOT DEFINED ENV{GH_WORKFLOW_ID}) + message(FATAL_ERROR "GH_WORKFLOW_ID environment variable must be set") +endif() + +# Required and optional programs. Attempts to find required and optional +# programs used to build the packages. +find_program(GH_PROGRAM gh REQUIRED) + +# Include the CMakeDetermineSystem module manually as it is not included when +# CMake runs in script mode. This is required for the host's CPU architecture +# to be detected. +set(CMAKE_PLATFORM_INFO_DIR "${CMAKE_SOURCE_DIR}/build/cmake") +include(CMakeDetermineSystem) + +# Set supported platforms for version detection +set(_supported_platforms "") +list(APPEND _supported_platforms "darwin_amd64") +list(APPEND _supported_platforms "darwin_arm64") +list(APPEND _supported_platforms "linux_amd64") +list(APPEND _supported_platforms "linux_arm64") +list(APPEND _supported_platforms "windows_amd64") + +# Detect host system +string(TOLOWER "${CMAKE_HOST_SYSTEM_NAME}" _os) +string(TOLOWER "${CMAKE_HOST_SYSTEM_PROCESSOR}" _arch) + +# Convert _arch to GOARCH equivalent name +if("${_arch}" STREQUAL "aarch64") + set(_arch "arm64") +endif() + +if("${_arch}" STREQUAL "x86_64") + set(_arch "amd64") +endif() + +set(_platform "${_os}_${_arch}") +message(STATUS "Detected platform: ${_platform}") + +if(NOT "${_platform}" IN_LIST _supported_platforms) + message(FATAL_ERROR "This script does not support this machine's platform: ${_platform}") +endif() + +set(_bin_ext "") +if("${_os}" STREQUAL "windows") + set(_bin_ext ".exe") +endif() + +# Set directory variables +set(UTILS_DIR "${CMAKE_SOURCE_DIR}/utils") +set(_build_dir "${CMAKE_SOURCE_DIR}/build") +set(_version_detection_dir "${_build_dir}/version_detection") + +# Include CMake files +include("${CMAKE_SOURCE_DIR}/utils.cmake") + +# GitHub variables +set(_gh_org "SumoLogic") +set(_gh_repo "sumologic-otel-collector") +set(_gh_slug "${_gh_org}/${_gh_repo}") +set(_gh_workflow "$ENV{GH_WORKFLOW_ID}") + +# Remote OTC artifact +set(_artifact_bin "otelcol-sumo-${_os}_${_arch}${_bin_ext}") +set(_artifact_path "${_version_detection_dir}/${_artifact_bin}") +set(_new_artifact_bin "otelcol-sumo${_bin_ext}") +set(_new_artifact_path "${_version_detection_dir}/${_new_artifact_bin}") + +if(NOT EXISTS "${_new_artifact_path}") + # Construct download command + set(_cmd "${GH_PROGRAM}") + set(_args_str "run download -R ${_gh_slug} ${_gh_workflow}") + set(_args_str "${_args_str} -n ${_artifact_bin}") + separate_arguments(_args NATIVE_COMMAND ${_args_str}) + + # Create the version_detection directory + make_directory("${_version_detection_dir}") + + message(STATUS "Setting working directory: ${_version_detection_dir}") + message(STATUS "Running download command: ${_cmd} ${_args_str}") + + # Execute download command + execute_process(COMMAND "${_cmd}" ${_args} + WORKING_DIRECTORY ${_version_detection_dir} + COMMAND_ERROR_IS_FATAL ANY) + + # Rename downloaded artifact to otelcol-sumo + file(RENAME ${_artifact_path} ${_new_artifact_path}) +endif() + +# Detect version +detect_version("${_new_artifact_bin}" "${_version_detection_dir}")