Skip to content

Commit

Permalink
Prefer step outputs to env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Jan 17, 2020
1 parent bc40bab commit 059d68b
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 57 deletions.
29 changes: 23 additions & 6 deletions .github/workflows/checkin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
76 changes: 52 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
66 changes: 60 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 27 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
42 changes: 31 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 059d68b

Please sign in to comment.