build(CD): improved the upload-release-to-plugin-repo.yml #697
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build Blockera Plugin Zip | |
on: | |
pull_request: | |
push: | |
branches: [master] | |
workflow_dispatch: | |
inputs: | |
release_type: | |
description: 'Your release type?' | |
required: true | |
type: 'choice' | |
options: | |
- 'rc' | |
- 'stable' | |
version_type: | |
description: 'Your version type?' | |
required: true | |
default: 'patch' | |
type: 'choice' | |
options: | |
- 'major' | |
- 'minor' | |
- 'patch' | |
# Cancels all previous workflow runs for pull requests that have not completed. | |
concurrency: | |
# The concurrency group contains the workflow name and the branch name for pull requests | |
# or the commit hash for any other events. | |
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }} | |
cancel-in-progress: true | |
jobs: | |
compute-next-stable-branch: | |
name: Compute next stable release branches | |
runs-on: ubuntu-latest | |
if: ${{ github.event_name == 'workflow_dispatch' }} | |
outputs: | |
next_stable_branch: ${{ steps.get_branches.outputs.next_stable_branch }} | |
steps: | |
- name: Get current and next stable release branches | |
id: get_branches | |
run: | | |
LATEST_STABLE_TAG=$(git tag --list 'v*' | sort -V | tail -n 1) | |
IFS='.' read LATEST_STABLE_MAJOR LATEST_STABLE_MINOR LATEST_STABLE_PATCH <<< "${LATEST_STABLE_TAG#v}" | |
VERSION_TYPE="${{ github.event.inputs.version_type }}" | |
if [[ $VERSION_TYPE == "major" ]]; then | |
echo "next_stable_branch=release/$((LATEST_STABLE_MAJOR + 1)).0" >> $GITHUB_OUTPUT | |
elif [[ $VERSION_TYPE == "minor" ]]; then | |
echo "next_stable_branch=release/${LATEST_STABLE_MAJOR}.$((LATEST_STABLE_MINOR + 1))" >> $GITHUB_OUTPUT | |
else | |
echo "next_stable_branch=release/${LATEST_STABLE_MAJOR}.${LATEST_STABLE_MINOR}.$((LATEST_STABLE_PATCH + 1))" >> $GITHUB_OUTPUT | |
fi | |
bump-version: | |
name: Bump version | |
runs-on: ubuntu-latest | |
needs: compute-next-stable-branch | |
if: | | |
github.event_name == 'workflow_dispatch' && ( | |
( | |
github.ref == 'refs/heads/master' || | |
endsWith( github.ref, needs.compute-next-stable-branch.outputs.next_stable_branch ) | |
) && ( | |
github.event.inputs.release_type == 'rc' || | |
github.event.inputs.release_type == 'stable' | |
) || ( | |
startsWith( github.ref, 'refs/heads/release/' ) && | |
github.event.inputs.release_type == 'stable' | |
) | |
) | |
outputs: | |
changelog: ${{ steps.update_packages.outputs.changelog }} | |
old_version: ${{ steps.get_version.outputs.old_version }} | |
new_version: ${{ steps.get_version.outputs.new_version }} | |
release_branch: ${{ steps.get_version.outputs.release_branch }} | |
release_branch_commit: ${{ steps.commit_version_bump_to_release_branch.outputs.version_bump_commit }} | |
master_commit: ${{ steps.commit_version_bump_to_master.outputs.version_bump_commit }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 | |
with: | |
token: ${{ secrets.BLOCKERABOT_PAT }} | |
show-progress: ${{ runner.debug == '1' && 'true' || 'false' }} | |
- name: Compute old and new version | |
id: get_version | |
run: | | |
OLD_VERSION=$(jq --raw-output '.version' package.json) | |
echo "old_version=${OLD_VERSION}" >> $GITHUB_OUTPUT | |
if [[ ${{ github.event.inputs.release_type }} == 'stable' ]]; then | |
IFS='.' read -r -a OLD_VERSION_ARRAY <<< "$OLD_VERSION" | |
if [[ ${{ github.event.inputs.version_type }} == "major" ]]; then | |
NEW_VERSION=$(npx semver $OLD_VERSION -i major) | |
elif [[ ${{ github.event.inputs.version_type }} == "minor" ]]; then | |
NEW_VERSION=$(npx semver $OLD_VERSION -i minor) | |
else | |
NEW_VERSION=$(npx semver $OLD_VERSION -i patch) | |
fi | |
else | |
if [[ $OLD_VERSION == *"rc"* ]]; then | |
NEW_VERSION=$(npx semver $OLD_VERSION -i prerelease) | |
else | |
if [[ ${{ github.event.inputs.version_type }} == "major" ]]; then | |
NEW_VERSION="$(npx semver $OLD_VERSION -i major)-rc.1" | |
elif [[ ${{ github.event.inputs.version_type }} == "minor" ]]; then | |
NEW_VERSION="$(npx semver $OLD_VERSION -i minor)-rc.1" | |
else | |
NEW_VERSION="$(npx semver $OLD_VERSION -i patch)-rc.1" | |
fi | |
fi | |
fi | |
echo "new_version=${NEW_VERSION}" >> $GITHUB_OUTPUT | |
IFS='.' read -r -a NEW_VERSION_ARRAY <<< "$NEW_VERSION" | |
RELEASE_BRANCH="release/${NEW_VERSION_ARRAY[0]}.${NEW_VERSION_ARRAY[1]}.${NEW_VERSION_ARRAY[2]}" | |
echo "release_branch=${RELEASE_BRANCH}" >> $GITHUB_OUTPUT | |
- name: Configure git user name and email | |
run: | | |
git config user.name "blockerabot" | |
git config user.email [email protected] | |
- name: Create and switch to release branch | |
if: | | |
github.event.inputs.release_type == 'rc' && | |
! contains( steps.get_version.outputs.old_version, 'rc' ) | |
run: git checkout -b "${{ steps.get_version.outputs.release_branch }}" | |
- name: Switch to release branch | |
if: | | |
github.event.inputs.release_type == 'stable' || | |
contains( steps.get_version.outputs.old_version, 'rc' ) | |
run: | | |
git fetch --depth=1 origin "${{ steps.get_version.outputs.release_branch }}" | |
git checkout "${{ steps.get_version.outputs.release_branch }}" | |
- name: Setup Node.js and install dependencies | |
uses: ./.github/setup-node | |
- name: Update the changelog of packages | |
id: update_packages | |
run: | | |
npm run update:changelogs -- --version="${{ steps.get_version.outputs.new_version }}" | |
CHANGELOG=$(cat changelog.txt | awk '{printf "%s\\n", $0}') | |
echo "changelog=$CHANGELOG" >> $GITHUB_OUTPUT | |
git add . | |
git commit -m "Update Changelog for ${{ steps.get_version.outputs.new_version }}" | |
echo "has_commit=$(git rev-parse --verify --short HEAD)" >> $GITHUB_OUTPUT | |
- name: Update plugin version | |
env: | |
VERSION: ${{ steps.get_version.outputs.new_version }} | |
run: | | |
cat <<< $(jq --tab --arg version "${VERSION}" '.version = $version' package.json) > package.json | |
cat <<< $(jq --tab --arg version "${VERSION}" '.version = $version | .packages[""].version = $version' package-lock.json) > package-lock.json | |
sed -i "s/${{ steps.get_version.outputs.old_version }}/${VERSION}/g" blockera.php | |
- name: Commit the version bump to the release branch | |
id: commit_version_bump_to_release_branch | |
run: | | |
git add blockera.php package.json package-lock.json | |
git commit -m "Bump plugin version to ${{ steps.get_version.outputs.new_version }}" | |
git push --set-upstream origin "${{ steps.get_version.outputs.release_branch }}" | |
echo "version_bump_commit=$(git rev-parse --verify --short HEAD)" >> $GITHUB_OUTPUT | |
- name: Fetch master | |
if: ${{ github.ref != 'refs/heads/master' }} | |
run: git fetch --depth=1 origin master | |
- name: Cherry-pick the version bump commit to master | |
id: commit_version_bump_to_master | |
run: | | |
git checkout master | |
git pull | |
MASTER_VERSION=$(jq --raw-output '.version' package.json) | |
if [[ ${{ steps.get_version.outputs.old_version }} == "$MASTER_VERSION" ]]; then | |
git cherry-pick "${{ steps.update_packages.outputs.has_commit }}" | |
git cherry-pick "${{ steps.get_version.outputs.release_branch }}" | |
git push | |
echo "version_bump_commit=$(git rev-parse --verify --short HEAD)" >> $GITHUB_OUTPUT | |
fi | |
build: | |
name: Build Release Artifact | |
runs-on: ubuntu-latest | |
needs: bump-version | |
if: | | |
always() && ( | |
github.event_name == 'pull_request' || | |
github.event_name == 'workflow_dispatch' || | |
github.repository == 'blockeraai/blockera' | |
) | |
outputs: | |
job_status: ${{ job.status }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 | |
with: | |
ref: ${{ needs.bump-version.outputs.release_branch || github.ref }} | |
show-progress: ${{ runner.debug == '1' && 'true' || 'false' }} | |
- name: Setup Node.js and install dependencies | |
uses: ./.github/setup-node | |
- name: Setup PHP Composer install dependencies | |
uses: ./.github/setup-php | |
with: | |
composer-options: '--no-dev -o --apcu-autoloader -a' | |
- name: Build Plugin Zip File | |
uses: ./.github/build-plugin-zip | |
- name: Upload artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: blockera | |
path: ./blockera.zip | |
- name: Build release notes draft | |
if: ${{ needs.bump-version.outputs.new_version }} | |
env: | |
VERSION: ${{ needs.bump-version.outputs.new_version }} | |
run: | | |
IFS='.' read -r -a VERSION_ARRAY <<< "${VERSION}" | |
MILESTONE="Blockera ${VERSION_ARRAY[0]}.${VERSION_ARRAY[1]}" | |
echo "${{ needs.bump-version.outputs.changelog }}" | sed 's/\\n/\n/g' > release-note.txt | |
# Continue to creating release notes with preparing other changelogs | |
npm run other:changelog -- --milestone="$MILESTONE" --unreleased --file="release-note.txt" --version="${{ needs.bump-version.outputs.new_version }}" > release-notes.txt | |
sed -ie '1,6d' release-notes.txt | |
if [[ ${{ needs.bump-version.outputs.new_version }} != *"rc"* ]]; then | |
# Include previous RCs' release notes, if any | |
CHANGELOG_REGEX="=\s[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]+)?\s=" | |
RC_REGEX="=\s${VERSION}(-rc\.[0-9]+)?\s=" | |
awk "/${RC_REGEX}/ {found=1;print;next} /${CHANGELOG_REGEX}/ {found=0} found" changelog.txt >> release-notes.txt | |
fi | |
- name: Upload release notes artifact | |
if: ${{ needs.bump-version.outputs.new_version }} | |
uses: actions/upload-artifact@v4 | |
with: | |
name: release-notes | |
path: ./release-notes.txt | |
revert-version-bump: | |
name: Revert version bump if build failed | |
needs: [bump-version, build] | |
runs-on: ubuntu-latest | |
if: | | |
always() && | |
( needs.build.outputs.job_status == 'failure' ) && | |
needs.bump-version.outputs.release_branch_commit | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 | |
with: | |
fetch-depth: 2 | |
ref: ${{ needs.bump-version.outputs.release_branch }} | |
token: ${{ secrets.BLOCKERABOT_PAT }} | |
show-progress: ${{ runner.debug == '1' && 'true' || 'false' }} | |
- name: Configure git user name and email | |
run: | | |
git config user.name "blockerabot" | |
git config user.email [email protected] | |
- name: Revert version bump commit on release branch | |
if: | | |
github.event.inputs.release_type == 'stable' || | |
contains( needs.bump-version.outputs.old_version, 'rc' ) | |
run: | | |
git revert --no-edit ${{ needs.bump-version.outputs.release_branch_commit }} | |
git push --set-upstream origin "${{ needs.bump-version.outputs.release_branch }}" | |
- name: Delete release branch if it was only just created for the RC | |
if: | | |
github.event.inputs.release_type == 'rc' && | |
! contains( needs.bump-version.outputs.old_version, 'rc' ) | |
run: | | |
git push origin :"${{ needs.bump-version.outputs.release_branch }}" | |
- name: Revert version bump on master | |
if: ${{ needs.bump-version.outputs.master_commit }} | |
run: | | |
git fetch --depth=2 origin master | |
git checkout master | |
git revert --no-edit ${{ needs.bump-version.outputs.master_commit }} | |
git push --set-upstream origin master | |
create-release: | |
name: Create Release Draft and Attach Asset | |
needs: [bump-version, build] | |
runs-on: ubuntu-latest | |
steps: | |
- name: Set Release Version | |
id: get_release_version | |
env: | |
VERSION: ${{ needs.bump-version.outputs.new_version }} | |
run: echo "version=$(echo $VERSION | cut -d / -f 3 | sed 's/-rc./ RC/' )" >> $GITHUB_OUTPUT | |
- name: Download Plugin Zip Artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: blockera | |
- name: Download Release Notes Artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: release-notes | |
- name: Create Release Draft | |
id: create_release | |
uses: actions/create-release@v1 | |
env: | |
GITHUB_TOKEN: ${{ secrets.BLOCKERABOT_PAT }} | |
with: | |
tag_name: 'v${{ needs.bump-version.outputs.new_version }}' | |
release_name: ${{ steps.get_release_version.outputs.version }} | |
commitish: ${{ needs.bump-version.outputs.release_branch || github.ref }} | |
draft: true | |
prerelease: ${{ contains(needs.bump-version.outputs.new_version, 'rc') }} | |
body_path: release-notes.txt | |
- name: Upload Release Asset | |
id: upload-release-asset | |
uses: actions/upload-release-asset@v1 | |
env: | |
GITHUB_TOKEN: ${{ secrets.BLOCKERABOT_PAT }} | |
with: | |
upload_url: ${{ steps.create_release.outputs.upload_url }} | |
asset_path: ./blockera.zip | |
asset_name: blockera.zip | |
asset_content_type: application/zip |