Skip to content

Commit

Permalink
Split workflows (#782)
Browse files Browse the repository at this point in the history
The current github `release` workflow has 3 use cases:
1. Create PR to bump version
2. Create draft release
3. Publish draft release to Github if already published to NPM

Using this workflow is not intuitive and can lead to confusion as
multiple actions (bumping the version, creating a draft release, and
publishing the release) are combined into a single workflow.

Changes in PR:
- Split github `release` workflow into following:
  - Bump version workflow
  - Create draft release workflow
  - Publish release to github workflow

This separation increases the clarity, maintainability, and flexibility
of the release process.
  • Loading branch information
akshay-ap authored Sep 23, 2024
1 parent 3291169 commit 1e03774
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Release
name: Bump Version
on:
workflow_dispatch:

Expand All @@ -19,6 +19,6 @@ jobs:
node-version: 20
- run: |
npm ci
bash bin/github-release.sh --verbose
bash bin/github-release.sh --verbose bump
env:
GH_TOKEN: ${{ github.token }}
24 changes: 24 additions & 0 deletions .github/workflows/draft_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Draft Release
on:
workflow_dispatch:

permissions:
contents: write
pull-requests: write

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- uses: actions/setup-node@v4
with:
node-version: 20
- run: |
npm ci
bash bin/github-release.sh --verbose draft
env:
GH_TOKEN: ${{ github.token }}
24 changes: 24 additions & 0 deletions .github/workflows/publish_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Publish Release
on:
workflow_dispatch:

permissions:
contents: write
pull-requests: write

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- uses: actions/setup-node@v4
with:
node-version: 20
- run: |
npm ci
bash bin/github-release.sh --verbose publish
env:
GH_TOKEN: ${{ github.token }}
150 changes: 87 additions & 63 deletions bin/github-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@ It provides functionality for automatically:
• Publishing GitHub releases once the version lands on NPM
USAGE
bash bin/github-release.sh [-v|--verbose] [-n|--dry-run]
bash bin/github-release.sh [-v|--verbose] [-n|--dry-run] [bump|draft|publish]
OPTIONS
-v | --verbose Verbose output
-n | --dry-run Dry run; don't create any commits, tags, or draft a new
release GitHub.
EXAMPLES
bash bin/github-release.sh
bash bin/github-release.sh bump
bash bin/github-release.sh draft
bash bin/github-release.sh publish
EOF
}

verbose="n"
dryrun="n"
command=""

while [[ $# -gt 0 ]]; do
case "$1" in
-v|--verbose)
Expand All @@ -34,6 +38,9 @@ while [[ $# -gt 0 ]]; do
-n|--dry-run)
dryrun="y"
;;
bump|draft|publish)
command="$1"
;;
*)
usage
exit 1
Expand All @@ -42,6 +49,11 @@ while [[ $# -gt 0 ]]; do
shift
done

if [[ -z "$command" ]]; then
usage
exit 1
fi

log() {
if [[ "$verbose" == "y" ]]; then
echo "$@"
Expand Down Expand Up @@ -78,65 +90,77 @@ draft="$(gh release view "$tag" --json isDraft,targetCommitish --jq 'if(.isDraft
log "commit: ${commit}"
log "draft: ${draft:-none}"

if [[ -z "$draft" ]] && [[ "$current" == "$latest" ]]; then
# In this case, we have no existing draft, and the current version is
# the same as the latest release, so we need to create a new PR to bump
# the package version.
log "==> Bumping Version"

git fetch --tags --force --quiet
if [[ -n "$(git tag -l --points-at HEAD | awk '$1 == "'$tag'"')" ]]; then
log "no changes since latest release"
exit 0
fi

newtag="$(npm version patch --no-git-tag-version)"
log "bumping to $newtag"

branch="bump/$newtag"
if git ls-remote --heads origin | grep "refs/heads/$branch\$" >/dev/null; then
log "version bump PR already exists"
exit 0
fi
if [[ "$dryrun" == "n" ]]; then
log "creating PR bumping version"
git checkout -b "$branch"
git commit -am "Bump Version to $newtag"
git push -u origin "$branch"
gh pr create --fill
fi
elif [[ "$current" != "$latest" ]]; then
# In this case, the current version is newer that the latest released
# version on NPM, so we create or update the draft release to ensure it
# includes the latest version.
log "==> Drafting Release"

if [[ "$commit" == "$draft" ]]; then
log "draft is already at latest commit"
exit 0
fi

log "generating NPM package"
npm pack
package="${name#@}-$current.tgz"
package="${package//\//-}"

if [[ "$dryrun" == "n" ]]; then
log "drafting release with NPM tarball"
if [[ -n "$draft" ]]; then
log "cleaning up existing draft"
gh release delete "$tag" --yes
case $command in
"bump")
if [[ -z "$draft" ]] && [[ "$current" == "$latest" ]]; then
# In this case, we have no existing draft, and the current version is
# the same as the latest release, so we need to create a new PR to bump
# the package version.
log "==> Bumping Version"

git fetch --tags --force --quiet
if [[ -n "$(git tag -l --points-at HEAD | awk '$1 == "'$tag'"')" ]]; then
log "no changes since latest release"
exit 0
fi

newtag="$(npm version patch --no-git-tag-version)"
log "bumping to $newtag"

branch="bump/$newtag"
if git ls-remote --heads origin | grep "refs/heads/$branch\$" >/dev/null; then
log "version bump PR already exists"
exit 0
fi
if [[ "$dryrun" == "n" ]]; then
log "creating PR bumping version"
git checkout -b "$branch"
git commit -am "Bump Version to $newtag"
git push -u origin "$branch"
gh pr create --fill
fi
else
log "skipped version bump. Either a draft release already exists or the current version is not the latest."
fi
gh release create "$tag" --draft --generate-notes --target "$commit" --title "$tag" "$package"
fi
else # if [[ -n "$draft" ]] && [[ "$current" == "$latest" ]]; then
# In this case, there is an existing draft, and the latest version is
# equal to it. Publish the draft release!
log "==> Publishing Release"

tag="v$current"
if [[ "$dryrun" == "n" ]]; then
log "publishing draft release"
gh release edit "$tag" --draft=false
fi
fi
;;
"draft")
if [[ -z "$draft" ]] && [[ "$current" != "$latest" ]]; then
# In this case, the current version is newer that the latest released
# version on NPM, so we create or update the draft release to ensure it
# includes the latest version.
log "==> Drafting Release"

if [[ "$commit" == "$draft" ]]; then
log "draft is already at latest commit"
exit 0
fi

log "generating NPM package"
npm pack
package="${name#@}-$current.tgz"
package="${package//\//-}"

if [[ "$dryrun" == "n" ]]; then
log "drafting release with NPM tarball"
gh release create "$tag" --draft --generate-notes --target "$commit" --title "$tag" "$package"
fi
else
log "draft release not created. Either a draft release already exists or the current version equals the latest NPM release."
fi
;;
"publish")
if [[ -n "$draft" ]] && [[ "$current" == "$latest" ]]; then
# In this case, there is an existing draft, and the latest version is
# equal to it. Publish the draft release!
log "==> Publishing Release"

tag="v$current"
if [[ "$dryrun" == "n" ]]; then
log "publishing draft release"
gh release edit "$tag" --draft=false
fi
else
log "skipped release publishing. Either no draft release exists or the current version is not the latest NPM release."
fi
;;
esac

0 comments on commit 1e03774

Please sign in to comment.