Skip to content

Commit

Permalink
fix: Update release workflow with version file ⚓
Browse files Browse the repository at this point in the history
  • Loading branch information
gabyx committed Apr 21, 2024
1 parent 96c8671 commit ee2b532
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 74 deletions.
39 changes: 35 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,48 @@ jobs:
git config user.name 'githooks-bot'
git config user.email '[email protected]'
# 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:
Expand All @@ -72,4 +102,5 @@ jobs:
run: |
# go releaser already pushed release tag
git push origin "githooks/$RELEASE_TAG"
git push -f origin ":$TRIGGER_TAG"
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
102 changes: 52 additions & 50 deletions nix/pkgs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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";
};
}
1 change: 1 addition & 0 deletions nix/pkgs/version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "version": "3.0.0" }
2 changes: 1 addition & 1 deletion nix/result
97 changes: 80 additions & 17 deletions scripts/release.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ee2b532

Please sign in to comment.