diff --git a/action.yml b/action.yml index bffdfc7a..e3232645 100644 --- a/action.yml +++ b/action.yml @@ -37,6 +37,10 @@ inputs: required: false default: description: Set the source branches (e.g. refs/heads/main) that are allowed to trigger a push of the dev container image. Leave empty to allow all. + refFilterForPushMode: + required: false + default: exact + description: Match full refs in refFilterForPush exactly (default) or set glob to let '*' match within one slash-delimited segment. eventFilterForPush: required: false default: push diff --git a/docs/github-action.md b/docs/github-action.md index 1a354a42..0218d32c 100644 --- a/docs/github-action.md +++ b/docs/github-action.md @@ -137,6 +137,7 @@ The [`devcontainers/ci` action](https://github.com/marketplace/actions/devcontai | checkoutPath | false | Only used for development/testing | | push | false | Control when images are pushed. Options are `never`, `filter`, `always`. For `filter`, images are pushed if the `refFilterForPush` and `eventFilterForPush` conditions are met. Defaults to `filter` if `imageName` is set, `never` otherwise. | | refFilterForPush | false | Set the source branches (e.g. `refs/heads/main`) that are allowed to trigger a push of the dev container image. Leave empty to allow all (default) | +| refFilterForPushMode | false | Match whole refs in `refFilterForPush` exactly (default) or set `glob` to enable `*` within a single segment. | | eventFilterForPush | false | Set the event names (e.g. `pull_request`, `push`) that are allowed to trigger a push of the dev container image. Defaults to `push`. Leave empty for all | | skipContainerUserIdUpdate | false | For non-root Dev Containers (i.e. where `remoteUser` is specified), the action attempts to make the container user UID and GID match those of the host user. Set this to true to skip this step (defaults to false) | | cacheFrom | false | Specify additional images to use for build caching | @@ -145,6 +146,21 @@ The [`devcontainers/ci` action](https://github.com/marketplace/actions/devcontai | platform | false | Platforms for which the image should be built. If omitted, defaults to the platform of the GitHub Actions Runner. Multiple platforms should be comma separated. When `useNativeRunner` is true, this must be a single platform value. | | useNativeRunner | false | Set to true for native multi-platform builds. When true, `platform` must be a single value and the image tag suffix is auto-derived (e.g., `linux/amd64` becomes `linux-amd64`). Defaults to `false`. | +### Matching source refs with wildcards + +With `push: filter`, `refFilterForPush` matches complete, case-sensitive refs exactly by default. To match release branches without listing each one, opt into glob mode: + +```yaml +with: + imageName: ghcr.io/example/devcontainer + refFilterForPushMode: glob + refFilterForPush: | + refs/heads/main + refs/heads/releases/v* +``` + +This allows `refs/heads/main` and `refs/heads/releases/v1.4` but not `refs/heads/releases/v1/patch`. A `*` matches zero or more characters within one slash-delimited segment. Consecutive stars also stay within one segment; there is no recursive `**` or other glob syntax. An empty list still allows all refs, and the event filter still applies. The mode does not affect `push: always` or `push: never`. + ## Outputs | Name | Description | diff --git a/github-action/__tests__/push-filter.test.ts b/github-action/__tests__/push-filter.test.ts new file mode 100644 index 00000000..a8a448e8 --- /dev/null +++ b/github-action/__tests__/push-filter.test.ts @@ -0,0 +1,128 @@ +import * as core from '@actions/core'; +import {pushImage} from '../src/docker'; +import {runPost} from '../src/main'; +import {copyImage} from '../src/skopeo'; + +jest.mock('@actions/core'); +jest.mock('../src/docker'); +jest.mock('../src/skopeo'); + +describe('post-step ref filters', () => { + let inputs: Record; + let refs: string[]; + let events: string[]; + const environment = process.env; + + beforeEach(() => { + inputs = {imageName: 'example.invalid/devcontainer', push: 'filter'}; + refs = ['refs/heads/main', 'refs/heads/releases/v*']; + events = ['push']; + process.env = { + ...environment, + GITHUB_REF: 'refs/heads/releases/v1.4', + GITHUB_EVENT_NAME: 'push', + }; + jest.mocked(core.getInput).mockImplementation(name => inputs[name] ?? ''); + jest + .mocked(core.getMultilineInput) + .mockImplementation(name => + name === 'refFilterForPush' ? refs : events, + ); + jest.mocked(core.getState).mockReturnValue(''); + }); + + afterEach(() => { + process.env = environment; + }); + + test('an unset mode does not interpret stars in existing filters', async () => { + await runPost(); + + expect(pushImage).not.toHaveBeenCalled(); + expect(copyImage).not.toHaveBeenCalled(); + expect(core.setFailed).not.toHaveBeenCalled(); + }); + + test('the default matches a literal ref in the list', async () => { + process.env.GITHUB_REF = 'refs/heads/main'; + await runPost(); + + expect(pushImage).toHaveBeenCalledWith(inputs.imageName, 'latest'); + }); + + test('glob mode opts into wildcard matching', async () => { + inputs.refFilterForPushMode = 'glob'; + await runPost(); + + expect(pushImage).toHaveBeenCalledWith(inputs.imageName, 'latest'); + }); + + test('glob mode still accepts literal entries alongside patterns', async () => { + inputs.refFilterForPushMode = 'glob'; + process.env.GITHUB_REF = 'refs/heads/main'; + await runPost(); + + expect(pushImage).toHaveBeenCalledWith(inputs.imageName, 'latest'); + }); + + test('a glob does not allow a ref from another namespace', async () => { + inputs.refFilterForPushMode = 'glob'; + process.env.GITHUB_REF = 'refs/tags/releases/v1.4'; + await runPost(); + + expect(pushImage).not.toHaveBeenCalled(); + }); + + test('glob mode still applies the event filter', async () => { + inputs.refFilterForPushMode = 'glob'; + process.env.GITHUB_EVENT_NAME = 'pull_request'; + await runPost(); + + expect(pushImage).not.toHaveBeenCalled(); + }); + + test('an empty ref list still allows all refs', async () => { + inputs.refFilterForPushMode = 'glob'; + refs = []; + await runPost(); + + expect(pushImage).toHaveBeenCalledWith(inputs.imageName, 'latest'); + }); + + test('a missing ref does not match a nonempty glob list', async () => { + inputs.refFilterForPushMode = 'glob'; + delete process.env.GITHUB_REF; + await runPost(); + + expect(pushImage).not.toHaveBeenCalled(); + }); + + test('an invalid mode fails without publishing', async () => { + inputs.refFilterForPushMode = 'GLOB'; + await runPost(); + + expect(core.setFailed).toHaveBeenCalledWith( + expect.stringContaining('Unexpected refFilterForPushMode'), + ); + expect(pushImage).not.toHaveBeenCalled(); + expect(copyImage).not.toHaveBeenCalled(); + }); + + test('always still ignores ref and event filters', async () => { + inputs.push = 'always'; + inputs.refFilterForPushMode = 'glob'; + process.env.GITHUB_REF = 'refs/heads/unrelated'; + process.env.GITHUB_EVENT_NAME = 'pull_request'; + await runPost(); + + expect(pushImage).toHaveBeenCalledWith(inputs.imageName, 'latest'); + }); + + test('never still suppresses a matching push', async () => { + inputs.push = 'never'; + inputs.refFilterForPushMode = 'glob'; + await runPost(); + + expect(pushImage).not.toHaveBeenCalled(); + }); +}); diff --git a/github-action/__tests__/ref-filter.test.ts b/github-action/__tests__/ref-filter.test.ts new file mode 100644 index 00000000..3a2796be --- /dev/null +++ b/github-action/__tests__/ref-filter.test.ts @@ -0,0 +1,24 @@ +import {matchesRefGlob} from '../src/ref-filter'; + +describe('matchesRefGlob', () => { + test.each([ + ['refs/heads/releases/v1.4', 'refs/heads/releases/v*', true], + ['refs/heads/releases/v', 'refs/heads/releases/v*', true], + ['refs/heads/releases/v1/patch', 'refs/heads/releases/v*', false], + ['refs/heads/releases/v1/patch', 'refs/heads/releases/**', false], + ['refs/heads/releases/v1', 'refs/heads/releases/**', true], + ['refs/heads/releases/V1', 'refs/heads/releases/v*', false], + ['refs/tags/releases/v1', 'refs/heads/releases/v*', false], + ['refs/heads/feature/foo/patch', 'refs/heads/*/foo/*', true], + ['refs/heads/v.1+work-rc', 'refs/heads/v.1+*-*', true], + ['refs/heads/vX1+work-rc', 'refs/heads/v.1+*-*', false], + ['refs/heads/abc', 'refs/heads/ab*bc', false], + ['refs/heads/abbc', 'refs/heads/ab*bc', true], + ['refs/heads/aba', 'refs/heads/a*b*a', true], + ['refs/heads/aa', 'refs/heads/a*a*a', false], + ['refs/heads/v[12]', 'refs/heads/v[12]', true], + ['refs/heads/v1', 'refs/heads/v[12]', false], + ])('matches %s against %s: %s', (ref, pattern, expected) => { + expect(matchesRefGlob(pattern, ref)).toBe(expected); + }); +}); diff --git a/github-action/package.json b/github-action/package.json index 81084f27..47adef43 100644 --- a/github-action/package.json +++ b/github-action/package.json @@ -13,7 +13,7 @@ "lint-fix": "eslint --fix src/**/*.ts", "package": "ncc build --source-map --license licenses.txt", "test": "jest", - "all": "npm run tsc-version && npm run build && npm run format && npm run lint && npm run package #&& npm test" + "all": "npm run tsc-version && npm run build && npm run format && npm run lint && npm run package && npm test" }, "repository": { "type": "git", diff --git a/github-action/src/main.ts b/github-action/src/main.ts index 40465057..f97ec332 100644 --- a/github-action/src/main.ts +++ b/github-action/src/main.ts @@ -10,6 +10,7 @@ import { } from '../../common/src/dev-container-cli'; import {isDockerBuildXInstalled, pushImage} from './docker'; +import {matchesRefGlob} from './ref-filter'; import {isSkopeoInstalled, copyImage} from './skopeo'; import {populateDefaults} from '../../common/src/envvars'; import {buildImageNames, platformToTagSuffix} from '../../common/src/platform'; @@ -258,9 +259,21 @@ export async function runPost(): Promise { if (pushOption === 'filter' || (!pushOption && imageName)) { // https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables const ref = process.env.GITHUB_REF; + const refFilterMode = core.getInput('refFilterForPushMode') || 'exact'; + if (refFilterMode !== 'exact' && refFilterMode !== 'glob') { + core.setFailed( + `Unexpected refFilterForPushMode value ('${refFilterMode}'); expected 'exact' or 'glob'`, + ); + return; + } if ( refFilterForPush.length !== 0 && // empty filter allows all - !refFilterForPush.some(s => s === ref) + (ref === undefined || + !refFilterForPush.some(filter => + refFilterMode === 'glob' + ? matchesRefGlob(filter, ref) + : filter === ref, + )) ) { core.info( `Image push skipped because GITHUB_REF (${ref}) is not in refFilterForPush`, diff --git a/github-action/src/ref-filter.ts b/github-action/src/ref-filter.ts new file mode 100644 index 00000000..4c7266de --- /dev/null +++ b/github-action/src/ref-filter.ts @@ -0,0 +1,36 @@ +/** Match a complete, case-sensitive ref. A star never crosses a slash. */ +export function matchesRefGlob(pattern: string, ref: string): boolean { + const patternSegments = pattern.split('/'); + const refSegments = ref.split('/'); + return ( + patternSegments.length === refSegments.length && + patternSegments.every((segment, index) => + matchesSegment(segment, refSegments[index]), + ) + ); +} + +function matchesSegment(pattern: string, segment: string): boolean { + const parts = pattern.split('*'); + if (parts.length === 1) { + return pattern === segment; + } + + const prefix = parts[0]; + const suffix = parts[parts.length - 1]; + const end = segment.length - suffix.length; + if (!segment.startsWith(prefix) || !segment.endsWith(suffix)) { + return false; + } + + // Match the literal pieces left to right without overlapping the suffix. + let position = prefix.length; + for (const part of parts.slice(1, -1)) { + const start = segment.indexOf(part, position); + if (start === -1 || start + part.length > end) { + return false; + } + position = start + part.length; + } + return position <= end; +}