Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from Brymastr/master
Browse files Browse the repository at this point in the history
Ability to generate multiple independently incremented build numbers
  • Loading branch information
Einar Egilsson authored Nov 22, 2019
2 parents a03ee0d + 78eb134 commit 32debd3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
48 changes: 34 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# build-number
GitHub action for generating sequential build numbers for GitHub actions. The build number is stored in your GitHub repository as a ref, it doesn't add any extra commits to your repository. Use in your workflow like so:

```
```yaml
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Generate build number
uses: einaregilsson/build-number@v1
uses: einaregilsson/build-number@v2
with:
token: ${{secrets.github_token}}
- name: Print new build number
run: echo "Build number is $BUILD_NUMBER"
# Or, if you're on Windows: echo "Build number is ${env:BUILD_NUMBER}"
```

After that runs the subsequent steps in your job will have the environment variable ```BUILD_NUMBER``` available. If you prefer to be more explicit you can use the output of the step, like so:
After that runs the subsequent steps in your job will have the environment variable `BUILD_NUMBER` available. If you prefer to be more explicit you can use the output of the step, like so:

```
```yaml
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Generate build number
id: buildnumber
uses: einaregilsson/build-number@v1
uses: einaregilsson/build-number@v2
with:
token: ${{secrets.github_token}}

Expand All @@ -38,16 +38,16 @@ The `GITHUB_TOKEN` environment variable is defined by GitHub already for you. Se

## Getting the build number in other jobs

For other steps in the same job you can use the methods above, to actually get the build number in other jobs you need some extra actions, since jobs are run in a completely clean environment. You need to use the ```actions/upload-artifact@v1``` action to save the build number as a workflow artifact, then download it at the start of the next job with ```actions/download-artifact@v1``` and then run the build number job to make it into an environment variable there again.
For other steps in the same job you can use the methods above, to actually get the build number in other jobs you need some extra actions, since jobs are run in a completely clean environment. You need to use the `actions/upload-artifact@v1` action to save the build number as a workflow artifact, then download it at the start of the next job with `actions/download-artifact@v1` and then run the build number job to make it into an environment variable there again.

```
```yaml
jobs:
job1:
runs-on: ubuntu-latest
steps:
- name: Generate build number
id: buildnumber
uses: einaregilsson/build-number@v1
uses: einaregilsson/build-number@v2
with:
token: ${{secrets.github_token}}
- name: Upload build number
Expand All @@ -65,28 +65,48 @@ jobs:
name: BUILD_NUMBER
- name: Restore build number
id: buildnumber
uses: einaregilsson/build-number@v1
uses: einaregilsson/build-number@v2
# Don't need to add Github token here, since you're only getting an artifact.
# After this runs you'll again have the $BUILD_NUMBER environment variable, and
# the ${{ steps.buildnumber.outputs.build_number }} output.
```


## Setting the initial build number.

If you're moving from another build system, you might want to start from some specific number. The ```build-number``` action simply uses a special tag name to store the build number, ```build-number-x```, so you can just create and push a tag with the number you want to start on. E.g. do
If you're moving from another build system, you might want to start from some specific number. The `build-number` action simply uses a special tag name to store the build number, `build-number-x`, so you can just create and push a tag with the number you want to start on. E.g. do

```
git tag build-number-500
git push origin build-number-500
```
and then your next build number will be 501. The action will always delete older refs that start with ```build-number-```, e.g. when it runs and finds ```build-number-500``` it will create a new tag, ```build-number-501``` and then delete ```build-number-500```.
and then your next build number will be 501. The action will always delete older refs that start with `build-number-`, e.g. when it runs and finds `build-number-500` it will create a new tag, `build-number-501` and then delete `build-number-500`.
## Generating multiple independent build numbers
Sometimes you may have more than one project to build in one repository. For example you may have a client and a server in the same github repository that you would like to generate independent build numbers for. Another example is you have two Dockerfiles in one repo and you'd like to version each of the built images with their own numbers.
To do this, use the `prefix` key, like so:
```yaml
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Generate build number
id: buildnumber
uses: einaregilsson/build-number@v2
with:
token: ${{ secrets.github_token }}
prefix: client
```

This will generate a git tag like `client-build-number-1`.

If you then do the same in another workflow and use `prefix: server` then you'll get a second build-number tag called `server-build-number-1`.

## Branches and build numbers

The build number generator is global, there's no concept of special build numbers for special branches, it's probably something you would just use on builds from your master branch. It's just one number that gets increased every time the action is run.
The build number generator is global, there's no concept of special build numbers for special branches unless handled manually with the `prefix` property. It's probably something you would just use on builds from your master branch. It's just one number that gets increased every time the action is run.

So, that's it. Hope you can use it. You can read more about how it works in this blog post: http://einaregilsson.com/a-github-action-for-generating-sequential-build-numbers/

9 changes: 6 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ inputs:
token:
description: 'GitHub Token to create and delete refs (GITHUB_TOKEN)'
required: false # Not required when getting the stored build number for later jobs, only in the first jobs when it's generated

prefix:
description: 'Prefix for the build-number-<num> tag to make it unique if tracking multiple build numbers'
required: false

outputs:
build_number:
description: 'Generated build number'

branding:
icon: 'hash'
icon: 'hash'
color: 'green'
14 changes: 9 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ function request(method, path, data, callback) {
function main() {

const path = 'BUILD_NUMBER/BUILD_NUMBER';
const prefix = env.INPUT_PREFIX ? `${env.INPUT_PREFIX}-` : '';

//See if we've already generated the build number and are in later steps...
if (fs.existsSync(path)) {
let buildNumber = fs.readFileSync(path);
Expand All @@ -80,7 +82,7 @@ function main() {
}
}

request('GET', `/repos/${env.GITHUB_REPOSITORY}/git/refs/tags/build-number-`, null, (err, status, result) => {
request('GET', `/repos/${env.GITHUB_REPOSITORY}/git/refs/tags/${prefix}build-number-`, null, (err, status, result) => {

let nextBuildNumber, nrTags;

Expand All @@ -89,11 +91,13 @@ function main() {
nextBuildNumber = 1;
nrTags = [];
} else if (status === 200) {
nrTags = result.filter(d => d.ref.match(/\/build-number-(\d+)$/));
const regexString = `/${prefix}build-number-(\\d+)$`;
const regex = new RegExp(regexString);
nrTags = result.filter(d => d.ref.match(regex));

const MAX_OLD_NUMBERS = 5; //One or two ref deletes might fail, but if we have lots then there's something wrong!
if (nrTags.length > MAX_OLD_NUMBERS) {
fail(`ERROR: Too many build-number- refs in repository, found ${nrTags.length}, expected only 1. Check your tags!`);
fail(`ERROR: Too many ${prefix}build-number- refs in repository, found ${nrTags.length}, expected only 1. Check your tags!`);
}

//Existing build numbers:
Expand All @@ -113,7 +117,7 @@ function main() {
}

let newRefData = {
ref:`refs/tags/build-number-${nextBuildNumber}`,
ref:`refs/tags/${prefix}build-number-${nextBuildNumber}`,
sha: env.GITHUB_SHA
};

Expand Down Expand Up @@ -143,7 +147,7 @@ function main() {
}
});
}
}
}

});
});
Expand Down

0 comments on commit 32debd3

Please sign in to comment.