is_default_branch global expression

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2022-04-25 13:41:39 +02:00
parent 535130561a
commit e1a45f6e54
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
7 changed files with 179 additions and 40 deletions

View file

@ -32,6 +32,12 @@ ___
* [Notes](#notes)
* [Latest tag](#latest-tag)
* [Global expressions](#global-expressions)
* [`{{branch}}`](#branch)
* [`{{tag}}`](#tag)
* [`{{sha}}`](#sha)
* [`{{base_ref}}`](#base_ref)
* [`{{is_default_branch}}`](#is_default_branch)
* [`{{date '<format>'}}`](#date-format)
* [Major version zero](#major-version-zero)
* [JSON output object](#json-output-object)
* [Overwrite labels](#overwrite-labels)
@ -232,7 +238,7 @@ Content of `${{ steps.meta.outputs.bake-file }}` file will look like this with `
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.version": "1.2.3",
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision": "90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.revision": "860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.licenses": "MIT"
},
"args": {
@ -605,26 +611,28 @@ tags: |
* [`type=semver,pattern=...`](#typesemver)
* [`type=match,pattern=...`](#typematch)
For conditionally tagging with latest for a specific branch name, e.g. if your default branch name
is not `master`, use `type=raw` with a boolean expression:
For conditionally tagging with latest for a specific branch name, e.g. if your
default branch name is not `master`, use `type=raw` with a boolean expression:
```yaml
tags: |
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}
# set latest tag for master branch
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }}
```
You can also use the [`{{is_default_branch}}` global expression](#is_default_branch)
to conditionally tag with latest for the default branch:
```yaml
tags: |
# set latest tag for default branch
type=raw,value=latest,enable={{is_default_branch}}
```
### Global expressions
The following [Handlebars' template](https://handlebarsjs.com/guide/) expressions for `prefix`, `suffix` and `value`
attributes are available:
| Expression | Output |
|--------------------------|----------------------|
| `{{branch}}` | `master` |
| `{{tag}}` | `v1.2.3` |
| `{{sha}}` | `90dd603` |
| `{{base_ref}}` | `master` |
| `{{date 'YYYYMMDD'}}` | `20210326` |
The following [Handlebars' template](https://handlebarsjs.com/guide/) expressions
for `prefix`, `suffix`, `value` and `enable` attributes are available:
```yaml
tags: |
@ -634,6 +642,63 @@ tags: |
type=raw,value=mytag-{{branch}}-{{sha}}
```
#### `{{branch}}`
Returns the branch name that triggered the workflow run. Will be empty if not
a branch reference:
| Event | Ref | Output |
|-----------------|-------------------------------|---------------------|
| `pull_request` | `refs/pull/2/merge` | |
| `push` | `refs/heads/master` | `master` |
| `push` | `refs/heads/my/branch` | `my-branch` |
| `push tag` | `refs/tags/v1.2.3` | |
#### `{{tag}}`
Returns the tag name that triggered the workflow run. Will be empty if not a
tag reference:
| Event | Ref | Output |
|-----------------|-------------------------------|--------------------|
| `pull_request` | `refs/pull/2/merge` | |
| `push` | `refs/heads/master` | |
| `push` | `refs/heads/my/branch` | |
| `push tag` | `refs/tags/v1.2.3` | `v1.2.3` |
#### `{{sha}}`
Returns the short commit SHA that triggered the workflow run (e.g., `90dd603`).
#### `{{base_ref}}`
Returns the base ref or target branch of the pull request that triggered the
workflow run. Will be empty for a branch reference:
| Event | Ref | Output |
|-----------------|-------------------------------|--------------------|
| `pull_request` | `refs/pull/2/merge` | `master` |
| `push` | `refs/heads/master` | |
| `push` | `refs/heads/my/branch` | |
| `push tag` | `refs/tags/v1.2.3` | `master` |
#### `{{is_default_branch}}`
Returns `true` if the branch that triggered the workflow run is the default
one, otherwise `false`.
Will compare against the branch name that triggered the workflow run or the
base ref or target branch for a pull request or a tag.
#### `{{date '<format>'}}`
Returns the current date rendered by its [moment format](https://momentjs.com/docs/#/displaying/format/).
| Expression | Output example |
|--------------------------------------------|-----------------------------------------|
| `{{date 'YYYYMMDD'}}` | `20200110` |
| `{{date 'dddd, MMMM Do YYYY, h:mm:ss a'}}` | `Friday, January 10th 2020, 3:25:50 pm` |
### Major version zero
Major version zero (`0.y.z`) is for initial development and **may** change at any time. This means the public API

View file

@ -598,7 +598,8 @@ describe('push', () => {
`type=raw,value=mytag-{{branch}}`,
`type=raw,value=mytag-{{date 'YYYYMMDD'}}`,
`type=raw,value=mytag-tag-{{tag}}`,
`type=raw,value=mytag-baseref-{{base_ref}}`
`type=raw,value=mytag-baseref-{{base_ref}}`,
`type=raw,value=mytag-defbranch,enable={{is_default_branch}}`
],
} as Inputs,
{
@ -606,7 +607,8 @@ describe('push', () => {
partial: [
'mytag-20200110',
'mytag-tag-',
'mytag-baseref-'
'mytag-baseref-',
'mytag-defbranch'
],
latest: false
} as Version,
@ -614,7 +616,8 @@ describe('push', () => {
'user/app:mytag-master',
'user/app:mytag-20200110',
'user/app:mytag-tag-',
'user/app:mytag-baseref-'
'user/app:mytag-baseref-',
'user/app:mytag-defbranch'
],
[
"org.opencontainers.image.title=Hello-World",
@ -667,7 +670,8 @@ describe('push', () => {
tags: [
`type=edge,branch=master`,
`type=ref,event=branch,enable=false`,
`type=sha,format=long`
`type=sha,format=long`,
`type=raw,value=defbranch,enable={{is_default_branch}}`
],
} as Inputs,
{
@ -1321,21 +1325,25 @@ describe('tag', () => {
images: ['org/app', 'ghcr.io/user/app'],
tags: [
`type=raw,{{tag}}-{{sha}}-foo`,
`type=raw,{{base_ref}}-foo`
`type=raw,{{base_ref}}-foo`,
`type=raw,defbranch-foo,enable={{is_default_branch}}`
]
} as Inputs,
{
main: 'v1.1.1-860c190-foo',
partial: [
'master-foo'
'master-foo',
'defbranch-foo'
],
latest: false
} as Version,
[
'org/app:v1.1.1-860c190-foo',
'org/app:master-foo',
'org/app:defbranch-foo',
'ghcr.io/user/app:v1.1.1-860c190-foo',
'ghcr.io/user/app:master-foo'
'ghcr.io/user/app:master-foo',
'ghcr.io/user/app:defbranch-foo'
],
[
"org.opencontainers.image.title=Hello-World",
@ -2317,15 +2325,19 @@ describe('pr', () => {
images: ['org/app'],
tags: [
`type=raw,value=mytag-{{base_ref}}`,
`type=raw,mytag-defbranch,enable={{is_default_branch}}`
]
} as Inputs,
{
main: 'mytag-master',
partial: [],
partial: [
'mytag-defbranch'
],
latest: false
} as Version,
[
'org/app:mytag-master'
'org/app:mytag-master',
'org/app:mytag-defbranch'
],
[
"org.opencontainers.image.title=Hello-World",
@ -2339,21 +2351,25 @@ describe('pr', () => {
]
],
[
'pr06',
'pr11',
'event_pull_request.env',
{
images: ['org/app'],
tags: [
`type=raw,value=mytag-{{base_ref}}`,
`type=raw,mytag-defbranch,enable={{is_default_branch}}`
]
} as Inputs,
{
main: 'mytag-master',
partial: [],
partial: [
'mytag-defbranch'
],
latest: false
} as Version,
[
'org/app:mytag-master'
'org/app:mytag-master',
'org/app:mytag-defbranch'
],
[
"org.opencontainers.image.title=Hello-World",
@ -2521,19 +2537,25 @@ describe('schedule', () => {
images: ['org/app', 'ghcr.io/user/app'],
tags: [
`type=schedule`,
`type=sha,priority=2000`
`type=sha,priority=2000`,
`type=raw,value=defbranch,enable={{is_default_branch}}`
]
} as Inputs,
{
main: 'sha-860c190',
partial: ['nightly'],
partial: [
'nightly',
'defbranch'
],
latest: false
} as Version,
[
'org/app:sha-860c190',
'org/app:nightly',
'org/app:defbranch',
'ghcr.io/user/app:sha-860c190',
'ghcr.io/user/app:nightly'
'ghcr.io/user/app:nightly',
'ghcr.io/user/app:defbranch'
],
[
"org.opencontainers.image.title=Hello-World",
@ -2611,6 +2633,40 @@ describe('release', () => {
"org.opencontainers.image.licenses=MIT"
]
],
[
'release02',
'event_release_created.env',
{
images: ['user/app'],
tags: [
`type=ref,event=tag`,
`type=raw,value=baseref-{{base_ref}}`,
`type=raw,value=defbranch,enable={{is_default_branch}}`
]
} as Inputs,
{
main: 'v1.1.1',
partial: [
'baseref-'
],
latest: true
} as Version,
[
'user/app:v1.1.1',
'user/app:baseref-',
'user/app:latest'
],
[
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=v1.1.1",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.licenses=MIT"
]
]
])('given %s with %p event', tagsLabelsTest);
});

View file

@ -417,11 +417,6 @@ describe('parse', () => {
{} as Tag,
true
],
[
`type=sha,enable=foo`,
{} as Tag,
true
],
[
`type=sha,format=foo`,
{} as Tag,

2
dist/index.js generated vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View file

@ -30,6 +30,7 @@ export class Meta {
constructor(inputs: Inputs, context: Context, repo: ReposGetResponseData) {
// Needs to override Git reference with pr ref instead of upstream branch ref
// for pull_request_target event
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target
if (/pull_request_target/.test(context.eventName)) {
context.ref = `refs/pull/${context.payload.number}/merge`;
}
@ -51,7 +52,11 @@ export class Meta {
};
for (const tag of this.tags) {
if (!/true/i.test(tag.attrs['enable'])) {
const enabled = this.setGlobalExp(tag.attrs['enable']);
if (!['true', 'false'].includes(enabled)) {
throw new Error(`Invalid value for enable attribute: ${enabled}`);
}
if (!/true/i.test(enabled)) {
continue;
}
switch (tag.type) {
@ -369,6 +374,27 @@ export class Meta {
}
return '';
},
is_default_branch: function () {
let branch = ctx.ref.replace(/^refs\/heads\//g, '');
if (/^refs\/tags\//.test(ctx.ref) && ctx.payload?.base_ref != undefined) {
branch = ctx.payload.base_ref.replace(/^refs\/heads\//g, '');
}
if (/^refs\/pull\//.test(ctx.ref) && ctx.payload?.pull_request?.base?.ref != undefined) {
branch = ctx.payload.pull_request.base.ref;
}
if (branch == undefined || branch.length == 0) {
return 'false';
}
if (ctx.payload?.repository?.default_branch == branch) {
return 'true';
}
// following events always trigger for last commit on default branch
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows
if (/create/.test(ctx.eventName) || /discussion/.test(ctx.eventName) || /issues/.test(ctx.eventName) || /schedule/.test(ctx.eventName)) {
return 'true';
}
return 'false';
},
date: function (format) {
return moment(currentDate).utc().format(format);
}

View file

@ -206,9 +206,6 @@ export function Parse(s: string): Tag {
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'priority')) {
tag.attrs['priority'] = DefaultPriorities[tag.type];
}
if (!['true', 'false'].includes(tag.attrs['enable'])) {
throw new Error(`Invalid value for enable attribute: ${tag.attrs['enable']}`);
}
return tag;
}