Skip to content

Commit

Permalink
backward compatibility with old images format
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Apr 28, 2022
1 parent a5680a6 commit 2f4dd14
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Expand Up @@ -241,7 +241,8 @@ jobs:
id: docker_meta
uses: ./
with:
images: ${{ env.DOCKER_IMAGE }}
images: |
${{ env.DOCKER_IMAGE }}
tags: |
type=schedule
type=ref,event=branch
Expand Down
41 changes: 19 additions & 22 deletions README.md
Expand Up @@ -126,7 +126,8 @@ jobs:
id: meta
uses: docker/metadata-action@v3
with:
images: name/app
images: |
name/app
tags: |
type=ref,event=branch
type=ref,event=pr
Expand Down Expand Up @@ -203,7 +204,8 @@ jobs:
id: meta
uses: docker/metadata-action@v3
with:
images: name/app
images: |
name/app
tags: |
type=ref,event=branch
type=ref,event=pr
Expand Down Expand Up @@ -265,31 +267,26 @@ Following inputs can be used as `step.with` keys
> org.opencontainers.image.vendor=MyCompany
> ```
> `CSV` type is a comma-delimited string
> ```yaml
> images: name/app,ghcr.io/name/app
> ```
| Name | Type | Description |
|---------------------|----------|------------------------------------|
| `images` | List/CSV | List of Docker images to use as base name for tags |
| `tags` | List | List of [tags](#tags-input) as key-value pair attributes |
| `flavor` | List | [Flavor](#flavor-input) to apply |
| `labels` | List | List of custom labels |
| `sep-tags` | String | Separator to use for tags output (default `\n`) |
| `sep-labels` | String | Separator to use for labels output (default `\n`) |
| `bake-target` | String | Bake target name (default `docker-metadata-action`) |
| Name | Type | Description |
|---------------------|--------|----------------------------------------------------------|
| `images` | List | List of Docker images to use as base name for tags |
| `tags` | List | List of [tags](#tags-input) as key-value pair attributes |
| `flavor` | List | [Flavor](#flavor-input) to apply |
| `labels` | List | List of custom labels |
| `sep-tags` | String | Separator to use for tags output (default `\n`) |
| `sep-labels` | String | Separator to use for labels output (default `\n`) |
| `bake-target` | String | Bake target name (default `docker-metadata-action`) |
### outputs
Following outputs are available
| Name | Type | Description |
|---------------|---------|---------------------------------------|
| `version` | String | Docker image version |
| `tags` | String | Docker tags |
| `labels` | String | Docker labels |
| `json` | String | JSON output of tags and labels |
| Name | Type | Description |
|---------------|---------|-------------------------------------------------------------------------------|
| `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 |
## `images` input
Expand Down
19 changes: 19 additions & 0 deletions __tests__/image.test.ts
Expand Up @@ -16,6 +16,22 @@ describe('transform', () => {
] as Image[],
false
],
[
[
`name/foo,name/bar`
],
[
{
name: `name/foo`,
enable: true,
},
{
name: `name/bar`,
enable: true,
}
] as Image[],
false
],
[
[
`name/foo`,
Expand Down Expand Up @@ -66,6 +82,9 @@ describe('transform', () => {
],
[
[`name=,enable=true`], undefined, true
],
[
[`name/foo,name=name/bar,enable=true`], undefined, true
]
])('given %p', async (l: string[], expected: Image[], invalid: boolean) => {
try {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/context.ts
Expand Up @@ -27,7 +27,7 @@ export function tmpDir(): string {

export function getInputs(): Inputs {
return {
images: getInputList('images'),
images: getInputList('images', true),
tags: getInputList('tags', true),
flavor: getInputList('flavor', true),
labels: getInputList('labels', true),
Expand Down
38 changes: 37 additions & 1 deletion src/image.ts
@@ -1,12 +1,39 @@
import {parse} from 'csv-parse/sync';
import * as core from '@actions/core';

export interface Image {
name: string;
enable: boolean;
}

export function Transform(inputs: string[]): Image[] {
const images: Image[] = [];
let images: Image[] = [];

// backward compatibility with old format
if (inputs.length == 1) {
let newformat = false;
const fields = parse(inputs[0], {
relaxColumnCount: true,
skipEmptyLines: true
})[0];
for (const field of fields) {
const parts = field
.toString()
.split('=')
.map(item => item.trim());
if (parts.length == 1) {
images.push({name: parts[0].toLowerCase(), enable: true});
} else {
newformat = true;
break;
}
}
if (!newformat) {
return output(images);
}
}

images = [];
for (const input of inputs) {
const image: Image = {name: '', enable: true};
const fields = parse(input, {
Expand Down Expand Up @@ -46,5 +73,14 @@ export function Transform(inputs: string[]): Image[] {
}
images.push(image);
}
return output(images);
}

function output(images: Image[]): Image[] {
core.startGroup(`Processing images input`);
for (const image of images) {
core.info(`name=${image.name},enable=${image.enable}`);
}
core.endGroup();
return images;
}

0 comments on commit 2f4dd14

Please sign in to comment.