diff --git a/.github/workflows/checkin.yml b/.github/workflows/checkin.yml index e9688ae3..a3de8630 100644 --- a/.github/workflows/checkin.yml +++ b/.github/workflows/checkin.yml @@ -31,19 +31,36 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - - name: nbgv -c -p src + - name: nbgv get-version -p src uses: ./ with: path: src - allVars: false + id: nbgv + - name: Print step outputs + run: | + echo $VersionJson + echo 'SemVer2: ${{ steps.nbgv.outputs.SemVer2 }}' + echo 'CloudBuildNumber: ${{ steps.nbgv.outputs.CloudBuildNumber }}' + if [ -z "${{ steps.nbgv.outputs.CloudBuildNumber }}" ]; then exit 1; fi + env: + VersionJson: ${{ toJson(steps.nbgv.outputs) }} + - name: nbgv cloud -c -p src + uses: ./ + with: + path: src + setCommonVars: true - name: Print env vars - run: gci env:NBGV_*,env:Git* + run: | + gci env:NBGV_*,env:Git* + if (-not $env:GitBuildVersion) { exit 1 } shell: pwsh - - name: nbgv -a -p src + - name: nbgv cloud -a -p src uses: ./ with: path: src - commonVars: false + setAllVars: true - name: Print env vars - run: gci env:NBGV_*,env:Git* + run: | + gci env:NBGV_*,env:Git* + if (-not $env:NBGV_Version) { exit 1 } shell: pwsh diff --git a/README.md b/README.md index 1c29feca..14fcad38 100644 --- a/README.md +++ b/README.md @@ -14,41 +14,69 @@ This sets many environment variables to the various forms of the version for you ## Inputs -### path +All inputs are optional. + +|Name|Default|Description +|--|--|--| +`path`|Repo root|The path to the directory for which the version should be determined. This should be at or below the directory containing the version.json file. +`setCommonVars`|false|Defines a few common version variables as environment variables, with a "Git" prefix (e.g. GitBuildVersion, GitBuildVersionSimple, GitAssemblyInformationalVersion). Adds the `--common-vars` switch to the `nbgv cloud` command. +`setAllVars`|false|Defines ALL version variables as environment variables, with a "NBGV_" prefix. Adds the `--all-vars` switch to the `nbgv cloud` command. +`toolVersion`|latest stable|The version of the nbgv dotnet CLI tool to install and use. If not specified, the default is the latest stable version. + +## Outputs + +Name | Description +--|-- +CloudBuildNumber|The cloud build number +VersionFileFound|A boolean value indicating whether a version.json file was found. +AssemblyVersion|The version to be used as the .NET assembly version. +AssemblyFileVersion|The version to be used as the .NET assembly file version. +AssemblyInformationalVersion|The version to be used as the .NET assembly informational version. +PublicRelease|A boolean value indicating whether this build is recognized as building from a public release branch. +PrereleaseVersion|The prerelease/unstable suffix to the version, including the hyphen. +PrereleaseVersionNoLeadingHyphen|The prerelease/unstable suffix to the version, without the leading hyphen. +SimpleVersion|The first 3 integers of the version. +BuildNumber|The build number (i.e. the third integer or PATCH) for this version. +VersionRevision|The fourth integer component of the version. +MajorMinorVersion|The "major.minor" portion of the version. +VersionMajor|The first integer of the version. +VersionMinor|The second integer of the version. +GitCommitId|The full SHA1 hash of the HEAD commit. +GitCommitIdShort|A truncated SHA1 hash of the HEAD commit (usually 10 characters) +GitCommitDate|The date of the git commit at HEAD +VersionHeight|The number of commits in the longest single path between the specified commit and the most distant ancestor (inclusive) that set the version to the value at HEAD. +VersionHeightOffset|The offset to add to VersionHeight when calculating the BuildNumber or wherever else the VersionHeight is used. +Version|The four integer version. +BuildMetadataFragment|The +metadata portion of the version, if any. +NuGetPackageVersion|The version to be used for NuGet packages. +ChocolateyPackageVersion|The version to be used for Chocolatey packages. +NpmPackageVersion|The version to be used for NPM packages. +SemVer1|The SemVer 1.0 compliant version. +SemVer2|The SemVer 2.0 compliant version. -**Optional** The path to the directory for which the version should be determined. This should be at or below the directory containing the version.json file. Default is repo root directory. - -### commonVars - -**Optional** Defines a few common version variables as cloud build variables, with a "Git" prefix (e.g. GitBuildVersion, GitBuildVersionSimple, GitAssemblyInformationalVersion). - -Adds the `--common-vars` switch to the `nbgv cloud` command. - -Default value is `true`. - -### allVars - -**Optional** Defines ALL version variables as cloud build variables, with a "NBGV_" prefix. - -Adds the `--all-vars` switch to the `nbgv cloud` command. - -Default value is `true`. +## Example usage -### toolVersion +### Using step outputs -**Optional** The version of the nbgv dotnet CLI tool to install and use. If not specified, the default is the latest stable version. +```yaml +- uses: aarnott/nbgv@v0.3-beta + id: nbgv +- run: echo 'SemVer2: ${{ steps.nbgv.outputs.SemVer2 }}' +``` -## Example usage +### Using environment variables -``` yaml -- uses: aarnott/nbgv@master +```yaml +- uses: aarnott/nbgv@v0.3-beta with: - path: src # optional path to directory to compute version for + setAllVars: true - run: echo "NBGV_SemVer2 $NBGV_SemVer2" ``` The action runs in about 13 seconds. +## Checkout requirements + Git history based versioning tools rely on history being included in the clone. `actions/checkout@v1` does this by default. But if you're using `actions/checkout@v2` you'll need to specify deep clone: diff --git a/action.yml b/action.yml index 38318ae5..23db3f5b 100644 --- a/action.yml +++ b/action.yml @@ -9,17 +9,71 @@ inputs: description: The path to the directory for which the version should be determined. This should be at or below the directory containing the version.json file. Default is repo root directory. required: false default: '.' - allVars: - description: Defines ALL version variables as cloud build variables, with a "NBGV_" prefix. + setAllVars: + description: Defines ALL version variables as environment variables, with a "NBGV_" prefix. required: false - default: 'true' - commonVars: - description: Defines a few common version variables as cloud build variables, with a "Git" prefix (e.g. GitBuildVersion, GitBuildVersionSimple, GitAssemblyInformationalVersion). + default: false + setCommonVars: + description: Defines a few common version variables as environment variables, with a "Git" prefix (e.g. GitBuildVersion, GitBuildVersionSimple, GitAssemblyInformationalVersion). required: false - default: 'true' + default: false toolVersion: description: The version of the nbgv dotnet CLI tool to install and use. If not specified, the default is the latest stable version. required: false +outputs: + CloudBuildNumber: + description: The cloud build number + VersionFileFound: + description: A boolean value indicating whether a version.json file was found. + AssemblyVersion: + description: The version to be used as the .NET assembly version. + AssemblyFileVersion: + description: The version to be used as the .NET assembly file version. + AssemblyInformationalVersion: + description: The version to be used as the .NET assembly informational version. + PublicRelease: + description: A boolean value indicating whether this build is recognized as building from a public release branch. + PrereleaseVersion: + description: The prerelease/unstable suffix to the version, including the hyphen. + PrereleaseVersionNoLeadingHyphen: + description: The prerelease/unstable suffix to the version, without the leading hyphen. + SimpleVersion: + description: The first 3 integers of the version. + BuildNumber: + description: The build number (i.e. the third integer or PATCH) for this version. + VersionRevision: + description: The fourth integer component of the version. + MajorMinorVersion: + description: The "major.minor" portion of the version. + VersionMajor: + description: The first integer of the version. + VersionMinor: + description: The second integer of the version. + GitCommitId: + description: The full SHA1 hash of the HEAD commit. + GitCommitIdShort: + description: A truncated SHA1 hash of the HEAD commit (usually 10 characters) + GitCommitDate: + description: The date of the git commit at HEAD + VersionHeight: + description: The number of commits in the longest single path between the specified commit and the most distant ancestor (inclusive) that set the version to the value at HEAD. + VersionHeightOffset: + description: The offset to add to VersionHeight when calculating the BuildNumber or wherever else the VersionHeight is used. + Version: + description: The four integer version. + BuildMetadataFragment: + description: The +metadata portion of the version, if any. + NuGetPackageVersion: + description: The version to be used for NuGet packages. + ChocolateyPackageVersion: + description: The version to be used for Chocolatey packages. + NpmPackageVersion: + description: The version to be used for NPM packages. + SemVer1: + description: The SemVer 1.0 compliant version. + SemVer2: + description: The SemVer 2.0 compliant version. + runs: using: node12 main: dist/index.js diff --git a/dist/index.js b/dist/index.js index a354d94a..00fe5c0a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -977,9 +977,12 @@ const path = __importStar(__webpack_require__(622)); function run() { return __awaiter(this, void 0, void 0, function* () { try { + const toolVersion = core.getInput('toolVersion'); + const dir_path = core.getInput('path'); + const setCommonVars = core.getInput('setCommonVars') === 'true'; + const setAllVars = core.getInput('setAllVars') === 'true'; // install nbgv let installArgs = ['tool', 'install', '-g', 'nbgv']; - const toolVersion = core.getInput('toolVersion'); if (toolVersion) { installArgs.push('--version', toolVersion); } @@ -989,20 +992,34 @@ function run() { } // add .dotnet/tools to the path core.addPath(path.join(os.homedir(), '.dotnet', 'tools')); - // run nbgv - let jsonStr = ''; - let args = ['cloud']; - const dir_path = core.getInput('path'); + // Collect a JSON string of all the version properties. + let args = ['get-version', '-f', 'json']; if (dir_path) { args.push('-p', dir_path); } - if (core.getInput('commonVars') === 'true') { - args.push('-c'); + let versionJson = ''; + yield exec.exec('nbgv', args, { listeners: { stdout: (data) => { versionJson += data.toString(); } } }); + core.setOutput('versionJson', versionJson); + // Break up the JSON into individual outputs. + const versionProperties = JSON.parse(versionJson); + for (let name in versionProperties.CloudBuildAllVars) { + // Trim off the leading NBGV_ + core.setOutput(name.substring(5), versionProperties.CloudBuildAllVars[name]); } - if (core.getInput('allVars') === 'true') { - args.push('-a'); + // Set environment variables if desired. + if (setCommonVars || setAllVars) { + args = ['cloud']; + if (dir_path) { + args.push('-p', dir_path); + } + if (setCommonVars) { + args.push('-c'); + } + if (setAllVars) { + args.push('-a'); + } + yield exec.exec('nbgv', args); } - yield exec.exec('nbgv', args); } catch (error) { core.setFailed(error.message); diff --git a/src/main.ts b/src/main.ts index b10ba982..fa42eb27 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,9 +5,13 @@ import * as path from 'path' async function run() { try { + const toolVersion = core.getInput('toolVersion'); + const dir_path = core.getInput('path'); + const setCommonVars = core.getInput('setCommonVars') === 'true'; + const setAllVars = core.getInput('setAllVars') === 'true'; + // install nbgv let installArgs = ['tool', 'install', '-g', 'nbgv']; - const toolVersion = core.getInput('toolVersion'); if (toolVersion) { installArgs.push('--version', toolVersion); } @@ -20,21 +24,37 @@ async function run() { // add .dotnet/tools to the path core.addPath(path.join(os.homedir(), '.dotnet', 'tools')); - // run nbgv - let jsonStr = ''; - let args = ['cloud']; - const dir_path = core.getInput('path'); + // Collect a JSON string of all the version properties. + let args = ['get-version', '-f', 'json']; if (dir_path) { args.push('-p', dir_path); } - if (core.getInput('commonVars') === 'true') { - args.push('-c'); - } - if (core.getInput('allVars') === 'true') { - args.push('-a'); + let versionJson = ''; + await exec.exec('nbgv', args, { listeners: { stdout: (data: Buffer) => { versionJson += data.toString() } } }); + core.setOutput('versionJson', versionJson); + + // Break up the JSON into individual outputs. + const versionProperties = JSON.parse(versionJson); + for (let name in versionProperties.CloudBuildAllVars) { + // Trim off the leading NBGV_ + core.setOutput(name.substring(5), versionProperties.CloudBuildAllVars[name]); } - await exec.exec('nbgv', args); + // Set environment variables if desired. + if (setCommonVars || setAllVars) { + args = ['cloud']; + if (dir_path) { + args.push('-p', dir_path); + } + if (setCommonVars) { + args.push('-c'); + } + if (setAllVars) { + args.push('-a'); + } + + await exec.exec('nbgv', args); + } } catch (error) { core.setFailed(error.message); }