From 706c0ef960497657ddf0655d0a69732323d93238 Mon Sep 17 00:00:00 2001 From: cycjimmy Date: Sat, 21 Mar 2020 16:47:29 +0800 Subject: [PATCH] feat(branches): support branches of semantic-release v16+ support branches of semantic-release v16+ #24 #25 --- .github/workflows/testRelease.yml | 49 ++++++++++++++++++++++++++++--- README.md | 3 +- action.yml | 13 ++++++-- package-lock.json | 11 +++++-- package.json | 3 +- src/handleOptions.js | 35 +++++++++++++++------- src/index.js | 4 +-- src/inputs.json | 3 +- 8 files changed, 96 insertions(+), 25 deletions(-) diff --git a/.github/workflows/testRelease.yml b/.github/workflows/testRelease.yml index baf7e9c3..c78f3c54 100644 --- a/.github/workflows/testRelease.yml +++ b/.github/workflows/testRelease.yml @@ -9,22 +9,63 @@ on: - cron: 0 2 * * 0 jobs: - test: - name: release + test-semantic-latest: + name: test-semantic-latest runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 - name: Semantic Release - uses: cycjimmy/semantic-release-action@v2 + uses: ./ id: semantic with: + dry_run: true + branches: | + [ + 'master', + {name: 'beta', prerelease: true}, + {name: 'alpha', prerelease: true} + ] branch: master extra_plugins: | @semantic-release/git @semantic-release/changelog + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + + - name: Test Outputs + if: steps.semantic.outputs.new_release_published == 'true' + run: | + echo ${{ steps.semantic.outputs.new_release_version }} + echo ${{ steps.semantic.outputs.new_release_major_version }} + echo ${{ steps.semantic.outputs.new_release_minor_version }} + echo ${{ steps.semantic.outputs.new_release_patch_version }} + + test-semantic-v15: + name: test-semantic-v15 + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Semantic Release + uses: ./ + id: semantic_v15 + with: + semantic_version: 15 dry_run: true + branches: | + [ + 'master', + {name: 'beta', prerelease: true}, + {name: 'alpha', prerelease: true} + ] + branch: master + extra_plugins: | + @semantic-release/git@7 + @semantic-release/changelog@3 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }} @@ -36,5 +77,5 @@ jobs: echo ${{ steps.semantic.outputs.new_release_major_version }} echo ${{ steps.semantic.outputs.new_release_minor_version }} echo ${{ steps.semantic.outputs.new_release_patch_version }} - echo ${{ steps.semantic.outputs.new_release_notes }} + diff --git a/README.md b/README.md index 3819ffd6..df810bf1 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,9 @@ GitHub Action for [Semantic Release](https://github.com/semantic-release/semanti #### Step3: Add a [Workflow File](https://help.github.com/en/articles/workflow-syntax-for-github-actions) to your repository to create custom automated processes. * inputs: - * `branch`: [Optional] The branch on which releases should happen. It will override the branch attribute in your configuration file. If the attribute is not configured on both sides, the default is master. * `semantic_version`: [Optional] Specify specifying version range for semantic-release. If no version range is specified, latest version will be used by default. + * `branches`: [Optional] The branches on which releases should happen. It will override the branches attribute in your configuration file. If the attribute is not configured on both sides, the default is `['+([0-9])?(.{+([0-9]),x}).x', 'master', 'next', 'next-major', {name: 'beta', prerelease: true}, {name: 'alpha', prerelease: true}]`. **Support for semantic-release above v16**. See https://semantic-release.gitbook.io/semantic-release/usage/configuration#branches for more information. + * `branch`: [Optional] The branch on which releases should happen. It will override the branch attribute in your configuration file. If the attribute is not configured on both sides, the default is master. **Support for semantic-release older than v16**. * `extra_plugins`: [Optional] Extra plugins for pre-install. You can also specify specifying version range for the extra plugins if you prefer. * `dry_run`: [Optional] Whether to run semantic release in `dry-run` mode. It will override the dryRun attribute in your configuration file. * outputs: diff --git a/action.yml b/action.yml index d8c808ea..07f84f67 100644 --- a/action.yml +++ b/action.yml @@ -5,13 +5,20 @@ branding: icon: 'package' color: 'orange' inputs: - branch: - description: 'The branch on which releases should happen. It will override the branch attribute in your configuration file. If the attribute is not configured on both sides, the default is master.' semantic_version: - description: 'Specify specifying version range for semantic-release. If no version range is specified, semantic-release@^15 will be used by default.' + required: false + description: 'Specify specifying version range for semantic-release. If no version range is specified, latest version will be used by default' + branches: + required: false + description: 'The branches on which releases should happen. It will override the branches attribute in your configuration file. Support for semantic-release above v16. See https://semantic-release.gitbook.io/semantic-release/usage/configuration#branches for more information.' + branch: + required: false + description: 'The branch on which releases should happen. It will override the branch attribute in your configuration file. If the attribute is not configured on both sides, the default is master. Support for semantic-release older than v16.' extra_plugins: + required: false description: 'Extra plugins for pre-install. You can also specify specifying version range for the extra plugins if you prefer.' dry_run: + required: false description: 'Whether to run semantic release in `dry-run` mode. It will override the dryRun attribute in your configuration file' outputs: new_release_published: diff --git a/package-lock.json b/package-lock.json index a65d5728..b8262dd3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,9 +5,14 @@ "requires": true, "dependencies": { "@actions/core": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.2.0.tgz", - "integrity": "sha512-ZKdyhlSlyz38S6YFfPnyNgCDZuAF2T0Qv5eHflNWytPS8Qjvz39bZFMry9Bb/dpSnqWcNeav5yM2CTYpJeY+Dw==" + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.2.3.tgz", + "integrity": "sha512-Wp4xnyokakM45Uuj4WLUxdsa8fJjKVl1fDTsPbTEcTcuu0Nb26IPQbOtjmnfaCPGcaoPOOqId8H9NapZ8gii4w==" + }, + "@cycjimmy/awesome-js-funcs": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/@cycjimmy/awesome-js-funcs/-/awesome-js-funcs-2.3.0.tgz", + "integrity": "sha512-HXJrv0S1gzjP9h2cmE57m+0XnVsGIsidPEjxUR5i50iGIxbjEe1EhL6iSh2P1LXdjjbt1U/UHj6n93yzDwt1gw==" } } } diff --git a/package.json b/package.json index f2308db4..e8ca28e2 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ }, "homepage": "https://github.com/cycjimmy/semantic-release-action#readme", "dependencies": { - "@actions/core": "^1.2.0" + "@actions/core": "^1.2.3", + "@cycjimmy/awesome-js-funcs": "^2.3.0" } } diff --git a/src/handleOptions.js b/src/handleOptions.js index 33307086..83fde149 100644 --- a/src/handleOptions.js +++ b/src/handleOptions.js @@ -1,29 +1,44 @@ const core = require('@actions/core'); +const stringToJson = require('@cycjimmy/awesome-js-funcs/typeConversion/stringToJson').default; const inputs = require('./inputs.json'); /** - * Handle Branch Option + * Handle Branches Option * @returns {{}|{branch: string}} */ -exports.handleBranchOption = () => { - const branchOption = {}; +exports.handleBranchesOption = () => { + const branchesOption = {}; + const branches = core.getInput(inputs.branches); const branch = core.getInput(inputs.branch); - if (!branch) { - return branchOption; - } + core.debug(`branches input: ${branches}`); + core.debug(`branch input: ${branch}`); const semanticVersion = require('semantic-release/package.json').version; const semanticMajorVersion = Number(semanticVersion.replace(/\..+/g, '')); core.debug(`semanticMajorVersion: ${semanticMajorVersion}`); + // older than v16 if (semanticMajorVersion < 16) { - branchOption.branch = branch; - } else { - branchOption.branches = [branch]; + if (!branch) { + return branchesOption; + } + + branchesOption.branch = branch; + return branchesOption; + } + + // above v16 + const strNeedConvertToJson = branches || branch || ''; + + if (!strNeedConvertToJson) { + return branchesOption; } - return branchOption; + const jsonOrStr = stringToJson('' + strNeedConvertToJson); + core.debug(`Converted branches attribute: ${JSON.stringify(jsonOrStr)}`); + branchesOption.branches = jsonOrStr; + return branchesOption; }; /** diff --git a/src/index.js b/src/index.js index 90ff1056..99e8d1f5 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,5 @@ const core = require('@actions/core'); -const {handleBranchOption, handleDryRunOption} = require('./handleOptions'); +const {handleBranchesOption, handleDryRunOption} = require('./handleOptions'); const setUpJob = require('./setUpJob.task'); const installSpecifyingVersionSemantic = require('./installSpecifyingVersionSemantic.task'); const preInstallPlugins = require('./preInstallPlugins.task'); @@ -17,7 +17,7 @@ const release = async () => { const semanticRelease = require('semantic-release'); const result = await semanticRelease({ - ...(handleBranchOption()), + ...(handleBranchesOption()), ...(handleDryRunOption()), }); diff --git a/src/inputs.json b/src/inputs.json index 5f322261..09dbd156 100644 --- a/src/inputs.json +++ b/src/inputs.json @@ -1,6 +1,7 @@ { - "branch": "branch", "semantic_version": "semantic_version", + "branches": "branches", + "branch": "branch", "extra_plugins": "extra_plugins", "dry_run": "dry_run" }