Skip to content

Commit

Permalink
Merge pull request #2 from riege/BUILD-52
Browse files Browse the repository at this point in the history
BUILD-52 Add additional outputs
  • Loading branch information
wawe authored May 10, 2021
2 parents f025368 + 11ef66e commit 46eb498
Show file tree
Hide file tree
Showing 8 changed files with 1,125 additions and 31 deletions.
25 changes: 21 additions & 4 deletions .github/workflows/testAction.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,24 @@ jobs:
- run: npm test
- uses: ./
id: version
- run: |
export VERSION="${{ steps.version.outputs.version }}"
echo "$VERSION must match .+-.+-.+"
bash -c '[[ $VERSION =~ .+-.+-.+ ]]'
- name: Test outputs
run: |
export OUTPUT="${{ steps.version.outputs.version }}"
echo "version: $OUTPUT must match .+-.+-.+"
bash -c '[[ $OUTPUT =~ .+-.+-.+ ]]'
export OUTPUT="${{ steps.version.outputs.version-without-v }}"
echo "version-without-v: $OUTPUT must match .+-.+-.+"
bash -c '[[ $OUTPUT =~ .+-.+-.+ ]]'
export OUTPUT="${{ steps.version.outputs.major }}"
echo "major: $OUTPUT must not be empty"
test -n "$OUTPUT"
export OUTPUT="${{ steps.version.outputs.minor }}"
echo "minor: $OUTPUT must not be empty"
test -n "$OUTPUT"
export OUTPUT="${{ steps.version.outputs.patch }}"
echo "patch: $OUTPUT must not be empty"
test -n "$OUTPUT"
39 changes: 31 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
# action-version
A GitHub action to determine the version for build artifacts.

## Versioning Scheme

The general idea is to generate a unique version string from the job context that is unique for each run, easy to read and compare, and clearly identifies the revision that was used.

| Ref Type | Version | Example |
| ------------ | ------- | ------- |
| Git tag | \<tagname\> | v1.3.2
| Branch | \<branchname\>-\<runnumber\>-\<sha\> | main-9-f025368a |
| Pull request | pr-\<prnumber\>-\<runnumber\>-\<sha\> | pr-9-1-89d735ed |

Tags are assumed to already satisfy these goals and are taken as-is. For other cases, the first part (branch name or PR number) tells whether two version can be compared, the second part (build number) allows to quickly tell versions apart, and the commit hash uniquely identifies the revision of the source that was build.

## Outputs
version: The version string

| CI/CD Build | Version |
| ----------- | ----------- |
| Git tag x.y.z on master branch (a prefixed "v" is okay, too) | X.Y.Z, X.Y, X, vX.Y.Z ... |
| Untagged main commit | main-\<runnumber\>-\<sha\> |
| Pull request | pr-\<prnumber\>-\<runnumber\>-\<sha\>
- **version**: The version string
- **version-without-v**: The version string, with a leading "v" removed for tags that resemble a version number
- **major**: The major part of a semantic version
- **minor**: The minor part of a semantic version
- **patch**: The remainder (after major and minor) of a semantic version

## Example Output Values

| Ref | Run | SHA | version | version-without-v | major | minor | patch |
| --- | --- | --- | ------- | ----------------- | ----- | ----- | ----- |
| refs/tags/v1| 9| a52f| v1| 1| 1| | |
| refs/tags/1.2.3| 9| a52f| 1.2.3| 1.2.3| 1| 2| 3|
| refs/tags/v1.2.3| 9| a52f| v1.2.3| 1.2.3| 1| 2| 3|
| refs/tags/1.2.3.4| 9| a52f| 1.2.3.4| 1.2.3.4| 1| 2| 3.4|
| refs/heads/main| 9| a52f| main-9-a52f| main-9-a52f| main| 9| a52f|
| refs/heads/stable| 9| a52f| stable-9-a52f| stable-9-a52f| stable| 9| a52f|
| refs/pull/1/merge| 9| a52f| pr-1-9-a52f| pr-1-9-a52f| pr-1| 9| a52f|

## Example Usage
```
Expand All @@ -27,8 +50,8 @@ jobs:
name: Echo action's outputs
steps:
- id: version
uses: riege/action-version@v1.0
uses: riege/action-version@v1
- run: |
echo Version: ${{ steps.version.outputs.version }}
echo Image tag: "${{ secrets.ACR_LOGIN_SERVER }}/my-container:${{ steps.version.outputs.version }}"
echo Image tag: "${{ secrets.ACR_LOGIN_SERVER }}/my-container:${{ steps.version.outputs.version-without-v }}"
```
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ description: 'Determine the version for the build artifacts'
outputs:
version:
description: "The version string"
version-without-v:
description: "The version string, with a leading \"v\" removed for tags that resemble a version number"
major:
description: "The major part of a semantic version"
minor:
description: "The minor part of a semantic version"
patch:
description: "The remainder (after major and minor) of a semantic version"

runs:
using: "node12"
Expand Down
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const core = require('@actions/core');
const getVersion = require('./version');
const version = require('./version');

try {
const version = getVersion.get(process.env.GITHUB_REF, process.env.GITHUB_RUN_NUMBER, process.env.GITHUB_SHA);
core.setOutput("version", version);
const outputs = version.get(process.env.GITHUB_REF, process.env.GITHUB_RUN_NUMBER, process.env.GITHUB_SHA);
core.setOutput("version", outputs.version);
core.setOutput("version-without-v", outputs.versionWithoutV);
core.setOutput("major", outputs.major);
core.setOutput("minor", outputs.minor);
core.setOutput("patch", outputs.patch);
} catch (error) {
core.setFailed(error.message);
}
Loading

0 comments on commit 46eb498

Please sign in to comment.