From dca841b984ba7ef4a4e3dcc182d4f2314c3d9411 Mon Sep 17 00:00:00 2001
From: Michael Kriese <michael.kriese@visualon.de>
Date: Thu, 22 Apr 2021 08:26:02 +0200
Subject: [PATCH] build: release workflow update (#9677)

---
 .github/workflows/build.yml       | 23 ++++++++++++++++---
 .github/workflows/release-npm.yml | 37 +++++++++++++++++++++++++------
 2 files changed, 50 insertions(+), 10 deletions(-)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 144292f75b..7dccfacde6 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -7,12 +7,19 @@ on:
       - v24
 
   workflow_dispatch:
+    inputs:
+      dryRun:
+        description: 'Dry-Run'
+        default: 'true'
 
 env:
+  # Currently no way to detect automatically (#8153)
+  DEFAULT_BRANCH: master
   YARN_MODULES_CACHE_KEY: v1
   YARN_PACKAGE_CACHE_KEY: v1
   YARN_CACHE_FOLDER: .cache/yarn
   NODE_VERSION: 14
+  DRY_RUN: true
 
 jobs:
   test:
@@ -98,7 +105,8 @@ jobs:
         uses: codecov/codecov-action@v1.4.1
         if: always() && env.coverage == 'true'
 
-      # build after tests to exclude files
+      # build after tests to exclude build files from tests
+      # TODO: check if build before test speeds up tests
       - name: Build
         run: yarn build
 
@@ -179,6 +187,16 @@ jobs:
           yarn config set version-git-tag false
           npm config set scripts-prepend-node-path true
 
+      - name: Check dry run
+        run: |
+          if [[ "${{github.event_name}}" == "workflow_dispatch" && "${{ github.event.inputs.dryRun }}" != "true"  ]]; then
+            echo "DRY_RUN=false" >> $GITHUB_ENV
+          elif [[ "${{github.ref}}" == "refs/heads/${{env.DEFAULT_BRANCH}}" ]]; then
+            echo "DRY_RUN=false" >> $GITHUB_ENV
+          elif [[ "${{github.ref}}" =~ ^refs/heads/v[0-9]+(\.[0-9]+)?$ ]]; then
+            echo "DRY_RUN=false" >> $GITHUB_ENV
+          fi
+
       # full checkout for semantic-release
       - uses: actions/checkout@v2.3.4
         with:
@@ -194,10 +212,9 @@ jobs:
         run: yarn install --frozen-lockfile
 
       - name: semantic-release
-        if: github.ref == 'refs/heads/master'
         run: |
           echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' >> ~/.npmrc
-          npx semantic-release --dry-run ${{github.ref != 'refs/heads/master'}} --ci ${{github.ref == 'refs/heads/master'}}
+          npx semantic-release --dry-run ${{env.DRY_RUN}}
         env:
           GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
           NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
diff --git a/.github/workflows/release-npm.yml b/.github/workflows/release-npm.yml
index b7286c4738..c718fb45dc 100644
--- a/.github/workflows/release-npm.yml
+++ b/.github/workflows/release-npm.yml
@@ -1,13 +1,29 @@
 name: release-npm
+
 on:
   repository_dispatch:
     types: [renovate-release]
 
+  workflow_dispatch:
+    inputs:
+      sha:
+        description: 'Git sha to checkout'
+        required: true
+      version:
+        description: 'Version to release'
+        required: true
+      tag:
+        description: 'Npm dist-tag'
+        default: 'latest'
+
 env:
   YARN_MODULES_CACHE_KEY: v1
   YARN_PACKAGE_CACHE_KEY: v1
   YARN_CACHE_FOLDER: .cache/yarn
   NODE_VERSION: 14
+  GIT_SHA: ${{ github.event.client_payload.sha }}
+  NPM_VERSION: ${{ github.event.client_payload.version }}
+  NPM_TAG: ${{ github.event.client_payload.tag }}
 
 jobs:
   release-npm:
@@ -27,9 +43,17 @@ jobs:
           yarn config set version-git-tag false
           npm config set scripts-prepend-node-path true
 
+      - name: Prepare env
+        run: |
+          if [[ "${{github.event_name}}" == "workflow_dispatch" ]]; then
+            echo "GIT_SHA=${{ github.event.inputs.sha }}" >> $GITHUB_ENV
+            echo "NPM_VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
+            echo "NPM_TAG=${{ github.event.inputs.tag }}" >> $GITHUB_ENV
+          fi
+
       - uses: actions/checkout@v2.3.4
         with:
-          ref: ${{ github.event.client_payload.sha }}
+          ref: ${{ env.GIT_SHA }}
 
       - name: Cache Yarn packages
         id: yarn_cache_packages
@@ -41,17 +65,16 @@ jobs:
       - name: Installing dependencies
         run: yarn install --frozen-lockfile
 
-      - name: Build ${{ github.event.client_payload.version }}
+      - name: Build ${{ env.NPM_VERSION }}
         run: yarn build
 
-      - name: Prepare ${{ github.event.client_payload.version }}
+      - name: Prepare  ${{ env.NPM_VERSION }}
         run: |
-          npm --no-git-tag-version version ${{ github.event.client_payload.version }}
+          npm --no-git-tag-version version ${{ env.NPM_VERSION }}
 
-      - name: Publish ${{ github.event.client_payload.version }}
+      - name: Publish ${{ env.NPM_VERSION }}
         run: |
           echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' >> ~/.npmrc
-          npm publish --tag ${TAG:-latest}
+          npm publish --tag ${NPM_TAG:-latest}
         env:
           NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
-          TAG: ${{ github.event.client_payload.tag }}
-- 
GitLab