From aaf6b65aeed1492440276213be3427eca39d302e Mon Sep 17 00:00:00 2001 From: Michael Sprengel Date: Tue, 28 Nov 2023 11:36:22 +0100 Subject: [PATCH] Remove pkg.yml and adjust versioning logic (#23) --- .github/workflows/build_pkg.yml | 31 ++++++++++++- container/bin/build_source | 82 ++++++++++++++++++++++----------- container/build.containerfile | 3 ++ scripts/gh_release | 14 ++++-- 4 files changed, 98 insertions(+), 32 deletions(-) diff --git a/.github/workflows/build_pkg.yml b/.github/workflows/build_pkg.yml index 6716333..bb65380 100644 --- a/.github/workflows/build_pkg.yml +++ b/.github/workflows/build_pkg.yml @@ -11,9 +11,27 @@ on: build_container: type: string default: ghcr.io/gardenlinux/package-build + source: + type: string + default: "" + debian_source: + type: string + default: "" dependencies: type: string default: "" + email: + type: string + default: "" + maintainer: + type: string + default: "" + distribution: + type: string + default: "" + message: + type: string + default: "" jobs: source: name: source package @@ -22,6 +40,7 @@ jobs: build_options: ${{ steps.build.outputs.build_options }} release: ${{ steps.release.outputs.release }} runs-on: ubuntu-latest + if: github.ref_type == 'tag' steps: - uses: actions/checkout@v4 with: @@ -47,10 +66,16 @@ jobs: cat _pkgs/Packages - name: build id: build + env: + GITHUB_REF_TYPE: ${{ github.ref_type }} + GITHUB_REF_NAME: ${{ github.ref_name }} run: | + version="" + [ "$GITHUB_REF_TYPE" == "tag" ] && version=${GITHUB_REF_NAME#*/} pkg="$(podman run \ --rm \ --mount="type=bind,src=$PWD/${GITHUB_ACTION_PATH}/container/bin,dst=/usr/local/sbin,ro" \ + -e PACKAGE_VERSION="$version" \ -v "$PWD/input:/input" \ -v "$PWD/output:/output" \ -v "$PWD/_pkgs:/pkgs" \ @@ -58,6 +83,7 @@ jobs: )" echo "pkg=$pkg" | tee -a "$GITHUB_OUTPUT" echo "build_options=$(cat output/.build_options)" | tee -a "$GITHUB_OUTPUT" + echo "source_name=$(cat output/.source_name)" | tee -a "$GITHUB_OUTPUT" - name: check if ${{ env.pkg }} already released id: check run: | @@ -69,8 +95,11 @@ jobs: - name: draft release and upload source packages id: release if: ${{ steps.check.outputs.skip_release != 'true' }} + env: + PKG_NAME: ${{ steps.build.outputs.pkg }} run: | - release="$(./scripts/gh_release "${{ github.token }}" "${{ github.repository }}" create --draft "${{ steps.build.outputs.pkg }}" "${{ github.sha }}" "${{ steps.build.outputs.pkg }}")" + tag="gardenlinux/${PKG_NAME#${{ steps.build.outputs.source_name }}_}" + release="$(./scripts/gh_release "${{ github.token }}" "${{ github.repository }}" create --draft "$tag" "${{ github.sha }}" "${{ steps.build.outputs.pkg }}")" for f in output/*; do ./scripts/gh_release "${{ github.token }}" "${{ github.repository }}" upload "$release" "$f" done diff --git a/container/bin/build_source b/container/bin/build_source index 8c487ca..8dd706f 100755 --- a/container/bin/build_source +++ b/container/bin/build_source @@ -10,38 +10,37 @@ email="contact@gardenlinux.io" maintainer="Garden Linux Builder" distribution=gardenlinux message="Rebuild for Garden Linux." +debian_source="" inputs_file="/input/.github/workflows/build.yml" main() ( - DEB_BUILD_OPTIONS="terse $(yq -r '.build_options // ""' < "/input/pkg.yaml")" - export DEB_BUILD_OPTIONS + # Init some variables + name="$(yq -r '.name' < "$inputs_file")" + source="$(yq -r '.jobs.build_pkg.with.source // "'$name'"' < "$inputs_file")" + debian_source="$(yq -r '.jobs.build_pkg.with.debian_source // "'$debian_source'"' < "$inputs_file")" + build_options="terse $(yq -r '.jobs.build_pkg.with.build_options // ""' < "$inputs_file")" - source="$(yq -r '.source // ""' < "/input/pkg.yaml")" - debian_source="$(yq -r '.debian_source // ""' < "/input/pkg.yaml")" + # Define Build Options + export DEB_BUILD_OPTIONS="$build_options" - if [[ "$source" = "git+"* ]]; then - git_source "${source#git+}" - elif [[ "$source" = "native" ]]; then - native_source - else - apt_source "$source" - fi + # Get sources for building source package + # out of it + get_sources $source - # if debian_source defined: remove debian folder from source and replace with debian folder from debian_source + # Is the debian_source parameter specified? + # If yes, replace the original debian/ directory + # with the one provided via debian_source. if [ -n "$debian_source" ]; then mkdir debian_src cd debian_src - if [[ "$debian_source" = "git+"* ]]; then - git_source "${debian_source#git+}" - else - apt_source "$debian_source" - fi + get_sources $debian_source rm -rf ../src/debian mv src/debian ../src/ cd .. rm -rf debian_src fi + # Start preparing and building the source package cd src apply_patches /input @@ -54,20 +53,25 @@ main() ( fi dpkg-source --build . - cd .. + + # Do some cleanup afterwards rm -rf src ls -lah + # Add some meta files next to the created artifacts echo "$DEB_BUILD_OPTIONS" > .build_options + echo "${pkg}" > .source_name ln -s "${pkg}_${version}.dsc" .source + # Copy all artifacts to the dedicated output directory if [ -d "/output" ]; then - { echo .build_options; echo .source; echo "${pkg}_${version}.dsc"; get_files < "${pkg}_${version}.dsc"; } | while read file; do + { echo .build_options; echo .source_name; echo .source; echo "${pkg}_${version}.dsc"; get_files < "${pkg}_${version}.dsc"; } | while read file; do sudo cp -d "$file" "/output/$file" done fi + # Finally, print the package name and version echo "${pkg}_${version}" >&3 ) @@ -128,21 +132,45 @@ apply_patches() ( fi name="$(yq -r '.name' < "$inputs_file")" - version="$(yq -r '.version // ""' < "$1/pkg.yaml")" - [ -n "$version" ] || version="$(dpkg-parsechangelog --show-field Version)" - email="$(yq -r --arg name "$name" '.jobs[$name].with.email // "'"$email"'"' < "$inputs_file")" - maintainer="$(yq -r --arg name "$name" '.jobs[$name].with.maintainer // "'"$maintainer"'"' < "$inputs_file")" - distribution="$(yq -r --arg name "$name" '.jobs[$name].with.distribution // "'"$distribution"'"' < "$inputs_file")" - version_suffix="$(yq -r '.version_suffix // ""' < "$1/pkg.yaml")" - message="$(yq -r --arg name "$name" '.jobs[$name].with.message // "'"$message"'"' < "$inputs_file")" + email="$(yq -r '.jobs.build_pkg.with.email // "'"$email"'"' < "$inputs_file")" + maintainer="$(yq -r '.jobs.build_pkg.with.maintainer // "'"$maintainer"'"' < "$inputs_file")" + distribution="$(yq -r '.jobs.build_pkg.with.distribution // "'"$distribution"'"' < "$inputs_file")" + message="$(yq -r '.jobs.build_pkg.with.message // "'"$message"'"' < "$inputs_file")" - DEBEMAIL="$email" DEBFULLNAME="$maintainer" dch --newversion "$version$version_suffix" --distribution "$distribution" --force-distribution -- "$message" + DEBEMAIL="$email" DEBFULLNAME="$maintainer" dch --newversion "$(get_version)" --distribution "$distribution" --force-distribution -- "$message" if [ -x "$1/exec.post" ]; then SOURCE="$source" "$1/exec.post" fi ) +get_sources() ( + local source=$1 + + # What kind of source do we have? + if [[ "$source" = "git+"* ]]; then + git_source "${source#git+}" + elif [[ "$source" = "native" ]]; then + native_source + else + apt_source "$source" + fi +) + +get_version() ( + local version=$PACKAGE_VERSION + + git -C /input config --global --add safe.directory '*' + + # If version is undefined, let's simply use + # the version of the upstream package and add + # the git tag to it. + [ -n "$version" ] || version="$(dpkg-parsechangelog --show-field Version)gardenlinux~$(git -C /input rev-parse HEAD)" + + # Print the version accordingly + echo $version +) + get_files() ( awk '!/^ / { flag=0 } flag { print $NF } /^Files:/ { flag=1 }' ) diff --git a/container/build.containerfile b/container/build.containerfile index 470dfe9..7f06c89 100755 --- a/container/build.containerfile +++ b/container/build.containerfile @@ -16,6 +16,9 @@ RUN apt-get update \ FROM $arch/$image WORKDIR /tmp +# Define Environment variables +ENV PACKAGE_VERSION="" + # Copy repository configration files for apt COPY container/apt/ /etc/apt/ diff --git a/scripts/gh_release b/scripts/gh_release index eb0e863..00647f2 100755 --- a/scripts/gh_release +++ b/scripts/gh_release @@ -64,9 +64,11 @@ case "$action" in commit="$1"; shift name="$1"; shift + normalized_tag=${tag/\~/\_} + release="$(post "releases" '{ "draft": '"$draft"', - "tag_name": "'"$tag"'", + "tag_name": "'"$normalized_tag"'", "target_commitish": "'"$commit"'", "name": "'"$name"'" }' | jq -r '.id')" @@ -78,16 +80,20 @@ case "$action" in release="$1"; shift asset_file="$1"; shift - asset_name="$(basename "$asset_file")" + normalized_asset_file=${asset_file/\~/\_} + + asset_name="$(basename "$normalized_asset_file")" upload "releases/$release/assets?name=$asset_name" < "$asset_file" > /dev/null - echo "uploaded $asset_file to $release" + echo "uploaded $asset_file to $release ($asset_name)" ;; "download") release="$1"; shift name="$1"; shift - asset_id="$(get "releases/$release/assets" | jq -r '.[] | select(.name == "'"$name"'") | .id')" + normalized_name=${name/\~/\_} + + asset_id="$(get "releases/$release/assets" | jq -r '.[] | select(.name == "'"$normalized_name"'") | .id')" get_binary "releases/assets/$asset_id" > "$name" ;;