Skip to content

Commit 059d68b

Browse files
committed
Prefer step outputs to env vars
1 parent bc40bab commit 059d68b

File tree

5 files changed

+193
-57
lines changed

5 files changed

+193
-57
lines changed

.github/workflows/checkin.yml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,36 @@ jobs:
3131
runs-on: ubuntu-latest
3232
steps:
3333
- uses: actions/checkout@v1
34-
- name: nbgv -c -p src
34+
- name: nbgv get-version -p src
3535
uses: ./
3636
with:
3737
path: src
38-
allVars: false
38+
id: nbgv
39+
- name: Print step outputs
40+
run: |
41+
echo $VersionJson
42+
echo 'SemVer2: ${{ steps.nbgv.outputs.SemVer2 }}'
43+
echo 'CloudBuildNumber: ${{ steps.nbgv.outputs.CloudBuildNumber }}'
44+
if [ -z "${{ steps.nbgv.outputs.CloudBuildNumber }}" ]; then exit 1; fi
45+
env:
46+
VersionJson: ${{ toJson(steps.nbgv.outputs) }}
47+
- name: nbgv cloud -c -p src
48+
uses: ./
49+
with:
50+
path: src
51+
setCommonVars: true
3952
- name: Print env vars
40-
run: gci env:NBGV_*,env:Git*
53+
run: |
54+
gci env:NBGV_*,env:Git*
55+
if (-not $env:GitBuildVersion) { exit 1 }
4156
shell: pwsh
42-
- name: nbgv -a -p src
57+
- name: nbgv cloud -a -p src
4358
uses: ./
4459
with:
4560
path: src
46-
commonVars: false
61+
setAllVars: true
4762
- name: Print env vars
48-
run: gci env:NBGV_*,env:Git*
63+
run: |
64+
gci env:NBGV_*,env:Git*
65+
if (-not $env:NBGV_Version) { exit 1 }
4966
shell: pwsh

README.md

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,69 @@ This sets many environment variables to the various forms of the version for you
1414

1515
## Inputs
1616

17-
### path
17+
All inputs are optional.
18+
19+
|Name|Default|Description
20+
|--|--|--|
21+
`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.
22+
`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.
23+
`setAllVars`|false|Defines ALL version variables as environment variables, with a "NBGV_" prefix. Adds the `--all-vars` switch to the `nbgv cloud` command.
24+
`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.
25+
26+
## Outputs
27+
28+
Name | Description
29+
--|--
30+
CloudBuildNumber|The cloud build number
31+
VersionFileFound|A boolean value indicating whether a version.json file was found.
32+
AssemblyVersion|The version to be used as the .NET assembly version.
33+
AssemblyFileVersion|The version to be used as the .NET assembly file version.
34+
AssemblyInformationalVersion|The version to be used as the .NET assembly informational version.
35+
PublicRelease|A boolean value indicating whether this build is recognized as building from a public release branch.
36+
PrereleaseVersion|The prerelease/unstable suffix to the version, including the hyphen.
37+
PrereleaseVersionNoLeadingHyphen|The prerelease/unstable suffix to the version, without the leading hyphen.
38+
SimpleVersion|The first 3 integers of the version.
39+
BuildNumber|The build number (i.e. the third integer or PATCH) for this version.
40+
VersionRevision|The fourth integer component of the version.
41+
MajorMinorVersion|The "major.minor" portion of the version.
42+
VersionMajor|The first integer of the version.
43+
VersionMinor|The second integer of the version.
44+
GitCommitId|The full SHA1 hash of the HEAD commit.
45+
GitCommitIdShort|A truncated SHA1 hash of the HEAD commit (usually 10 characters)
46+
GitCommitDate|The date of the git commit at HEAD
47+
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.
48+
VersionHeightOffset|The offset to add to VersionHeight when calculating the BuildNumber or wherever else the VersionHeight is used.
49+
Version|The four integer version.
50+
BuildMetadataFragment|The +metadata portion of the version, if any.
51+
NuGetPackageVersion|The version to be used for NuGet packages.
52+
ChocolateyPackageVersion|The version to be used for Chocolatey packages.
53+
NpmPackageVersion|The version to be used for NPM packages.
54+
SemVer1|The SemVer 1.0 compliant version.
55+
SemVer2|The SemVer 2.0 compliant version.
1856

19-
**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.
20-
21-
### commonVars
22-
23-
**Optional** Defines a few common version variables as cloud build variables, with a "Git" prefix (e.g. GitBuildVersion, GitBuildVersionSimple, GitAssemblyInformationalVersion).
24-
25-
Adds the `--common-vars` switch to the `nbgv cloud` command.
26-
27-
Default value is `true`.
28-
29-
### allVars
30-
31-
**Optional** Defines ALL version variables as cloud build variables, with a "NBGV_" prefix.
32-
33-
Adds the `--all-vars` switch to the `nbgv cloud` command.
34-
35-
Default value is `true`.
57+
## Example usage
3658

37-
### toolVersion
59+
### Using step outputs
3860

39-
**Optional** The version of the nbgv dotnet CLI tool to install and use. If not specified, the default is the latest stable version.
61+
```yaml
62+
- uses: aarnott/nbgv@v0.3-beta
63+
id: nbgv
64+
- run: echo 'SemVer2: ${{ steps.nbgv.outputs.SemVer2 }}'
65+
```
4066
41-
## Example usage
67+
### Using environment variables
4268
43-
``` yaml
44-
- uses: aarnott/nbgv@master
69+
```yaml
70+
- uses: aarnott/nbgv@v0.3-beta
4571
with:
46-
path: src # optional path to directory to compute version for
72+
setAllVars: true
4773
- run: echo "NBGV_SemVer2 $NBGV_SemVer2"
4874
```
4975
5076
The action runs in about 13 seconds.
5177
78+
## Checkout requirements
79+
5280
Git history based versioning tools rely on history being included in the clone.
5381
`actions/checkout@v1` does this by default.
5482
But if you're using `actions/checkout@v2` you'll need to specify deep clone:

action.yml

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,71 @@ inputs:
99
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.
1010
required: false
1111
default: '.'
12-
allVars:
13-
description: Defines ALL version variables as cloud build variables, with a "NBGV_" prefix.
12+
setAllVars:
13+
description: Defines ALL version variables as environment variables, with a "NBGV_" prefix.
1414
required: false
15-
default: 'true'
16-
commonVars:
17-
description: Defines a few common version variables as cloud build variables, with a "Git" prefix (e.g. GitBuildVersion, GitBuildVersionSimple, GitAssemblyInformationalVersion).
15+
default: false
16+
setCommonVars:
17+
description: Defines a few common version variables as environment variables, with a "Git" prefix (e.g. GitBuildVersion, GitBuildVersionSimple, GitAssemblyInformationalVersion).
1818
required: false
19-
default: 'true'
19+
default: false
2020
toolVersion:
2121
description: The version of the nbgv dotnet CLI tool to install and use. If not specified, the default is the latest stable version.
2222
required: false
23+
outputs:
24+
CloudBuildNumber:
25+
description: The cloud build number
26+
VersionFileFound:
27+
description: A boolean value indicating whether a version.json file was found.
28+
AssemblyVersion:
29+
description: The version to be used as the .NET assembly version.
30+
AssemblyFileVersion:
31+
description: The version to be used as the .NET assembly file version.
32+
AssemblyInformationalVersion:
33+
description: The version to be used as the .NET assembly informational version.
34+
PublicRelease:
35+
description: A boolean value indicating whether this build is recognized as building from a public release branch.
36+
PrereleaseVersion:
37+
description: The prerelease/unstable suffix to the version, including the hyphen.
38+
PrereleaseVersionNoLeadingHyphen:
39+
description: The prerelease/unstable suffix to the version, without the leading hyphen.
40+
SimpleVersion:
41+
description: The first 3 integers of the version.
42+
BuildNumber:
43+
description: The build number (i.e. the third integer or PATCH) for this version.
44+
VersionRevision:
45+
description: The fourth integer component of the version.
46+
MajorMinorVersion:
47+
description: The "major.minor" portion of the version.
48+
VersionMajor:
49+
description: The first integer of the version.
50+
VersionMinor:
51+
description: The second integer of the version.
52+
GitCommitId:
53+
description: The full SHA1 hash of the HEAD commit.
54+
GitCommitIdShort:
55+
description: A truncated SHA1 hash of the HEAD commit (usually 10 characters)
56+
GitCommitDate:
57+
description: The date of the git commit at HEAD
58+
VersionHeight:
59+
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.
60+
VersionHeightOffset:
61+
description: The offset to add to VersionHeight when calculating the BuildNumber or wherever else the VersionHeight is used.
62+
Version:
63+
description: The four integer version.
64+
BuildMetadataFragment:
65+
description: The +metadata portion of the version, if any.
66+
NuGetPackageVersion:
67+
description: The version to be used for NuGet packages.
68+
ChocolateyPackageVersion:
69+
description: The version to be used for Chocolatey packages.
70+
NpmPackageVersion:
71+
description: The version to be used for NPM packages.
72+
SemVer1:
73+
description: The SemVer 1.0 compliant version.
74+
SemVer2:
75+
description: The SemVer 2.0 compliant version.
76+
2377
runs:
2478
using: node12
2579
main: dist/index.js

dist/index.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -977,9 +977,12 @@ const path = __importStar(__webpack_require__(622));
977977
function run() {
978978
return __awaiter(this, void 0, void 0, function* () {
979979
try {
980+
const toolVersion = core.getInput('toolVersion');
981+
const dir_path = core.getInput('path');
982+
const setCommonVars = core.getInput('setCommonVars') === 'true';
983+
const setAllVars = core.getInput('setAllVars') === 'true';
980984
// install nbgv
981985
let installArgs = ['tool', 'install', '-g', 'nbgv'];
982-
const toolVersion = core.getInput('toolVersion');
983986
if (toolVersion) {
984987
installArgs.push('--version', toolVersion);
985988
}
@@ -989,20 +992,34 @@ function run() {
989992
}
990993
// add .dotnet/tools to the path
991994
core.addPath(path.join(os.homedir(), '.dotnet', 'tools'));
992-
// run nbgv
993-
let jsonStr = '';
994-
let args = ['cloud'];
995-
const dir_path = core.getInput('path');
995+
// Collect a JSON string of all the version properties.
996+
let args = ['get-version', '-f', 'json'];
996997
if (dir_path) {
997998
args.push('-p', dir_path);
998999
}
999-
if (core.getInput('commonVars') === 'true') {
1000-
args.push('-c');
1000+
let versionJson = '';
1001+
yield exec.exec('nbgv', args, { listeners: { stdout: (data) => { versionJson += data.toString(); } } });
1002+
core.setOutput('versionJson', versionJson);
1003+
// Break up the JSON into individual outputs.
1004+
const versionProperties = JSON.parse(versionJson);
1005+
for (let name in versionProperties.CloudBuildAllVars) {
1006+
// Trim off the leading NBGV_
1007+
core.setOutput(name.substring(5), versionProperties.CloudBuildAllVars[name]);
10011008
}
1002-
if (core.getInput('allVars') === 'true') {
1003-
args.push('-a');
1009+
// Set environment variables if desired.
1010+
if (setCommonVars || setAllVars) {
1011+
args = ['cloud'];
1012+
if (dir_path) {
1013+
args.push('-p', dir_path);
1014+
}
1015+
if (setCommonVars) {
1016+
args.push('-c');
1017+
}
1018+
if (setAllVars) {
1019+
args.push('-a');
1020+
}
1021+
yield exec.exec('nbgv', args);
10041022
}
1005-
yield exec.exec('nbgv', args);
10061023
}
10071024
catch (error) {
10081025
core.setFailed(error.message);

src/main.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import * as path from 'path'
55

66
async function run() {
77
try {
8+
const toolVersion = core.getInput('toolVersion');
9+
const dir_path = core.getInput('path');
10+
const setCommonVars = core.getInput('setCommonVars') === 'true';
11+
const setAllVars = core.getInput('setAllVars') === 'true';
12+
813
// install nbgv
914
let installArgs = ['tool', 'install', '-g', 'nbgv'];
10-
const toolVersion = core.getInput('toolVersion');
1115
if (toolVersion) {
1216
installArgs.push('--version', toolVersion);
1317
}
@@ -20,21 +24,37 @@ async function run() {
2024
// add .dotnet/tools to the path
2125
core.addPath(path.join(os.homedir(), '.dotnet', 'tools'));
2226

23-
// run nbgv
24-
let jsonStr = '';
25-
let args = ['cloud'];
26-
const dir_path = core.getInput('path');
27+
// Collect a JSON string of all the version properties.
28+
let args = ['get-version', '-f', 'json'];
2729
if (dir_path) {
2830
args.push('-p', dir_path);
2931
}
30-
if (core.getInput('commonVars') === 'true') {
31-
args.push('-c');
32-
}
33-
if (core.getInput('allVars') === 'true') {
34-
args.push('-a');
32+
let versionJson = '';
33+
await exec.exec('nbgv', args, { listeners: { stdout: (data: Buffer) => { versionJson += data.toString() } } });
34+
core.setOutput('versionJson', versionJson);
35+
36+
// Break up the JSON into individual outputs.
37+
const versionProperties = JSON.parse(versionJson);
38+
for (let name in versionProperties.CloudBuildAllVars) {
39+
// Trim off the leading NBGV_
40+
core.setOutput(name.substring(5), versionProperties.CloudBuildAllVars[name]);
3541
}
3642

37-
await exec.exec('nbgv', args);
43+
// Set environment variables if desired.
44+
if (setCommonVars || setAllVars) {
45+
args = ['cloud'];
46+
if (dir_path) {
47+
args.push('-p', dir_path);
48+
}
49+
if (setCommonVars) {
50+
args.push('-c');
51+
}
52+
if (setAllVars) {
53+
args.push('-a');
54+
}
55+
56+
await exec.exec('nbgv', args);
57+
}
3858
} catch (error) {
3959
core.setFailed(error.message);
4060
}

0 commit comments

Comments
 (0)