From 7ea987597959b53194c3ecd70b21daa0e72d72e8 Mon Sep 17 00:00:00 2001 From: Loong Date: Wed, 29 Nov 2023 22:37:16 +0800 Subject: [PATCH] ci: only update version info when publishing stable release and allow to manually trigger alpha release --- .github/scripts/parse_version.sh | 96 +++++++++++++++++++------------- .github/workflows/release.yml | 32 ++++++----- 2 files changed, 74 insertions(+), 54 deletions(-) diff --git a/.github/scripts/parse_version.sh b/.github/scripts/parse_version.sh index ad1c598c..ccc6c392 100755 --- a/.github/scripts/parse_version.sh +++ b/.github/scripts/parse_version.sh @@ -2,20 +2,12 @@ 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" @@ -23,47 +15,73 @@ elif [ "$GITHUB_EVENT_NAME" == "schedule" ]; then 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" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4709e7dd..2943c8e9 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 @@ -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