From 068a01a2e1a4a70b7c1c248ea3218ef06edf9257 Mon Sep 17 00:00:00 2001 From: Antonette Caldwell Date: Thu, 16 Nov 2023 21:07:33 -0600 Subject: [PATCH] ci(repo): create pre-release.yml workflow fix #344 --- .../workflows/{release.yml => _release.yml} | 11 +-- .github/workflows/pre-release.yml | 68 +++++++++++++++++++ scripts/create-multiple-git-tag.sh | 19 ++++++ scripts/version-prerelease-packages.sh | 20 ++++++ 4 files changed, 108 insertions(+), 10 deletions(-) rename .github/workflows/{release.yml => _release.yml} (91%) create mode 100644 .github/workflows/pre-release.yml create mode 100755 scripts/create-multiple-git-tag.sh create mode 100755 scripts/version-prerelease-packages.sh diff --git a/.github/workflows/release.yml b/.github/workflows/_release.yml similarity index 91% rename from .github/workflows/release.yml rename to .github/workflows/_release.yml index 59234f17..bc8f900b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/_release.yml @@ -1,15 +1,6 @@ name: Publish NPM Package -on: - release: - types: [published] - branches: - - 'v-*' - - master - workflow_dispatch: - inputs: - release-type: - required: true +on: workflow_dispatch concurrency: ${{ github.workflow }}-${{ github.ref }} diff --git a/.github/workflows/pre-release.yml b/.github/workflows/pre-release.yml new file mode 100644 index 00000000..c03edbc4 --- /dev/null +++ b/.github/workflows/pre-release.yml @@ -0,0 +1,68 @@ +name: Pre-release and Publish to NPM + +on: workflow_dispatch + +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - name: Checkout Repo + uses: actions/checkout@v4 + + - name: Setup Node.js 18.x + uses: actions/setup-node@v3 + with: + node-version: 18 + + - name: Install deps and build + run: | + yarn + yarn build-all + + publish-gpr: + needs: build + runs-on: ubuntu-latest + permissions: + contents: write + packages: write + steps: + - name: Checkout Repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup Node.js 18.x + uses: actions/setup-node@v3 + with: + node-version: 18 + registry-url: "https://registry.npmjs.org" + scope: "@layer5" + + - name: Identify changed packages + run: | + CHANGED_PACKAGES=$(yarn lerna changed --json | jq -r '.[].name') + echo $CHANGED_PACKAGES + + - name: Version packages + run: ./version-prerelease-packages.sh $CHANGED_PACKAGES + + - name: Commit changes + run: | + if [ -n "$(git status --porcelain)" ]; then + git add . + git commit -m "chore: publish" + git push origin HEAD + else + echo "No changes to commit." + fi + + - name: Create Git tags + run: ./create-multiple-git-tag.sh + + - name: Publish packages + run: yarn lerna publish from-package --yes + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} \ No newline at end of file diff --git a/scripts/create-multiple-git-tag.sh b/scripts/create-multiple-git-tag.sh new file mode 100755 index 00000000..f259f718 --- /dev/null +++ b/scripts/create-multiple-git-tag.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +for dir in $(HUSKY=0 yarn lerna ls --json --all | jq -r '.[].location'); do + VERSION=$(node -e "console.log(require('$dir/package.json').version)") + NAME=$(node -e "console.log(require('$dir/package.json').name)") + TAG="$NAME@v$VERSION" + + # Check if the tag already exists + if git rev-parse -q --verify "refs/tags/$TAG" > /dev/null; then + echo "Git tag $TAG already exists. Skipping tag creation." + else + # Tag doesn't exist, create it + git tag -a "$TAG" -m "Version $VERSION" + echo "Git tag $TAG created" + fi +done + +# Push all tags +git push --tags diff --git a/scripts/version-prerelease-packages.sh b/scripts/version-prerelease-packages.sh new file mode 100755 index 00000000..8b860e68 --- /dev/null +++ b/scripts/version-prerelease-packages.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# Usage: version-prerelrease-packages.sh +# Example: version-prerelrease-packages.sh + +# Get the list of changed packages using Lerna +CHANGED_PACKAGES=$(HUSKY=0 yarn lerna changed --json | jq -r '.[].name') + +if [ -n "$CHANGED_PACKAGES" ]; then + echo "Changed packages detected: $CHANGED_PACKAGES" + HUSKY=0 yarn lerna version --no-private --conventional-commits --conventional-prerelease --include-merged-tags --no-git-tag-version --yes + + # Stage changes to package.json files + for PACKAGE_NAME in $CHANGED_PACKAGES; do + PACKAGE_PATH="packages/$(echo $PACKAGE_NAME | tr -d '@' | sed 's/\//-/')" + grep -q "\"name\": \"$PACKAGE_NAME\"" "$PACKAGE_PATH/package.json" && git add "$PACKAGE_PATH/package.json" + done +else + echo "No changed packages detected. Skipping lerna version." +fi