Skip to content

Commit

Permalink
Merge pull request #89 from crazy-max/json-output
Browse files Browse the repository at this point in the history
Add json output
  • Loading branch information
crazy-max committed May 22, 2021
2 parents 01dc739 + e6f3e4a commit e856afa
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 20 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -165,6 +165,30 @@ jobs:
org.opencontainers.image.description=Another description
org.opencontainers.image.vendor=MyCompany
json:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Docker meta
id: meta
uses: ./
with:
images: |
${{ env.DOCKER_IMAGE }}
ghcr.io/name/app
labels: |
maintainer=CrazyMax
-
name: JSON output
run: |
echo "maintainer=${{ fromJSON(steps.meta.outputs.json).labels['maintainer'] }}"
echo "version=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}"
echo "revision=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}"
echo "created=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}"
docker-push:
runs-on: ubuntu-latest
services:
Expand Down
28 changes: 27 additions & 1 deletion README.md
Expand Up @@ -32,6 +32,7 @@ ___
* [Latest tag](#latest-tag)
* [Global expressions](#global-expressions)
* [Major version zero](#major-version-zero)
* [JSON output object](#json-output-object)
* [Overwrite labels](#overwrite-labels)
* [Keep up-to-date with GitHub Dependabot](#keep-up-to-date-with-github-dependabot)

Expand Down Expand Up @@ -274,6 +275,7 @@ Following outputs are available
| `version` | String | Docker image version |
| `tags` | String | Docker tags |
| `labels` | String | Docker labels |
| `json` | String | JSON output of tags and labels |
| `bake-file` | File | [Bake definition file](https://github.com/docker/buildx#file-definition) path |
## `flavor` input
Expand Down Expand Up @@ -586,6 +588,30 @@ tags: |
type=semver,pattern={{major}},enable=${{ !startsWith(github.ref, 'refs/tags/v0.') }}
```
### JSON output object
The `json` output is a JSON object composed of the generated tags and labels so that you can reuse them further in your
workflow using the [`fromJSON` function](https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#fromjson):
```yaml
-
name: Docker meta
uses: docker/metadata-action@v3
id: meta
with:
images: name/app
-
name: Build and push
uses: docker/build-push-action@v2
with:
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
BUILDTIME=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
VERSION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
REVISION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
```
### Overwrite labels
If some of the [OCI Image Format Specification](https://github.com/opencontainers/image-spec/blob/master/annotations.md)
Expand All @@ -594,7 +620,7 @@ labels generated are not suitable, you can overwrite them like this:
```yaml
-
name: Docker meta
id: docker_meta
id: meta
uses: docker/metadata-action@v3
with:
images: name/app
Expand Down
252 changes: 252 additions & 0 deletions __tests__/meta.test.ts
Expand Up @@ -2394,6 +2394,258 @@ describe('raw', () => {
])('given %p wth %p event', tagsLabelsTest);
});

describe('json', () => {
// prettier-ignore
test.each([
[
'json01',
'event_push.env',
{
images: ['user/app'],
tags: [
`type=ref,event=branch`,
`type=raw,my`,
`type=raw,custom`,
`type=raw,tags`
],
labels: [
"invalid"
]
} as Inputs,
{
"tags": [
"user/app:dev",
"user/app:my",
"user/app:custom",
"user/app:tags"
],
"labels": {
"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": "dev",
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision": "90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses": "MIT"
}
}
],
[
'json02',
'event_push.env',
{
images: ['user/app'],
tags: [
`type=ref,event=branch`,
`type=raw,my`
]
} as Inputs,
{
"tags": [
"user/app:dev",
"user/app:my",
],
"labels": {
"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": "dev",
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision": "90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses": "MIT"
}
}
],
[
'json03',
'event_tag_release1.env',
{
images: ['user/app'],
tags: [
`type=ref,event=tag`,
`type=raw,my`,
`type=raw,custom`,
`type=raw,tags`
],
bakeTarget: "meta"
} as Inputs,
{
"tags": [
"user/app:release1",
"user/app:my",
"user/app:custom",
"user/app:tags",
"user/app:latest"
],
"labels": {
"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": "release1",
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision": "90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses": "MIT"
}
}
],
[
'json04',
'event_tag_20200110-RC2.env',
{
images: ['user/app'],
tags: [
`type=match,pattern=\\d{8}`,
`type=raw,my`,
`type=raw,custom`,
`type=raw,tags`
],
flavor: [
`latest=false`
]
} as Inputs,
{
"tags": [
"user/app:20200110",
"user/app:my",
"user/app:custom",
"user/app:tags"
],
"labels": {
"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": "20200110",
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision": "90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses": "MIT"
}
}
],
[
'json05',
'event_tag_v1.1.1.env',
{
images: ['org/app', 'ghcr.io/user/app'],
tags: [
`type=semver,pattern={{version}}`,
`type=semver,pattern={{major}}.{{minor}}`,
`type=semver,pattern={{major}}`,
`type=raw,my`,
`type=raw,custom`,
`type=raw,tags`
]
} as Inputs,
{
"tags": [
"org/app:1.1.1",
"org/app:1.1",
"org/app:1",
"org/app:my",
"org/app:custom",
"org/app:tags",
"org/app:latest",
"ghcr.io/user/app:1.1.1",
"ghcr.io/user/app:1.1",
"ghcr.io/user/app:1",
"ghcr.io/user/app:my",
"ghcr.io/user/app:custom",
"ghcr.io/user/app:tags",
"ghcr.io/user/app:latest"
],
"labels": {
"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": "1.1.1",
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision": "90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses": "MIT"
}
}
],
[
'json06',
'event_tag_v1.1.1.env',
{
images: ['org/app', 'ghcr.io/user/app'],
tags: [
`type=raw,my`,
`type=raw,custom`,
`type=raw,tags`
]
} as Inputs,
{
"tags": [
"org/app:my",
"org/app:custom",
"org/app:tags",
"ghcr.io/user/app:my",
"ghcr.io/user/app:custom",
"ghcr.io/user/app:tags"
],
"labels": {
"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": "my",
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision": "90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses": "MIT"
}
}
],
[
'json07',
'event_tag_v1.1.1.env',
{
images: ['org/app'],
labels: [
"foo",
"maintainer=CrazyMax",
"org.opencontainers.image.title=MyCustom=Title",
"org.opencontainers.image.description=Another description",
"org.opencontainers.image.vendor=MyCompany",
],
} as Inputs,
{
"tags": [
"org/app:v1.1.1",
"org/app:latest"
],
"labels": {
"maintainer": "CrazyMax",
"org.opencontainers.image.title": "MyCustom=Title",
"org.opencontainers.image.description": "Another description",
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.vendor": "MyCompany",
"org.opencontainers.image.version": "v1.1.1",
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision": "90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses": "MIT"
}
}
]
])('given %p with %p event', async (name: string, envFile: string, inputs: Inputs, exJSON: {}) => {
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));
const context = github.context();
console.log(process.env, context);

const repo = await github.repo(process.env.GITHUB_TOKEN || '');
const meta = new Meta({...getInputs(), ...inputs}, context, repo);

const jsonOutput = meta.getJSON();
console.log('json', jsonOutput);
expect(jsonOutput).toEqual(exJSON);
});
});

describe('bake', () => {
// prettier-ignore
test.each([
Expand Down

0 comments on commit e856afa

Please sign in to comment.