<NME> nightly-dev-release.yml
<BEF> name: Publish nightly dev releases

on:
  schedule:
    - cron: 0 2 * * * # Every day at 02:00
  workflow_dispatch: # Manually on demand

jobs:
  publish-config:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [14.x] # This should be LTS

    steps:
    - name: Checkout code
      uses: actions/checkout@v3
      with:
        fetch-depth: 0 # Fetch the history, or this action won't work

    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}

    - name: Detect changes (git)
      id: changes
      run: |
        # ===============================
        # Detect changes using git
        # ===============================

        LAST_TAG=$(git describe --abbrev=0 --tags)
        echo "Checking for changes since last tag $LAST_TAG"

        # Figure out if anything changed in the package directories
        CHANGES=$(git diff "$LAST_TAG" --name-only | grep -E "^packages\/" | grep -vE "^packages\/controller\/doc\/" || true)
        if [ -z "$CHANGES" ] ; then
          echo "🔸 No package changes since latest version, aborting..."
          echo "::set-output name=result::unchanged"
        else
          echo "::set-output name=result::ok"
        fi

    - name: Prepare installation
      if: steps.changes.outputs.result == 'ok'
      run: |
        sudo add-apt-repository ppa:chris-lea/redis-server -y
        sudo apt-get update -q
        sudo apt-get install redis-server redis-sentinel -y
        sudo systemctl start redis-server

    - name: Install dependencies
      if: steps.changes.outputs.result == 'ok'
      run: npm ci --ignore-scripts # install typescript and @types do not `setup first`

    - name: Build TS files
      if: steps.changes.outputs.result == 'ok'
      run: npm run build

    - name: Build Docs
      if: steps.changes.outputs.result == 'ok'
      run: npm run build:doc

    - name: Run scripts
      if: steps.changes.outputs.result == 'ok'
      id: version
      uses: actions/github-script@v5
      with:
        script: |
          const semver = require("semver");
          const now = new Date();
      if: steps.changes.outputs.result == 'ok'
      id: version
      uses: actions/github-script@v6
      with:
        result-encoding: string
        script: |
          const semver = require("semver");
          const now = new Date();
          const today = new Date(now.getTime() - now.getTimezoneOffset()*60000);
          const dateStr = today.toISOString().split("T")[0].replace(/-/g, "");
          const sha = require("child_process").execSync("git rev-parse --short HEAD").toString("utf8").trim();

          const prevVersion = require(`${process.env.GITHUB_WORKSPACE}/lerna.json`).version;

          const parsed = semver.parse(prevVersion);
          const prereleaseIdentifier = parsed.prerelease[0] || "alpha";

          for (let i = 1; i < parsed.prerelease.length; i++) {
            const part = parsed.prerelease[i];
            if (typeof part === "number") {
              continue;
            }
            // Parse stuff like `8-20210909-001a711c` back to `8`
            const numeric = parseInt(part);
            if (!Number.isNaN(numeric)) {
              parsed.prerelease[i] = numeric;
            }
          }

          // Figure out the next version
          const nightlyVersion =  `${semver.inc(parsed, "prerelease", prereleaseIdentifier)}-${dateStr}-${sha}`;

          // ensure that io-pack is in sync
          const fs = require('fs');
          const ioPack = JSON.parse(fs.readFileSync(`${process.env.GITHUB_WORKSPACE}/packages/controller/io-package.json`));
          ioPack.common.version = semver.inc(nightlyVersion, 'patch');;
          fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/packages/controller/io-package.json`, JSON.stringify(ioPack, null, 2));

          return nightlyVersion;

    - name: Bump version and publish
      if: steps.changes.outputs.result == 'ok'
      env:
        VERSION: ${{ steps.version.outputs.result }}
      run: |
        npm config set //registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}
        npm whoami

        git config --global user.email "moritz.heusinger@gmail.com"
        git config --global user.name "Github Action"

        git add .
        git commit -m "v${VERSION}" && npx lerna version ${VERSION} --exact --ignore-scripts --no-commit-hooks --yes --amend --force-publish || npx lerna version ${VERSION} --exact --ignore-scripts --no-commit-hooks --yes --force-publish
        npx lerna publish from-package --yes --dist-tag dev
        git push

    - name: Create Github Release
      if: steps.changes.outputs.result == 'ok'
      uses: ncipollo/release-action@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag: v${{ steps.version.outputs.result }}
        name: Release v${{ steps.version.outputs.result }}
        draft: false
        prerelease: true
        body: 'nightly release'

    - name: Notify Sentry.io about the release
      if: steps.changes.outputs.result == 'ok'
      env:
        VERSION: ${{ steps.version.outputs.result }}
      run: |
        npm i -g @sentry/cli
        export SENTRY_AUTH_TOKEN=${{ secrets.SENTRY_AUTH_TOKEN }}
        export SENTRY_URL=https://sentry.iobroker.net
        export SENTRY_ORG=iobroker
        export SENTRY_PROJECT=iobroker-js-controller
        export SENTRY_VERSION=iobroker.js-controller@${VERSION}
        sentry-cli releases new $SENTRY_VERSION
        sentry-cli releases set-commits $SENTRY_VERSION --auto
        sentry-cli releases finalize $SENTRY_VERSION

<MSG> fix: output bumped version as string, not JSON (#1519)


<DFF> @@ -68,6 +68,7 @@ jobs:
       id: version
       uses: actions/github-script@v5
       with:
+        result-encoding: string
         script: |
           const semver = require("semver");
           const now = new Date();

{'commit': 'diff --git a/.github/workflows/nightly-dev-release.yml b/.github/workflows/nightly-dev-release.yml\nindex 772432bc..0cee6028 100644\n--- a/.github/workflows/nightly-dev-release.yml\n+++ b/.github/workflows/nightly-dev-release.yml\n@@ -68,6 +68,7 @@ jobs:\n       id: version\n       uses: actions/github-script@v5\n       with:\n+        result-encoding: string\n         script: |\n           const semver = require("semver");\n           const now = new Date();\n', 'commit_subject': 'fix: output bumped version as string, not JSON (#1519)', 'repo_name': 'ioBroker/ioBroker.js-controller', 'committer': {'name': 'GitHub', 'email': '1505422b2465e9a84f6fdfaa161078890c593f06@github.com', 'time_sec': '1636964786', 'tz_offset': '60', 'date': {'seconds': '1636964786'}}, 'commit_tree': '0c7e45508db5ac0d404820fbd61d6ebb28d23cd1', 'commit_message': 'fix: output bumped version as string, not JSON (#1519)\n\n', 'language_name': 'Shell', 'license': 'mit', 'hunks': '@@ -68,6 +68,7 @@ jobs:\n       id: version\n       uses: actions/github-script@v5\n       with:\n+        result-encoding: string\n         script: |\n           const semver = require("semver");\n           const now = new Date();\n', 'addition_count': 1, 'deletion_count': 0, 'src_file': 'a/.github/workflows/nightly-dev-release.yml', 'tgt_file': 'b/.github/workflows/nightly-dev-release.yml', 'file_extension': '.yml', 'before_file': 'name: Publish nightly dev releases\n\non:\n  schedule:\n    - cron: 0 2 * * * # Every day at 02:00\n  workflow_dispatch: # Manually on demand\n\njobs:\n  publish-config:\n    runs-on: ubuntu-latest\n\n    strategy:\n      matrix:\n        node-version: [14.x] # This should be LTS\n\n    steps:\n    - name: Checkout code\n      uses: actions/checkout@v3\n      with:\n        fetch-depth: 0 # Fetch the history, or this action won\'t work\n\n    - name: Use Node.js ${{ matrix.node-version }}\n      uses: actions/setup-node@v3\n      with:\n        node-version: ${{ matrix.node-version }}\n\n    - name: Detect changes (git)\n      id: changes\n      run: |\n        # ===============================\n        # Detect changes using git\n        # ===============================\n\n        LAST_TAG=$(git describe --abbrev=0 --tags)\n        echo "Checking for changes since last tag $LAST_TAG"\n\n        # Figure out if anything changed in the package directories\n        CHANGES=$(git diff "$LAST_TAG" --name-only | grep -E "^packages\\/" | grep -vE "^packages\\/controller\\/doc\\/" || true)\n        if [ -z "$CHANGES" ] ; then\n          echo "🔸 No package changes since latest version, aborting..."\n          echo "::set-output name=result::unchanged"\n        else\n          echo "::set-output name=result::ok"\n        fi\n\n    - name: Prepare installation\n      if: steps.changes.outputs.result == \'ok\'\n      run: |\n        sudo add-apt-repository ppa:chris-lea/redis-server -y\n        sudo apt-get update -q\n        sudo apt-get install redis-server redis-sentinel -y\n        sudo systemctl start redis-server\n\n    - name: Install dependencies\n      if: steps.changes.outputs.result == \'ok\'\n      run: npm ci --ignore-scripts # install typescript and @types do not `setup first`\n\n    - name: Build TS files\n      if: steps.changes.outputs.result == \'ok\'\n      run: npm run build\n\n    - name: Build Docs\n      if: steps.changes.outputs.result == \'ok\'\n      run: npm run build:doc\n\n    - name: Run scripts\n      if: steps.changes.outputs.result == \'ok\'\n      id: version\n      uses: actions/github-script@v5\n      with:\n        script: |\n          const semver = require("semver");\n          const now = new Date();\n      if: steps.changes.outputs.result == \'ok\'\n      id: version\n      uses: actions/github-script@v6\n      with:\n        result-encoding: string\n        script: |\n          const semver = require("semver");\n          const now = new Date();\n          const today = new Date(now.getTime() - now.getTimezoneOffset()*60000);\n          const dateStr = today.toISOString().split("T")[0].replace(/-/g, "");\n          const sha = require("child_process").execSync("git rev-parse --short HEAD").toString("utf8").trim();\n\n          const prevVersion = require(`${process.env.GITHUB_WORKSPACE}/lerna.json`).version;\n\n          const parsed = semver.parse(prevVersion);\n          const prereleaseIdentifier = parsed.prerelease[0] || "alpha";\n\n          for (let i = 1; i < parsed.prerelease.length; i++) {\n            const part = parsed.prerelease[i];\n            if (typeof part === "number") {\n              continue;\n            }\n            // Parse stuff like `8-20210909-001a711c` back to `8`\n            const numeric = parseInt(part);\n            if (!Number.isNaN(numeric)) {\n              parsed.prerelease[i] = numeric;\n            }\n          }\n\n          // Figure out the next version\n          const nightlyVersion =  `${semver.inc(parsed, "prerelease", prereleaseIdentifier)}-${dateStr}-${sha}`;\n\n          // ensure that io-pack is in sync\n          const fs = require(\'fs\');\n          const ioPack = JSON.parse(fs.readFileSync(`${process.env.GITHUB_WORKSPACE}/packages/controller/io-package.json`));\n          ioPack.common.version = semver.inc(nightlyVersion, \'patch\');;\n          fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/packages/controller/io-package.json`, JSON.stringify(ioPack, null, 2));\n\n          return nightlyVersion;\n\n    - name: Bump version and publish\n      if: steps.changes.outputs.result == \'ok\'\n      env:\n        VERSION: ${{ steps.version.outputs.result }}\n      run: |\n        npm config set //registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}\n        npm whoami\n\n        git config --global user.email "moritz.heusinger@gmail.com"\n        git config --global user.name "Github Action"\n\n        git add .\n        git commit -m "v${VERSION}" && npx lerna version ${VERSION} --exact --ignore-scripts --no-commit-hooks --yes --amend --force-publish || npx lerna version ${VERSION} --exact --ignore-scripts --no-commit-hooks --yes --force-publish\n        npx lerna publish from-package --yes --dist-tag dev\n        git push\n\n    - name: Create Github Release\n      if: steps.changes.outputs.result == \'ok\'\n      uses: ncipollo/release-action@v1\n      env:\n        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}\n      with:\n        tag: v${{ steps.version.outputs.result }}\n        name: Release v${{ steps.version.outputs.result }}\n        draft: false\n        prerelease: true\n        body: \'nightly release\'\n\n    - name: Notify Sentry.io about the release\n      if: steps.changes.outputs.result == \'ok\'\n      env:\n        VERSION: ${{ steps.version.outputs.result }}\n      run: |\n        npm i -g @sentry/cli\n        export SENTRY_AUTH_TOKEN=${{ secrets.SENTRY_AUTH_TOKEN }}\n        export SENTRY_URL=https://sentry.iobroker.net\n        export SENTRY_ORG=iobroker\n        export SENTRY_PROJECT=iobroker-js-controller\n        export SENTRY_VERSION=iobroker.js-controller@${VERSION}\n        sentry-cli releases new $SENTRY_VERSION\n        sentry-cli releases set-commits $SENTRY_VERSION --auto\n        sentry-cli releases finalize $SENTRY_VERSION\n', 'message': 'fix: output bumped version as string, not JSON (#1519)\n\n', 'author_name': 'GitHub', 'lang': 'github/workflows/nightly-dev-release'}