Skip to content

Commit

Permalink
Introduce new release flow - Part 1 (segmentio#2143)
Browse files Browse the repository at this point in the history
* Introduce new release flow

* quick check to validate push permissios

* revert changes

* refactor release.sh

* revert release.sh

* Revert unwanted changes

* Restore gh pat token change
  • Loading branch information
varadarajan-tw authored Jul 8, 2024
1 parent 8db747a commit 93087d2
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 23 deletions.
45 changes: 24 additions & 21 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,27 @@ jobs:
run: |
yarn lerna publish from-package --yes --allowBranch=main --loglevel=verbose --dist-tag latest
# Commenting this out temporarily until we fix the release script to not rely on tags
# release:
# needs: build-and-publish # comment when testing locally with https://github.com/nektos/act
#
# runs-on: ubuntu-20.04
# timeout-minutes: 15
# steps:
# - uses: actions/checkout@v4
# with:
# fetch-depth: 0 # Shallow clones should be disabled for computing changelog
# fetch-tags: true
#
# - name: Create Github Release
# id: create-github-release
# uses: actions/github-script@v7
# env:
# RELEASE_TAG: ${{ steps.get-release-tag.outputs.release-tag }}
# with:
# script: |
# const script = require('./scripts/github-action/create-github-release.js')
# await script({github, context, core, exec})
release:
needs: build-and-publish # comment when testing locally with https://github.com/nektos/act

runs-on: ubuntu-20.04
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled to ensure the commit history for the repository is available to the action
fetch-tags: true

- name: Generate Release Tag
id: get-release-tag
run: ./scripts/generate-release-tags.sh

- name: Create Github Release
id: create-github-release
uses: actions/github-script@v7
env:
RELEASE_TAG: ${{ steps.get-release-tag.outputs.release-tag }}
with:
script: |
const script = require('./scripts/github-action/create-github-release.js')
await script({github, context, core, exec})
72 changes: 72 additions & 0 deletions .github/workflows/version-packages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# This workflow is triggered manually via the GitHub Actions page or API
name: Version Packages
on:
workflow_dispatch:
inputs:
branch:
description: 'Branch to create PR from'
required: true
default: 'release-actions'

jobs:
build-and-version-packages:
env:
HUSKY: 0
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: write
steps:
- uses: actions/checkout@v4
with:
ref: main
depth: 0

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
registry-url: 'https://registry.npmjs.org'
cache: yarn

- name: Checkout branch
run: |
git checkout -b ${{ github.event.inputs.branch }}
git push origin ${{ github.event.inputs.branch }}
- name: Install Dependencies
run: yarn install --frozen-lockfile --ignore-optional

- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Build
run: NODE_ENV=production yarn build

- name: Version Packages
run: yarn lerna version minor --allow-branch ${{ github.event.inputs.branch }} --no-git-tag-version --no-commit-hooks --no-private --yes

- name: Commit and push
id: commit_and_push
run: |
count=$(git diff --cached --stat)
if [ -z "$count" ]; then
echo "No changes to commit"
echo "SKIP_PR=true" >> $GITHUB_OUTPUT
exit 0
fi
git add .
git commit -m "chore: version packages"
git push origin ${{ github.event.inputs.branch }}
- name: Create PR
if: ${{ steps.commit_and_push.outputs.SKIP_PR != 'true' }}
run: |
packages_published=$(git status -s -uno| grep "package.json" |awk '{print $2}'| xargs jq -r '.name + "@" + .version' --argjson null {})
pr_message="This PR was opened by GithHub Actions. Whenever you're ready to publish the packages, merge this PR."
description="$(printf "%s\n # Packages\n%s" "$pr_message" "$packages_published")"
gh pr create --base main --head ${{ github.event.inputs.branch }} --title "Publish" --body "$description"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
25 changes: 24 additions & 1 deletion scripts/postversion.sh → scripts/generate-release-tags.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,32 @@
set -e
sha=$(git rev-parse HEAD);
branch=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD);
message=$(git log -1 --pretty=%B);

if [[ $branch != "main" && $branch != "release" ]];
then
echo "Skipping release tag generation as branch is not main or release"
exit 0
fi;

if [[ $message != *"Publish"* ]];
then
echo "Skipping release tag generation as last commit is not a publish commit"
exit 0
fi;

# check if jq is installed
if ! command -v jq &> /dev/null
then
echo "jq could not be found. Please install jq to generate release tag."
exit 1
fi

# Generate and push release tag. Release tag format: release-YYYY-MM-DD[.N] e.g. release-2024-01-01
if ! n=$(git rev-list --count $sha~ --grep "Publish" --since="00:00"); then
echo 'Failed to compute release tag. Exiting.'
exit 1
else
else
case "$n" in
0) suffix="" ;; # first commit of the day gets no suffix
*) suffix=".$n" ;; # subsequent commits get a suffix, starting with .1
Expand All @@ -26,3 +40,12 @@ else
echo "Tagging $sha with $tag"
git tag -a $tag -m "Release $tag"
fi

# this script assumes the last commit was publish commit and gets all package.json files changed in the last commit
# and generates tags for each package.json file.
files=$(git show --pretty="" --name-only HEAD | grep -Ei '^packages/.*package\.json$')
for file in $files; do
tag=$(cat $file | jq -r '.name + "@" + .version')
echo "Tagging $sha with $tag"
git tag -a $tag -m "Release $tag"
done
2 changes: 1 addition & 1 deletion scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ fi;

git pull --ff-only
echo "Running lerna version minor..."
lerna version minor --no-private -y
lerna version minor --no-private -y

0 comments on commit 93087d2

Please sign in to comment.