Skip to content

Commit

Permalink
ci: only update version info when publishing stable release and allow…
Browse files Browse the repository at this point in the history
… to manually trigger alpha release
  • Loading branch information
wangl-cc committed Nov 29, 2023
1 parent 37e11cd commit 7ea9875
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 54 deletions.
96 changes: 57 additions & 39 deletions .github/scripts/parse_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,86 @@

set -e

CARGO_PKG_VERSION=$(yq -r '.package.version' maa-cli/Cargo.toml)
CARGO_PKG_VERSION=$(yq -oy -r '.package.version' maa-cli/Cargo.toml)
commit_sha=$(git rev-parse HEAD)

if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then
echo "PR detected, marking version as alpha pre-release and skipping publish"
channel="alpha"
version="$CARGO_PKG_VERSION-alpha.$(date +%s)"
tag="nightly"
publish="false"
elif [ "$GITHUB_EVENT_NAME" == "schedule" ]; then
echo "Scheduled event detected, marking version as alpha pre-release and publish to alpha channel"
# check if there are some new commits
release_alpha() {
channel="alpha"
published_commit=$(yq -r ".details.commit" version/$channel.json)
published_commit=$(yq -oy -r ".details.commit" version/$channel.json)
if [ "$published_commit" == "$commit_sha" ]; then
echo "No new commits, exiting, skipping all steps"
echo "skip=true" >> "$GITHUB_OUTPUT"
exit 0
fi
version="$CARGO_PKG_VERSION-alpha.$(date +%s)"
tag="nightly"
}

release_beta() {
channel="beta"
published_version=$(yq -oy -r ".details.version" version/$channel.json)
published_version_prefix=${published_version%-*}
published_version_suffix=${published_version#*-}
if [ "$published_version_prefix" != "$CARGO_PKG_VERSION" ]; then
echo "Last published version is not the same as current version (published: $published_version)"
version="$CARGO_PKG_VERSION-$channel.1"
elif [ "$published_version_suffix" == "$published_version" ]; then
echo "Last published version is not a pre-release version (published: $published_version)"
version="$CARGO_PKG_VERSION-$channel.1"
else
beta_number=${published_version_suffix#*.}
version="$CARGO_PKG_VERSION-$channel.$((beta_number + 1))"
fi
}

if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then
echo "PR detected, marking version as alpha pre-release and skipping publish"
release_alpha
publish="false"
elif [ "$GITHUB_EVENT_NAME" == "schedule" ]; then
echo "Scheduled event detected, marking version as alpha pre-release and publish to alpha channel"
release_alpha
tag="nightly"
elif [ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]; then
echo "Workflow dispatch event detected, reading inputs"
beta=$(yq -r '.inputs.beta' "$GITHUB_EVENT_PATH")
if [ "$beta" == "true" ]; then
echo "Beta flag detected, marking version as beta pre-release and publish to beta channel"
channel="beta"
published_version=$(yq -r ".details.version" version/beta.json)
published_version_prefix=${published_version%-*}
published_version_suffix=${published_version#*-}
if [ "$published_version_prefix" != "$CARGO_PKG_VERSION" ]; then
echo "Last published version is not the same as current version (published: $published_version)"
version="$CARGO_PKG_VERSION-$channel.1"
elif [ "$published_version_suffix" == "$published_version" ]; then
echo "Last published version is not a pre-release version (published: $published_version)"
version="$CARGO_PKG_VERSION-$channel.1"
else
beta_number=${published_version_suffix#*.}
version="$CARGO_PKG_VERSION-$channel.$((beta_number + 1))"
fi
channel=$(yq -oy -r '.inputs.channel' "$GITHUB_EVENT_PATH")
if [ "$channel" == "alpha" ]; then
echo "Dispatched alpha channel, marking version as alpha pre-release"
release_alpha
tag="nightly"
elif [ "$channel" == "beta" ]; then
echo "Beta channel detected, marking version as beta pre-release and publish to beta channel"
release_beta
elif [ "$channel" == "stable" ]; then
echo "Stable channel detected, marking version as stable release and publish to stable channel"
else
echo "No beta flag detected, marking version as stable release and publish to stable channel"
channel="stable"
echo "Unknown channel $channel, aborting"
exit 1
fi
publish=$(yq -r '.inputs.publish' "$GITHUB_EVENT_PATH")
else # push to a tag
REF_VERSION=${GITHUB_REF#refs/tags/v}
if [ "$REF_VERSION" == "$GITHUB_REF" ]; then
publish=$(yq -oy -r '.inputs.publish' "$GITHUB_EVENT_PATH")
elif [ "$GITHUB_EVENT_NAME" == "push" ]; then
ref_version=${GITHUB_REF#refs/tags/v}
if [ "$ref_version" == "$GITHUB_REF" ]; then
echo "Version tag not matched, aborting"
exit 1
fi
echo "Tag detected, marking version as stable release and publish to stable channel"
channel="stable"
version="$REF_VERSION"
else
echo "Unknown event $GITHUB_EVENT_NAME, aborting"
exit 1
fi

echo "Release version $version to $channel channel with tag $tag"
channel=${channel:-stable}
version=${version:-$CARGO_PKG_VERSION}
tag=${tag:-v$version}
publish=${publish:-true}

echo "Release version $version with tag $tag to channel $channel (publish: $publish)"
{
echo "commit=$commit_sha"
echo "channel=$channel"
echo "version=$version"
echo "tag=${tag:-v$version}"
echo "publish=${publish:-ture}"
echo "tag=$tag"
echo "publish=$publish"
echo "skip=false"
} >> "$GITHUB_OUTPUT"
32 changes: 17 additions & 15 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ on:
- ".github/workflows/release.yml"
workflow_dispatch:
inputs:
beta:
description: Release as beta
default: true
channel:
description: The release channel
default: beta
required: true
type: boolean
beta_number:
description: Beta number of this release
default: 1
required: true
type: number
type: choice
options:
- alpha
- beta
- stable
publish:
description: Whether to publish a new release
default: false
Expand Down Expand Up @@ -221,12 +220,15 @@ jobs:
echo "$checksum" > "$archive_name.sha256"
# old version info (deprecated)
version_file="version/version.json"
yq -i -oj ".maa-cli.$target.version = \"$VERSION\"" "$version_file"
yq -i -oj ".maa-cli.$target.tag = \"$TAG\"" "$version_file"
yq -i -oj ".maa-cli.$target.name = \"$archive_name\"" "$version_file"
yq -i -oj ".maa-cli.$target.size = $size" $version_file
yq -i -oj ".maa-cli.$target.sha256sum = \"$checksum_hash\"" "$version_file"
# only update when a stable release is published
if [ "$CHANNEL" == "stable" ]; then
version_file="version/version.json"
yq -i -oj ".maa-cli.$target.version = \"$VERSION\"" "$version_file"
yq -i -oj ".maa-cli.$target.tag = \"$TAG\"" "$version_file"
yq -i -oj ".maa-cli.$target.name = \"$archive_name\"" "$version_file"
yq -i -oj ".maa-cli.$target.size = $size" $version_file
yq -i -oj ".maa-cli.$target.sha256sum = \"$checksum_hash\"" "$version_file"
fi
# target dependent version info
for version_file in "${version_files[@]}"; do
Expand Down

0 comments on commit 7ea9875

Please sign in to comment.