From ee2b532c622a12f2793d1653dddb36f593931be8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20N=C3=BCtzi?= Date: Sun, 21 Apr 2024 15:52:41 +0200 Subject: [PATCH] fix: Update release workflow with version file :anchor: --- .github/workflows/release.yml | 39 +++++++++++-- justfile | 4 +- nix/pkgs/default.nix | 102 +++++++++++++++++----------------- nix/pkgs/version.json | 1 + nix/result | 2 +- scripts/release.sh | 97 ++++++++++++++++++++++++++------ 6 files changed, 171 insertions(+), 74 deletions(-) create mode 100644 nix/pkgs/version.json diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5b2d9f58..6c24a0cf 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -42,18 +42,48 @@ jobs: git config user.name 'githooks-bot' git config user.email 'githooks-bot@users.noreply.github.com' + # Gets the message on the annotated commit: deref() { git for-each-ref "refs/tags/$TRIGGER_TAG" --format="%($1)" ; }; + # Creates a new tag with the same message, including trailing headers. git tag "$RELEASE_TAG" -a -m "$(deref contents)" git tag "githooks/$RELEASE_TAG" - - name: Safety Check + + - name: Define Release Branch + id: releaseBranch env: RELEASE_TAG: ${{ steps.getTag.outputs.releaseTag }} run: | - git fetch --depth 50 origin main - [ -n "$(git rev-list --first-parent --ancestry-path "$RELEASE_TAG^..origin/main")" ] || { - echo "Tag is not reachable from main (--first-parent) !" >&2 + # If we specify another branch we are checking this one, + RELEASE_BRANCH="main" + RELEASE_VERSION="${RELEASE_TAG##v}" + + regex="^Release-Branch:\s+(.*)$" + # Gets the message on the annotated commit: + deref() { + git for-each-ref "refs/tags/$RELEASE_TAG" --format="%($1)" ; + }; + + if deref contents | grep -qE "$regex"; then + RELEASE_BRANCH=$(deref contents | grep -E "$regex" | + sed -E "s/$regex/\1/") || { + echo "Release-Branch trailer is wrong." + exit 1 + } + fi + # Fetch the branch. + git fetch --depth 50 origin "$RELEASE_BRANCH" + + # Check if its reachable. + [ -n "$(git rev-list --first-parent \ + --ancestry-path + "$RELEASE_TAG^..origin/$RELEASE_BRANCH")" ] || { + echo "Tag is not reachable from '$RELEASE_BRANCH' (--first-parent) !" >&2 exit 1 } + + echo ::set-output name=releaseVersion ::$RELEASE_VERSION + echo ::set-output name=releaseBranch ::$RELEASE_BRANCH + - name: Run GoReleaser uses: goreleaser/goreleaser-action@v2 with: @@ -72,4 +102,5 @@ jobs: run: | # go releaser already pushed release tag git push origin "githooks/$RELEASE_TAG" + git push -f origin ":$TRIGGER_TAG" diff --git a/justfile b/justfile index 104ddf13..2c3acee2 100644 --- a/justfile +++ b/justfile @@ -10,9 +10,9 @@ build *args: cd "{{root_dir}}" && \ githooks/scripts/build.sh "$@" -build-nix: +build-nix *args: cd "{{root_dir}}" && \ - nix build -L "./nix#default" -o ./nix/result + nix build -L "./nix#default" -o ./nix/result {{args}} doc *args: diff --git a/nix/pkgs/default.nix b/nix/pkgs/default.nix index ecb60ac3..223244ad 100644 --- a/nix/pkgs/default.nix +++ b/nix/pkgs/default.nix @@ -6,64 +6,66 @@ testers, makeWrapper, versionMeta ? "", -}: -buildGo122Module rec { - pname = "githooks"; +}: let + currentVersion = (lib.importJSON ./version.json).version; +in + buildGo122Module rec { + pname = "githooks"; - version = - if versionMeta != "" - then "3.0.0+nix.${versionMeta}" - else "3.0.0"; + version = + if versionMeta != "" + then "${currentVersion}+nix.${versionMeta}" + else currentVersion; - src = ../../.; - # In nixpkgs that should be: - # fetchFromGitHub { - # owner = "gabyx"; - # repo = "githooks"; - # rev = "v${version}"; - # hash = "sha256-TD6RiZ4Bq8gU444erYDkuGrKkpDrjMTrSH3qZpBwwqk="; - # }; + src = ../../.; + # In nixpkgs that should be: + # fetchFromGitHub { + # owner = "gabyx"; + # repo = "githooks"; + # rev = "v${version}"; + # hash = "sha256-TD6RiZ4Bq8gU444erYDkuGrKkpDrjMTrSH3qZpBwwqk="; + # }; - modRoot = "./githooks"; - vendorHash = "sha256-ZcDD4Z/thtyCvXg6GzzKC/FSbh700QEaqXU8FaZaZc4="; - nativeBuildInputs = [makeWrapper]; - buildInputs = [git]; + modRoot = "./githooks"; + vendorHash = "sha256-ZcDD4Z/thtyCvXg6GzzKC/FSbh700QEaqXU8FaZaZc4="; + nativeBuildInputs = [makeWrapper]; + buildInputs = [git]; - ldflags = [ - "-s" # Disable symbole table. - "-w" # Disable DWARF generation. - ]; + ldflags = [ + "-s" # Disable symbole table. + "-w" # Disable DWARF generation. + ]; - tags = ["package_manager_enabled"]; + tags = ["package_manager_enabled"]; - doCheck = false; + doCheck = false; - postConfigure = '' - GH_BUILD_VERSION="${version}" \ - GH_BUILD_TAG="v${version}" \ - go generate -mod=vendor ./... - ''; + postConfigure = '' + GH_BUILD_VERSION="${version}" \ + GH_BUILD_TAG="v${version}" \ + go generate -mod=vendor ./... + ''; - postInstall = '' - mv "$out/bin/cli" "$out/bin/githooks-cli" - mv "$out/bin/runner" "$out/bin/githooks-runner" - mv "$out/bin/dialog" "$out/bin/githooks-dialog" + postInstall = '' + mv "$out/bin/cli" "$out/bin/githooks-cli" + mv "$out/bin/runner" "$out/bin/githooks-runner" + mv "$out/bin/dialog" "$out/bin/githooks-dialog" - wrapProgram "$out/bin/githooks-cli" --prefix PATH : ${lib.makeBinPath [git]} - wrapProgram "$out/bin/githooks-runner" --prefix PATH : ${lib.makeBinPath [git]} - ''; + wrapProgram "$out/bin/githooks-cli" --prefix PATH : ${lib.makeBinPath [git]} + wrapProgram "$out/bin/githooks-runner" --prefix PATH : ${lib.makeBinPath [git]} + ''; - passthru.tests.version = testers.testVersion { - package = "githooks-cli"; - command = "githooks-cli --version"; - inherit version; - }; + passthru.tests.version = testers.testVersion { + package = "githooks-cli"; + command = "githooks-cli --version"; + inherit version; + }; - meta = with lib; { - description = "Githooks is a Git hooks manager with per-repo and shared Git hooks including version control"; - homepage = "https://github.com/gabyx/Githooks"; - license = licenses.mpl20; - maintainers = with maintainers; [gabyx]; - mainProgram = "githooks-cli"; - }; -} + meta = with lib; { + description = "Githooks is a Git hooks manager with per-repo and shared Git hooks including version control"; + homepage = "https://github.com/gabyx/Githooks"; + license = licenses.mpl20; + maintainers = with maintainers; [gabyx]; + mainProgram = "githooks-cli"; + }; + } diff --git a/nix/pkgs/version.json b/nix/pkgs/version.json new file mode 100644 index 00000000..f339f7b9 --- /dev/null +++ b/nix/pkgs/version.json @@ -0,0 +1 @@ +{ "version": "3.0.0" } diff --git a/nix/result b/nix/result index 80483912..eaf8e7a8 120000 --- a/nix/result +++ b/nix/result @@ -1 +1 @@ -/nix/store/195rgxnkqvq8lk03rq8nibz53zpzsx4f-githooks-cli-3.0.0 \ No newline at end of file +/nix/store/mis2msnw1jd2pchad5j23jmzpzihi4kx-githooks-3.0.0+nix.96c86715a221cb2238981c05661f07dfeccb6b90-dirty \ No newline at end of file diff --git a/scripts/release.sh b/scripts/release.sh index 1e70bb97..cddb8bac 100755 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -1,27 +1,90 @@ #!/usr/bin/env bash +# +# Creating a prepare tag to trigger the release process on the +# Github workflow. +# You can set the following meaning full +# Git trailers on the annotated tag: +# `Update-Info: ...` : One line of update info to be given to the user when updating. +# `Update-NoSkip: true` : Specifying that this version update cannot be skipped. +# `Release-Branch: .*`: Specifying another branch than main. +# Useful for testing stuff which does not affect the user, since +# it must be on this branch to get this update. +# This branch will be checked to contain the tag. + set -euo pipefail -readarray -t prepareTag < <(git tag --list "prepare-*") +ROOT_DIR=$(git rev-parse --show-toplevel) +cd "$ROOT_DIR" -for tag in "${prepareTag[@]}"; do - echo "Deleting prepare tag '$tag'." - git push -f origin ":${tag}" || true - git tag -d "$tag" -done +function delete_prepare_tags() { + readarray -t prepareTag < <(git tag --list "prepare-*") -version="$1" -tag="v$version" -if git tag --list "v*" | grep -q "$tag"; then - echo "Git tag '$tag' already exists." -fi + for tag in "${prepareTag[@]}"; do + echo "Deleting prepare tag '$tag'." + git push -f origin ":${tag}" || true + git tag -d "$tag" + done +} + +function commit_version_file() { + local version="$1" + echo "Writing new version file..." + + temp=$(mktemp) + jq ".version |= \"$version\"" nix/pkgs/version.json >"$temp" + mv "$temp" nix/pkgs/version.json + + if git diff --quiet --exit-code; then + git add nix/pkgs/version.json + git commit -m "np: Update version to '$version'" + fi +} + +function create_tag() { + tag="v$version" + if git tag --list "v*" | grep -q "$tag"; then + echo "Git tag '$tag' already exists." + exit 1 + fi + add_message=() + if [ -n "$update_info" ]; then + add_message+=(-m "Update-Info: $update_info") + fi + + if [ "$branch" != "main" ]; then + add_message+=(-m "Release-Branch: $branch") + fi + + echo "Tagging..." + git tag -a -m "Version $tag" "${add_message[@]}" "prepare-$tag" + + echo "Tag contains:" + git cat-file -p "prepare-$tag" +} + +function trigger_build() { + printf "Do you want to trigger the build? [y|n]: " + read -r answer + if [ "$answer" != "y" ]; then + echo "Do not trigger build -> abort." + exit 0 + fi + + echo "Pushing tag 'prepare-$tag'." + git push -f origin --no-follow-tags "$branch" "prepare-$tag" +} + +version="$1" update_info="${2:-}" +branch=$(git branch --show-current) -if [ -z "$update_info" ]; then - git tag "prepare-$tag" -else - git tag -a -m "Version $tag" -m "Update-Info: $update_info" "prepare-$tag" +if ! git diff --quiet --exit-code; then + echo "You have changes on this branch." + exit 1 fi -echo "Pushing tag 'prepare-$tag'." -git push -f origin "prepare-$tag" +delete_prepare_tags +commit_version_file "$version" +create_tag +trigger_build