Skip to content

Commit

Permalink
feat(circleci): add option to simplify config if releases not tagged
Browse files Browse the repository at this point in the history
  • Loading branch information
ivomurrell committed Feb 24, 2025
1 parent bec67ee commit 6b8b552
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions core/cli/test/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14593,6 +14593,7 @@ exports[`loadConfig should load an invalid config when not validating 1`] = `
"cimgNodeVersions": [
"18.19-browsers",
],
"runOnTag": true,
"tagFilterRegex": "/^v\\d+\\.\\d+\\.\\d+(-.+)?/",
},
"plugin": {
Expand Down
1 change: 1 addition & 0 deletions plugins/circleci/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ _All properties are optional._
| :----------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-------------- | :-------------------------- |
| `cimgNodeVersions` | list of CircleCI [Node.js image versions](https://circleci.com/developer/images/image/cimg/node) to use. if more than one is provided, a [matrix build](https://circleci.com/docs/using-matrix-jobs/) will be generated in your CircleCI config. | `Array<string>` | `["18.19-browsers"]` |
| `cypressImage` | the Cypress docker image to use. see https://github.com/cypress-io/cypress-docker-images for available images and tags. if this option is present, and you're using the [`circleci-deploy`](../circleci-deploy) plugin, this will override the default `node` executor for the `e2e-test-review` and `e2e-test-staging` jobs. | `string` | |
| `runOnTag` | whether you want to run any workflow jobs when a tag is created. you may want to disable this if you don't do versioned releases and you want to keep the generated config simple. | `boolean` | `true` |
| `tagFilterRegex` | the regular expression used to match tags for jobs that should run on tag workflows. by default, matches tags that look like `v1.2.3`; if your releases use a different tag format, change this option to match your tags. | `string` | `'/^v\d+\.\d+\.\d+(-.+)?/'` |

_All properties are optional._
Expand Down
17 changes: 10 additions & 7 deletions plugins/circleci/src/circleci-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const mergeWithConcatenatedArrays = (arg0: unknown, ...args: unknown[]) =>
}
})

const getBaseConfig = (nodeVersions: string[], tagFilterRegex: string): CircleCIState => {
const getBaseConfig = (nodeVersions: string[], tagFilterRegex?: string): CircleCIState => {
const runsOnMultipleNodeVersions = nodeVersions.length > 1
const setupMatrix = runsOnMultipleNodeVersions
? matrixBoilerplate('tool-kit/setup', nodeVersions)
Expand Down Expand Up @@ -162,12 +162,12 @@ const getBaseConfig = (nodeVersions: string[], tagFilterRegex: string): CircleCI
}
},
jobs: [
{ checkout: tagFilter(tagFilterRegex) },
tagFilterRegex ? { checkout: tagFilter(tagFilterRegex) } : 'checkout',
{
'tool-kit/setup': {
...setupMatrix,
requires: ['checkout'],
...tagFilter(tagFilterRegex)
...(tagFilterRegex ? tagFilter(tagFilterRegex) : {})
}
}
]
Expand Down Expand Up @@ -360,7 +360,7 @@ type ExecutorCount = 'none' | 'one' | 'matrix'
const generateWorkflowJobs = (
workflow: CircleCiWorkflow,
nodeVersions: string[],
tagFilterRegex: string,
tagFilterRegex?: string,
customJobs?: CircleCiJob[]
): WorkflowJob[] | undefined => {
// HACK:20250106:IM We were previously implicitly prepending a tool-kit/
Expand Down Expand Up @@ -472,7 +472,9 @@ const generateWorkflowJobs = (
}
})
},
workflow.runOnRelease && (job.runOnRelease ?? true) ? tagFilter(tagFilterRegex) : {},
tagFilterRegex && workflow.runOnRelease && (job.runOnRelease ?? true)
? tagFilter(tagFilterRegex)
: {},
job.custom
)
}
Expand Down Expand Up @@ -587,6 +589,7 @@ export default class CircleCi extends Hook<

generateConfig(): CircleCIState {
const { cimgNodeVersions: nodeVersions, tagFilterRegex } = this.pluginOptions
const configuredTagFilterRegex = this.pluginOptions.runOnTag ? tagFilterRegex : undefined

if (!this.generatedConfig) {
const generated: CircleCIStatePartial = {}
Expand All @@ -608,15 +611,15 @@ export default class CircleCi extends Hook<
generated.workflows = Object.fromEntries(
this.options.workflows.map((workflow) => {
const generatedJobs = {
jobs: generateWorkflowJobs(workflow, nodeVersions, tagFilterRegex, this.options.jobs)
jobs: generateWorkflowJobs(workflow, nodeVersions, configuredTagFilterRegex, this.options.jobs)
}
return [workflow.name, mergeWithConcatenatedArrays(generatedJobs, workflow.custom)]
})
)
}
const generatedConfig = mergeWithConcatenatedArrays(
{},
this.options.disableBaseConfig ? {} : getBaseConfig(nodeVersions, tagFilterRegex),
this.options.disableBaseConfig ? {} : getBaseConfig(nodeVersions, configuredTagFilterRegex),
generated,
this.options.custom ?? {}
)
Expand Down
6 changes: 6 additions & 0 deletions plugins/circleci/src/schemas/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export default z
.describe(
"the Cypress docker image to use. see https://github.com/cypress-io/cypress-docker-images for available images and tags. if this option is present, and you're using the [`circleci-deploy`](../circleci-deploy) plugin, this will override the default `node` executor for the `e2e-test-review` and `e2e-test-staging` jobs."
),
runOnTag: z
.boolean()
.default(true)
.describe(
"whether you want to run any workflow jobs when a tag is created. you may want to disable this if you don't do versioned releases and you want to keep the generated config simple."
),
tagFilterRegex: z
.string()
.default(/^v\d+\.\d+\.\d+(-.+)?/.toString())
Expand Down

0 comments on commit 6b8b552

Please sign in to comment.