Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions .github/workflows/__test-action-get-image-metadata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,141 @@ on: # yamllint disable-line rule:truthy
permissions: {}

jobs:
candidate-contracts:
name: Assert candidate identity and metadata policy
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const assert = require('node:assert/strict');
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;
const { stdout } = await exec.getExecOutput('ruby', [
'-rjson', '-ryaml', '-e', 'puts JSON.generate(YAML.safe_load(File.read(ARGV[0])))',
'actions/docker/get-image-metadata/action.yml',
]);
const steps = JSON.parse(stdout).runs.steps;
const image = 'ghcr.io/example/app';
const revision = 'a'.repeat(40);
const baseEnv = {
INPUT_CANDIDATE: 'true', GITHUB_REPOSITORY_ID: '123', GITHUB_REPOSITORY: 'example/app',
GITHUB_RUN_ID: '456', GITHUB_RUN_ATTEMPT: '1', IMAGE_NAME_WITH_REGISTRY: image,
DOCKER_METADATA_LABELS: `org.opencontainers.image.revision=${revision}\norg.opencontainers.image.version=main`,
DOCKER_METADATA_ANNOTATIONS: 'manifest:org.opencontainers.image.version=main',
DOCKER_METADATA_TAGS: `${image}:main\n${image}:latest`,
DEFAULT_BRANCH: 'main', GH_REF: 'refs/heads/main', ISSUE_NUMBER: '12',
};
async function evaluate(id, overrides = {}, eventName = 'push') {
const outputs = {};
const env = { ...baseEnv, ...overrides };
try {
await new AsyncFunction('require', 'process', 'context', 'core', steps.find(step => step.id === id).with.script)(
require, { env }, { eventName, ref: env.GH_REF }, {
setOutput: (key, value) => { outputs[key] = value; },
setFailed: message => { throw new Error(message); },
},
);
} catch (error) {
if (id === 'generate-outputs') assert.equal(outputs.tags, undefined, 'Invalid candidates must expose no publication tags');
throw error;
}
return outputs;
}
const first = await evaluate('generate-outputs');
const second = await evaluate('generate-outputs');
assert.match(first.tags, /^ghcr\.io\/example\/app:candidate-123-456-1-[a-f0-9-]{36}$/);
assert.notEqual(first.tags, second.tags, 'Repeated calls must not share a temporary tag');
assert.equal(first.labels, baseEnv.DOCKER_METADATA_LABELS);
assert.equal(first.annotations, baseEnv.DOCKER_METADATA_ANNOTATIONS);
assert.deepEqual(JSON.parse(first.candidate), {
destinations: [`${image}:main`, `${image}:latest`],
source: { repository: 'example/app', revision }, run: { id: '456', attempt: '1' },
});
const normal = await evaluate('generate-outputs', { INPUT_CANDIDATE: 'false' });
assert.equal(normal.tags, baseEnv.DOCKER_METADATA_TAGS);
assert.equal(normal.candidate, undefined);
for (const [key, value, prefix] of [
['GITHUB_RUN_ATTEMPT', '2', 'candidate-123-456-2-'],
['GITHUB_RUN_ID', '789', 'candidate-123-789-1-'],
['GITHUB_REPOSITORY_ID', '987', 'candidate-987-456-1-'],
]) {
assert((await evaluate('generate-outputs', { [key]: value })).tags.startsWith(`${image}:${prefix}`));
}
for (const key of ['GITHUB_REPOSITORY_ID', 'GITHUB_RUN_ID', 'GITHUB_RUN_ATTEMPT']) {
for (const value of ['', '0', '-1', '1\nother', 'no']) {
await assert.rejects(evaluate('generate-outputs', { [key]: value }), /requires repository, run and attempt IDs/);
}
}
for (const overrides of [
{ GITHUB_REPOSITORY: '' }, { DOCKER_METADATA_LABELS: 'org.opencontainers.image.revision=invalid' },
{ DOCKER_METADATA_TAGS: '' }, { DOCKER_METADATA_TAGS: 'ghcr.io/other/app:main' },
]) {
await assert.rejects(evaluate('generate-outputs', overrides), /Candidate publication requires/);
}
for (const value of ['', 'yes', 'TRUE']) {
await assert.rejects(evaluate('define-metadata-inputs', { INPUT_CANDIDATE: value }), /must be true or false/);
}
for (const [event, overrides, expected] of [
['push', {}, { flavor: 'latest=true', tags: 'type=ref,event=branch' }],
['pull_request', { GH_REF: 'refs/pull/12/merge' }, { flavor: 'latest=false', tags: 'type=sha,prefix=pr-12-\ntype=ref,event=pr' }],
['push', { GH_REF: 'refs/tags/v1.2.3' }, { flavor: 'latest=false', tags: 'type=ref,event=tag' }],
['push', { INPUT_TAG: '1.2.3' }, { tags: 'type=raw,value=1.2.3', flavor: 'latest=false' }],
]) {
assert.deepEqual(await evaluate('define-metadata-inputs', overrides, event), expected);
}

candidate-metadata:
name: Test candidate metadata (${{ matrix.tag || 'event tags' }})
runs-on: ubuntu-latest
strategy:
matrix:
tag: ["", "1.2.3"]
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- id: normal
uses: $/actions/docker/get-image-metadata
with:
image: application-test
tag: ${{ matrix.tag }}

- id: candidate
uses: $/actions/docker/get-image-metadata
with:
image: application-test
tag: ${{ matrix.tag }}
candidate: true

- name: Assert metadata and destinations
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
NORMAL: ${{ toJSON(steps.normal.outputs) }}
CANDIDATE: ${{ toJSON(steps.candidate.outputs) }}
with:
script: |
const assert = require('node:assert/strict');
const normal = JSON.parse(process.env.NORMAL);
const candidate = JSON.parse(process.env.CANDIDATE);
const withoutCreationTime = value => value.split('\n').filter(line => !line.includes('org.opencontainers.image.created='));
assert.equal(candidate.image, normal.image);
for (const key of ['labels', 'annotations']) {
assert.deepEqual(withoutCreationTime(candidate[key]), withoutCreationTime(normal[key]));
}
assert.deepEqual(JSON.parse(candidate.candidate).destinations, normal.tags.split('\n'));
assert.equal(candidate.tags.split('\n').length, 1);
assert(candidate.tags.startsWith(`${normal.image}:candidate-${process.env.GITHUB_REPOSITORY_ID}-${context.runId}-${process.env.GITHUB_RUN_ATTEMPT}-`));
assert(!normal.tags.split('\n').includes(candidate.tags));

tests:
name: Test for "docker/get-image-metadata" action
runs-on: ubuntu-latest
Expand Down
191 changes: 191 additions & 0 deletions .github/workflows/__test-workflow-docker-build-images-building.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,40 @@ permissions: {}

# jscpd:ignore-start
jobs:
candidate-inputs:
name: Assert candidate workflow image selection
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const assert = require('node:assert/strict');
const { stdout } = await exec.getExecOutput('ruby', [
'-rjson', '-ryaml', '-e', 'puts JSON.generate(YAML.safe_load(File.read(ARGV[0])))',
'.github/workflows/docker-build-images.yml',
]);
const script = JSON.parse(stdout).jobs['prepare-variables'].steps.find(step => step.id === 'validate-inputs').with.script;
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;
async function validate(images) {
const outputs = {};
await new AsyncFunction('process', 'context', 'core', script)(
{ env: { CANDIDATE_INPUT: 'true', OCI_REGISTRY_PASSWORD: 'fixture', RUNS_ON_INPUT: '["ubuntu-latest"]', IMAGES_INPUT: JSON.stringify(images) } },
{ repo: { owner: 'example', repo: 'app' } }, { setOutput: (key, value) => { outputs[key] = value; } },
);
return JSON.parse(outputs.images);
}
const single = { name: 'single', platforms: ['linux/amd64'], tag: '1.2.3' };
const multi = { name: 'multi', platforms: ['linux/amd64', 'linux/arm64'] };
assert.equal((await validate([single, multi])).length, 2);
await assert.rejects(validate([]), /non-empty list with unique image names/);
await assert.rejects(validate([single, { ...single, target: 'other' }]), /unique image names/);

act-build-images-args-secrets:
name: Arrange - Build with args, secrets
uses: $/.github/workflows/docker-build-images.yml
Expand Down Expand Up @@ -89,4 +123,161 @@ jobs:
assert(builtImages[image], `"built-images" output does not contain "${image}" image`);
}

act-candidates:
name: Build signed candidates with final metadata
uses: $/.github/workflows/docker-build-images.yml
permissions:
attestations: write
contents: read
id-token: write
issues: read
packages: write
pull-requests: read
secrets:
oci-registry-password: ${{ secrets.GITHUB_TOKEN }}
with:
candidate: true
images: |
[
{
"name": "test-candidate-${{ github.run_id }}-${{ github.run_attempt }}-single",
"dockerfile": "tests/application/Dockerfile",
"target": "base",
"platforms": ["linux/amd64"],
"tag": "1.2.3"
},
{
"name": "test-candidate-${{ github.run_id }}-${{ github.run_attempt }}-multi",
"dockerfile": "tests/application/Dockerfile",
"target": "base",
"platforms": ["linux/amd64", "linux/arm64"],
"tag": "1.2.3"
}
]

assert-candidates:
name: Assert candidate outputs, tags and security metadata
needs: act-candidates
runs-on: ubuntu-latest
permissions:
contents: read
packages: read
attestations: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- uses: $/actions/docker/setup
with:
oci-registry: ghcr.io
oci-registry-username: ${{ github.repository_owner }}
oci-registry-password: ${{ secrets.GITHUB_TOKEN }}

- uses: sigstore/cosign-installer@756fe2d284c7292c4e9b62c8a28f1e5629c59532 # v4.1.3

- uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
BUILT_IMAGES: ${{ needs.act-candidates.outputs.built-images }}
GH_TOKEN: ${{ github.token }}
with:
script: |
const assert = require('node:assert/strict');
const images = Object.values(JSON.parse(process.env.BUILT_IMAGES));
assert.equal(images.length, 2);
for (const image of images) {
const repository = `${image.registry}/${image.repository}`;
assert.deepEqual(image.candidate.destinations, [`${repository}:1.2.3`]);
assert.equal(image.candidate.source.repository, process.env.GITHUB_REPOSITORY);
assert.match(image.candidate.source.revision, /^[a-f0-9]{40}$/);
assert.deepEqual(image.candidate.run, {
id: process.env.GITHUB_RUN_ID, attempt: process.env.GITHUB_RUN_ATTEMPT,
});
assert.equal(image.tags.length, 1);
assert(image.tags[0].startsWith(`candidate-${process.env.GITHUB_REPOSITORY_ID}-${context.runId}-${process.env.GITHUB_RUN_ATTEMPT}-`));
assert.deepEqual(image.images, [`${repository}:${image.tags[0]}@${image.digest}`]);
const reference = `${repository}@${image.digest}`;
const { stdout: inspected } = await exec.getExecOutput('docker', ['buildx', 'imagetools', 'inspect', `${repository}:${image.tags[0]}`]);
assert.equal(inspected.match(/Digest:\s+(sha256:[a-f0-9]{64})/)[1], image.digest);
const { stdout: raw } = await exec.getExecOutput('docker', ['buildx', 'imagetools', 'inspect', '--raw', reference]);
const manifest = JSON.parse(raw);
if (image.name.endsWith('-multi')) {
assert.deepEqual(manifest.manifests.map(item => `${item.platform.os}/${item.platform.architecture}`).sort(), ['linux/amd64', 'linux/arm64']);
assert.equal(manifest.annotations['org.opencontainers.image.version'], '1.2.3');
} else {
assert(manifest.config, 'Single-platform candidate must retain its image manifest');
}
await exec.exec('docker', ['pull', '--platform', 'linux/amd64', reference]);
const { stdout: config } = await exec.getExecOutput('docker', ['image', 'inspect', reference]);
const labels = JSON.parse(config)[0].Config.Labels;
assert.equal(labels['org.opencontainers.image.version'], '1.2.3');
assert.equal(labels['org.opencontainers.image.revision'], image.candidate.source.revision);
const versions = await github.paginate(github.rest.packages.getAllPackageVersionsForPackageOwnedByOrg, {
package_type: 'container', package_name: `${context.repo.repo}/${image.name}`,
org: context.repo.owner, per_page: 100,
});
const tags = versions.flatMap(version => version.metadata.container.tags);
assert(tags.includes(image.tags[0]));
assert(tags.every(tag => tag === image.tags[0] || tag.startsWith('sha256-')), `Unexpected consumer tag: ${tags}`);
await exec.exec('cosign', [
'verify', '--certificate-oidc-issuer', 'https://token.actions.githubusercontent.com',
'--certificate-identity-regexp', `^https://github.com/${process.env.GITHUB_REPOSITORY}/`, reference,
]);
await exec.exec('gh', ['attestation', 'verify', `oci://${reference}`, '--repo', process.env.GITHUB_REPOSITORY]);
}

failed-candidate:
name: Assert failed builds have no candidate output
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- id: failed-build
continue-on-error: true
uses: $/actions/docker/build-image
with:
image: test-candidate-${{ github.run_id }}-${{ github.run_attempt }}-failed
dockerfile: tests/application/Dockerfile
target: test
platform: linux/amd64
candidate: true
tag: "1.2.3"
cache-type: "false"
# The existing test stage rejects missing mandatory build arguments.

- name: Assert failure without outputs
env:
OUTCOME: ${{ steps.failed-build.outcome }}
BUILT_IMAGE: ${{ steps.failed-build.outputs.built-image }}
run: |
test "$OUTCOME" = failure
test -z "$BUILT_IMAGE"

cleanup-candidates:
name: Clean isolated candidate test packages
if: always()
needs: [act-candidates, assert-candidates, failed-candidate]
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
for (const suffix of ['single', 'multi', 'failed']) {
const name = `test-candidate-${context.runId}-${process.env.GITHUB_RUN_ATTEMPT}-${suffix}`;
try {
await github.rest.packages.deletePackageForOrg({
package_type: 'container', package_name: `${context.repo.repo}/${name}`, org: context.repo.owner,
});
} catch (error) {
if (error.status !== 404) throw error;
}
}

# jscpd:ignore-end
Loading
Loading