Skip to content

Commit

Permalink
feat: workflow to publish major SDK version (#1402)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeSchwert authored Jan 25, 2024
1 parent f36c9c9 commit cc63ac2
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/publish-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Publish SDK Reference Docs

on:
workflow_run:
workflows: ["Publish to NPM"]
workflows: ["Publish to NPM", "Publish Major Version to NPM"]
types:
- completed

Expand Down
189 changes: 189 additions & 0 deletions .github/workflows/publish-major-version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
name: Publish Major Version to NPM

on:
workflow_dispatch:
inputs:
release_type:
type: choice
description: Release Type
options:
- alpha
- release
required: true
default: alpha
upgrade_type:
type: choice
description: Upgrade Type
options:
- none
# - patch
# - minor
- major
required: false
default: none
dry_run:
type: boolean
description: "(Optional) Dry run"
required: false
default: true

jobs:
PublishMajorVersion:
name: Publish Workflow
runs-on: ubuntu-latest-4-cores
env:
GH_TOKEN: ${{ secrets.TS_IMMUTABLE_SDK_GITHUB_TOKEN }}
NODE_OPTIONS: --max-old-space-size=14366
SDK_PUBLISH_SLACK_WEBHOOK: ${{ secrets.SDK_PUBLISH_SLACK_WEBHOOK }}
SDK_PUBLISH_MAJOR_VERSION_ACTORS: ${{ secrets.SDK_PUBLISH_MAJOR_VERSION_ACTORS }}
steps:
- name: Check User Permission
id: check_user_permission
uses: actions-cool/check-user-permission@v2
with:
token: ${{ secrets.TS_IMMUTABLE_SDK_GITHUB_TOKEN }}
require: admin
username: ${{ github.triggering_actor }}
check-bot: true

- name: Log User Permission
run: echo "Check user permissions for triggering actor - ${{ github.triggering_actor }}"
- run: echo "user-permission = ${{ steps.check_user_permission.outputs.user-permission }}"
- run: echo "require-result = ${{ steps.check_user_permission.outputs.require-result }}"

- name: Admin Permission Check
if: ${{ steps.check_user_permission.outputs.require-result != 'true' }}
run: exit 1

- name: Allowed Actors Check
id: allowed_actors_check
# only allow certain SDK team members to run this workflow
if: ${{ contains(fromJson(env.SDK_PUBLISH_MAJOR_VERSION_ACTORS), github.triggering_actor) }}
run: echo "ALLOWED_ACTOR=true" >> $GITHUB_OUTPUT

- name: Allowed Actors Filter
if: ${{ steps.allowed_actors_check.outputs.ALLOWED_ACTOR != 'true' }}
run: exit 1

- name: Check Public Release Branch
if: contains(github.event.inputs.release_type, 'release') && (github.ref != 'refs/heads/main')
run: failure("Public releases should be only done from main branch, current branch ${{ github.ref }}")

- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup Github
run: |
git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
- name: Get tags
run: git fetch --tags

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: lts/*
cache: 'yarn'

- name: Workout next version string
run: |
upgrade_type=${{ github.event.inputs.upgrade_type }}
if [ ${{ contains(github.event.inputs.upgrade_type, 'none') }} == true ]
then
upgrade_type=""
revision_upgrade=$( ${{ contains(github.event.inputs.release_type, 'alpha') }} && echo '--revision' || echo '')
echo $revision
fi
./.github/scripts/version-up.sh --${{ github.event.inputs.release_type }} --$upgrade_type --apply $revision_upgrade
shell: bash

- name: Install dependencies
run: yarn install --immutable

- name: Lint
run: yarn lint

- name: Check Single Package Version Policy
run: yarn syncpack:check

- name: Get next version string
id: version
run: |
echo "NEXT_VERSION=$(git describe --tags --abbrev=0)" >> $GITHUB_OUTPUT
- name: Update package.json version for build
run: |
tmp=$(mktemp)
jq '.version = "${{steps.version.outputs.NEXT_VERSION}}"' ./sdk/package.json > "$tmp" && mv "$tmp" ./sdk/package.json
- name: Build
run: export NODE_OPTIONS=--max-old-space-size=6144 && RELEASE_TYPE=${{ github.event.inputs.release_type }} yarn build

- name: Typecheck
run: yarn typecheck

- name: Test
run: yarn test

- name: Push tags
# Boolean inputs are not actually booleans, see https://github.com/actions/runner/issues/1483
if: github.event.inputs.dry_run == 'false'
run: |
echo "$(git push --tags)"
- name: Pre Release Step
if: contains(github.event.inputs.release_type, 'alpha')
id: pre_release
uses: JS-DevTools/npm-publish@v3
with:
token: ${{ secrets.TS_IMMUTABLE_SDK_NPM_TOKEN }}
access: public
package: ./sdk/package.json
tag: ${{ contains(github.event.inputs.release_type, 'alpha') && 'alpha' }}
dry-run: ${{ github.event.inputs.dry_run }}

- name: Authenticate NPM
if: contains(github.event.inputs.release_type, 'release')
run: npm config set //registry.npmjs.org/:_authToken ${{ secrets.TS_IMMUTABLE_SDK_NPM_TOKEN }}

- name: Release
id: npm_release
if: contains(github.event.inputs.release_type, 'release')
run: yarn release --ci --no-increment -c .release-it.json $( ${{ github.event.inputs.dry_run }} && echo "--dry-run" || echo "") --github.tokenRef=${{ secrets.TS_IMMUTABLE_SDK_GITHUB_TOKEN }}

- name: Warm up CDN
id: warm_up_cdn
if: contains(github.event.inputs.release_type, 'release')
run: wget https://cdn.jsdelivr.net/npm/@imtbl/sdk/dist/browser/checkout.js

# Wait for 30 seconds to make sure the tag is available on GitHub
- uses: GuillaumeFalourd/wait-sleep-action@v1
with:
time: '30'

- name: Create GitHub Release
id: gh_release
if: contains(github.event.inputs.release_type, 'release') && github.event.inputs.dry_run == 'false'
run: gh release create ${{ steps.version.outputs.NEXT_VERSION }} --title ${{ steps.version.outputs.NEXT_VERSION }} --draft=false --prerelease=false --generate-notes --repo immutable/ts-immutable-sdk

- name: Get GitHub Release Name and URL
if: contains(github.event.inputs.release_type, 'release') && github.event.inputs.dry_run == 'false'
id: release
run: |
echo "RELEASE_NAME=$(gh release view --json name | jq -r .name)" >> $GITHUB_OUTPUT
echo "RELEASE_URL=$(gh release view --json url | jq -r .url)" >> $GITHUB_OUTPUT
- name: Notify SDK Slack Publish Success
if: ${{ success() && (steps.npm_release.conclusion == 'success' || steps.pre_release.conclusion == 'success') && github.event.inputs.dry_run == 'false' }}
uses: ./.github/actions/notify-slack-publish-status
with:
message: "✅ ${{ github.triggering_actor }} successfully published *Major* SDK version ${{steps.version.outputs.NEXT_VERSION}} to NPM.\n\nhttps://www.npmjs.com/package/@imtbl/sdk/v/${{steps.version.outputs.NEXT_VERSION}}"

- name: Notify SDK Slack Publish Failure
if: ${{ failure() && (steps.npm_release.conclusion == 'failure' || steps.pre_release.conclusion == 'failure' || steps.gh_release.conclusion == 'failure') && github.event.inputs.dry_run == 'false' }}
uses: ./.github/actions/notify-slack-publish-status
with:
message: "❌ Failed to publish SDK version ${{steps.version.outputs.NEXT_VERSION}} to NPM. Please check the logs for more details."

0 comments on commit cc63ac2

Please sign in to comment.