diff --git a/docs/schema/yaml/1.0.0.yaml b/docs/schema/yaml/1.0.0.yaml index 4200cffe..b4a2cd65 100644 --- a/docs/schema/yaml/1.0.0.yaml +++ b/docs/schema/yaml/1.0.0.yaml @@ -261,6 +261,8 @@ services: args: # @param services.helm.nativeHelm.postRenderer.args[] - '' + # @param services.helm.skipImageBuild + skipImageBuild: false # @param services.codefresh codefresh: # @param services.codefresh.repository (required) diff --git a/src/server/lib/jsonschema/schemas/1.0.0.json b/src/server/lib/jsonschema/schemas/1.0.0.json index d8945117..ca6172b3 100644 --- a/src/server/lib/jsonschema/schemas/1.0.0.json +++ b/src/server/lib/jsonschema/schemas/1.0.0.json @@ -532,6 +532,40 @@ } } } + }, + "skipImageBuild": { + "type": "boolean", + "default": false, + "description": "Skip image building for a native Helm service that uses prebuilt images in its chart values. Requires deploymentMethod: native and no docker configuration." + } + }, + "if": { + "properties": { + "skipImageBuild": { + "enum": [ + true + ] + } + }, + "required": [ + "skipImageBuild" + ] + }, + "then": { + "properties": { + "deploymentMethod": { + "enum": [ + "native" + ] + } + }, + "required": [ + "deploymentMethod" + ], + "not": { + "required": [ + "docker" + ] } } }, diff --git a/src/server/lib/nativeHelm/__tests__/helm.test.ts b/src/server/lib/nativeHelm/__tests__/helm.test.ts index 8973e93a..15011c7e 100644 --- a/src/server/lib/nativeHelm/__tests__/helm.test.ts +++ b/src/server/lib/nativeHelm/__tests__/helm.test.ts @@ -70,6 +70,7 @@ jest.mock('server/lib/nativeBuild/utils', () => { return { ...originalModule, waitForJobAndGetLogs: jest.fn(), + getGitHubToken: jest.fn().mockResolvedValue('test-github-token'), }; }); jest.mock('server/services/logArchival', () => ({ @@ -1868,6 +1869,60 @@ describe('Native Helm', () => { }); describe('generateHelmManifest', () => { + it('checks out the pinned chart revision and deploys a local chart with image building disabled', async () => { + mockGetAllConfigs.mockResolvedValue({}); + (nativeHelmUtils.determineChartType as jest.Mock).mockImplementation( + jest.requireActual('../utils').determineChartType + ); + (nativeHelmUtils.mergeHelmConfigWithGlobal as jest.Mock).mockImplementation( + jest.requireActual('../utils').mergeHelmConfigWithGlobal + ); + const deploy = { + uuid: 'gateway-preview', + sha: 'accepted-chart-commit', + branchName: 'chart-main', + id: 42, + deployableId: 99, + deployable: { + name: 'gateway', + repository: { fullName: 'example-org/chart-repo' }, + helm: { + deploymentMethod: 'native', + skipImageBuild: true, + args: '--dependency-update', + chart: { name: './charts/gateway' }, + }, + }, + build: { uuid: 'preview', namespace: 'env-preview', isStatic: false }, + $fetchGraph: jest.fn().mockResolvedValue(undefined), + } as any; + + const manifest = await generateHelmManifest( + deploy, + 'test-job', + { namespace: 'env-preview' }, + { + chartType: ChartType.LOCAL, + customValues: [], + helmSecretRefs: [], + secretSetFiles: [], + valuesFiles: ['charts/gateway/values-preview.yaml'], + chartPath: './charts/gateway', + releaseName: 'gateway-preview', + helmVersion: '3.18.6', + } + ); + const job = yaml.load(manifest) as any; + const clone = job.spec.template.spec.initContainers.find((container) => container.name === 'clone-repo'); + expect(clone).toBeDefined(); + expect(JSON.stringify(clone)).toContain('example-org/chart-repo'); + expect(JSON.stringify(clone)).toContain('accepted-chart-commit'); + const runner = job.spec.template.spec.containers.find((container) => container.name === 'helm-deploy'); + expect(runner.args[0]).toContain('helm upgrade --install gateway-preview ./charts/gateway'); + expect(runner.args[0]).toContain('-f ./charts/gateway/values-preview.yaml'); + expect(runner.args[0]).toContain('--dependency-update'); + }); + it('builds the expected manifest for plain native Helm without a renderer', async () => { mockGetAllConfigs.mockResolvedValue({ publicChart: { block: false }, diff --git a/src/server/lib/yamlSchemas/schema_1_0_0/schema_1_0_0.ts b/src/server/lib/yamlSchemas/schema_1_0_0/schema_1_0_0.ts index c7677c15..09d30f49 100644 --- a/src/server/lib/yamlSchemas/schema_1_0_0/schema_1_0_0.ts +++ b/src/server/lib/yamlSchemas/schema_1_0_0/schema_1_0_0.ts @@ -167,6 +167,17 @@ const schema_1_0_0 = { helm: { type: 'object', additionalProperties: true, + if: { + properties: { skipImageBuild: { enum: [true] } }, + required: ['skipImageBuild'], + }, + then: { + properties: { + deploymentMethod: { enum: ['native'] }, + }, + required: ['deploymentMethod'], + not: { required: ['docker'] }, + }, properties: { cfStepType: { type: 'string' }, type: { type: 'string' }, @@ -176,6 +187,12 @@ const schema_1_0_0 = { repository: { type: 'string' }, branchName: { type: 'string' }, deploymentMethod: { type: 'string', enum: ['native', 'ci'] }, + skipImageBuild: { + type: 'boolean', + default: false, + description: + 'Skip image building for a native Helm service that uses prebuilt images in its chart values. Requires deploymentMethod: native and no docker configuration.', + }, chart: { type: 'object', additionalProperties: false, diff --git a/src/server/models/yaml/YamlService.ts b/src/server/models/yaml/YamlService.ts index 80c0c6fe..9c0bdf74 100644 --- a/src/server/models/yaml/YamlService.ts +++ b/src/server/models/yaml/YamlService.ts @@ -203,6 +203,7 @@ export interface Helm { readonly builder?: Builder; readonly envLens?: boolean; readonly deploymentMethod?: 'native' | 'ci'; + readonly skipImageBuild?: boolean; readonly nativeHelm?: NativeHelmConfig; readonly envMapping?: { readonly app?: { @@ -573,6 +574,14 @@ export async function getHelmConfigFromYaml(service: Service): Promise { const helmConfig = _.merge({}, helmDefaults, chartConfig, helmService); + // Chart-only services must not inherit Docker build inputs from installation defaults. + if (helmService?.skipImageBuild === true) { + if (helmService.docker) { + throw new Error('helm.skipImageBuild cannot be combined with helm.docker'); + } + delete helmConfig.docker; + } + // Preserve value files from service config if specified if (helmService?.chart?.valueFiles?.length > 0) { helmConfig.chart.values = helmService.chart.values; diff --git a/src/server/models/yaml/tests/YamlService.test.ts b/src/server/models/yaml/tests/YamlService.test.ts index 5b1d9e36..bd7346f5 100644 --- a/src/server/models/yaml/tests/YamlService.test.ts +++ b/src/server/models/yaml/tests/YamlService.test.ts @@ -491,6 +491,98 @@ services: }); }); + describe('helm.skipImageBuild', () => { + const configFor = (helm = {}) => ({ + version: '1.0.0', + services: [ + { + name: 'gateway', + helm: { + repository: 'org/chart-repo', + branchName: 'main', + deploymentMethod: 'native', + chart: { name: './charts/gateway' }, + ...helm, + }, + }, + ], + }); + + test.each([undefined, false, true])('accepts boolean opt-in %s', (skipImageBuild) => { + expect(() => new YamlConfigValidator().validate_1_0_0(configFor({ skipImageBuild }) as any)).not.toThrow(); + }); + + test.each(['true', 1, null])('rejects non-boolean opt-in %s', (skipImageBuild) => { + expect(() => new YamlConfigValidator().validate_1_0_0(configFor({ skipImageBuild }) as any)).toThrow(); + }); + + test('identifies the missing native deployment method', () => { + expect(() => + new YamlConfigValidator().validate_1_0_0( + configFor({ skipImageBuild: true, deploymentMethod: undefined }) as any + ) + ).toThrow('requires property "deploymentMethod"'); + }); + + test('identifies an incompatible deployment method', () => { + expect(() => + new YamlConfigValidator().validate_1_0_0(configFor({ skipImageBuild: true, deploymentMethod: 'ci' }) as any) + ).toThrow('helm.deploymentMethod is not one of enum values: native'); + }); + + test('rejects opt-in combined with a Docker build declaration', () => { + expect(() => + new YamlConfigValidator().validate_1_0_0( + configFor({ + skipImageBuild: true, + docker: { defaultTag: 'main', app: { dockerfilePath: 'Dockerfile' } }, + }) as any + ) + ).toThrow(); + }); + + test.each([undefined, false])('preserves legacy Docker declarations when the option is %s', (skipImageBuild) => { + expect(() => + new YamlConfigValidator().validate_1_0_0( + configFor({ + skipImageBuild, + deploymentMethod: 'ci', + docker: { defaultTag: 'main', app: { dockerfilePath: 'Dockerfile' } }, + }) as any + ) + ).not.toThrow(); + }); + + test('explicit opt-in overrides inherited Docker defaults without mutating them', async () => { + const defaults = { + helmDefaults: { docker: { defaultTag: 'main', app: { dockerfilePath: 'default.Dockerfile' } } }, + }; + mockGetAllConfigs.mockResolvedValue(defaults); + const service = configFor({ skipImageBuild: true }).services[0]; + const helm = await YamlService.getHelmConfigFromYaml(service as any); + + expect(helm.skipImageBuild).toBe(true); + expect(helm.chart.name).toBe('./charts/gateway'); + expect(helm.docker).toBeUndefined(); + expect(defaults.helmDefaults.docker.app.dockerfilePath).toBe('default.Dockerfile'); + + const legacyHelm = await YamlService.getHelmConfigFromYaml(configFor().services[0] as any); + expect(legacyHelm.docker.app.dockerfilePath).toBe('default.Dockerfile'); + }); + + test('does not discard an explicit Docker declaration when merging an unchecked service', async () => { + mockGetAllConfigs.mockResolvedValue({}); + const service = configFor({ + skipImageBuild: true, + docker: { defaultTag: 'main', app: { dockerfilePath: 'Dockerfile' } }, + }).services[0]; + + await expect(YamlService.getHelmConfigFromYaml(service as any)).rejects.toThrow( + 'helm.skipImageBuild cannot be combined with helm.docker' + ); + }); + }); + describe('hasLifecycleManagedDockerBuild', () => { test('returns true for github services with docker config', () => { const parser = new YamlConfigParser(); diff --git a/src/server/services/__tests__/deploy.test.ts b/src/server/services/__tests__/deploy.test.ts index 3befda7e..4d33f2c0 100644 --- a/src/server/services/__tests__/deploy.test.ts +++ b/src/server/services/__tests__/deploy.test.ts @@ -659,6 +659,173 @@ describe('DeployService - shouldTriggerGithubDeployment', () => { }); }); + describe('chart-only native Helm services', () => { + const createChartDeploy = (helm = {}) => ({ + id: 17, + uuid: 'gateway-preview', + branchName: 'chart-main', + githubRepositoryId: 84, + build: { + githubRepositoryId: 42, + triggerType: 'pull_request', + branchName: 'app-feature', + configSha: 'app-config-sha', + }, + deployable: { + name: 'gateway', + type: DeployTypes.HELM, + repository: { fullName: 'example-org/chart-repo' }, + helm: { + deploymentMethod: 'native', + chart: { name: './charts/gateway' }, + ...helm, + }, + }, + $fetchGraph: jest.fn().mockResolvedValue(undefined), + }); + + beforeEach(() => { + mockDetermineChartType.mockResolvedValue(ChartType.LOCAL); + (github.getSHAForBranch as jest.Mock).mockReset().mockResolvedValue('chart-sha'); + }); + + test('skips image builders and pins the chart repository revision instead of the app revision', async () => { + const deploy = createChartDeploy({ skipImageBuild: true }); + const patch = jest.spyOn(deployService, 'patchAndUpdateActivityFeed').mockResolvedValue(undefined); + const build = jest.spyOn(deployService, 'buildImageForHelmAndGithub').mockResolvedValue(true); + + await expect( + deployService.buildImage(deploy as any, 0, 'run-1', 'app-push-sha', 42, 'app-feature') + ).resolves.toBe(true); + + expect(github.getSHAForBranch).toHaveBeenCalledWith('chart-main', 'example-org', 'chart-repo'); + expect(build).not.toHaveBeenCalled(); + expect(mockCodefreshBuildImage).not.toHaveBeenCalled(); + expect(mockBuildWithNative).not.toHaveBeenCalled(); + expect(patch).toHaveBeenCalledWith( + deploy, + { + status: DeployStatus.BUILT, + statusMessage: 'Image build skipped; Helm chart uses prebuilt images', + sha: 'chart-sha', + }, + 'run-1' + ); + }); + + test('keeps the accepted chart SHA when replaying a deployment', async () => { + const deploy = createChartDeploy({ skipImageBuild: true }); + const patch = jest.spyOn(deployService, 'patchAndUpdateActivityFeed').mockResolvedValue(undefined); + + await expect( + deployService.buildImage(deploy as any, 0, 'run-1', undefined, undefined, undefined, undefined, undefined, { + '84:chart-main': 'accepted-chart-sha', + }) + ).resolves.toBe(true); + + expect(github.getSHAForBranch).not.toHaveBeenCalled(); + expect(patch).toHaveBeenCalledWith(deploy, expect.objectContaining({ sha: 'accepted-chart-sha' }), 'run-1'); + }); + + test('picks up a new chart commit on a later deployment without building an image', async () => { + const deploy = createChartDeploy({ skipImageBuild: true }); + const patch = jest.spyOn(deployService, 'patchAndUpdateActivityFeed').mockResolvedValue(undefined); + (github.getSHAForBranch as jest.Mock) + .mockResolvedValueOnce('first-chart-sha') + .mockResolvedValueOnce('second-chart-sha'); + + await expect(deployService.buildImage(deploy as any, 0, 'run-1')).resolves.toBe(true); + await expect(deployService.buildImage(deploy as any, 0, 'run-2')).resolves.toBe(true); + + expect(patch).toHaveBeenNthCalledWith(1, deploy, expect.objectContaining({ sha: 'first-chart-sha' }), 'run-1'); + expect(patch).toHaveBeenNthCalledWith(2, deploy, expect.objectContaining({ sha: 'second-chart-sha' }), 'run-2'); + expect(mockCodefreshBuildImage).not.toHaveBeenCalled(); + expect(mockBuildWithNative).not.toHaveBeenCalled(); + }); + + test.each([undefined, false])( + 'retains local chart image building when skipImageBuild is %s', + async (skipImageBuild) => { + const deploy = createChartDeploy({ skipImageBuild }); + const build = jest.spyOn(deployService, 'buildImageForHelmAndGithub').mockResolvedValue(true); + + await expect(deployService.buildImage(deploy as any, 0, 'run-1')).resolves.toBe(true); + expect(build).toHaveBeenCalledTimes(1); + } + ); + + test('retains organization chart image building without the flag', async () => { + mockDetermineChartType.mockResolvedValue(ChartType.ORG_CHART); + const deploy = createChartDeploy(); + const build = jest.spyOn(deployService, 'buildImageForHelmAndGithub').mockResolvedValue(true); + + await expect(deployService.buildImage(deploy as any, 0, 'run-1')).resolves.toBe(true); + expect(build).toHaveBeenCalledTimes(1); + }); + + test.each([ + { skipImageBuild: true, deploymentMethod: 'ci' }, + { skipImageBuild: true, docker: { app: { dockerfilePath: 'Dockerfile' } } }, + ])('rejects conflicting persisted settings before starting a builder (%j)', async (helm) => { + const deploy = createChartDeploy(helm); + const patch = jest.spyOn(deployService, 'patchAndUpdateActivityFeed').mockResolvedValue(undefined); + const build = jest.spyOn(deployService, 'buildImageForHelmAndGithub').mockResolvedValue(true); + + await expect(deployService.buildImage(deploy as any, 0, 'run-1')).resolves.toBe(false); + expect(build).not.toHaveBeenCalled(); + expect(patch).toHaveBeenCalledWith( + deploy, + expect.objectContaining({ + status: DeployStatus.BUILD_FAILED, + statusMessage: expect.stringContaining('helm.skipImageBuild'), + }), + 'run-1' + ); + }); + + test('fails closed when the local chart source revision cannot be resolved', async () => { + const deploy = createChartDeploy({ skipImageBuild: true }); + const patch = jest.spyOn(deployService, 'patchAndUpdateActivityFeed').mockResolvedValue(undefined); + (github.getSHAForBranch as jest.Mock).mockRejectedValue(new Error('Not Found')); + + await expect(deployService.buildImage(deploy as any, 0, 'run-1')).resolves.toBe(false); + expect(patch).toHaveBeenCalledWith( + deploy, + expect.objectContaining({ status: DeployStatus.BUILD_FAILED }), + 'run-1' + ); + expect(patch.mock.calls.some(([, update]) => update.status === DeployStatus.BUILT)).toBe(false); + }); + + test('fails closed when a local chart has no source repository', async () => { + const deploy = createChartDeploy({ skipImageBuild: true }); + (deploy.deployable as any).repository = null; + const patch = jest.spyOn(deployService, 'patchAndUpdateActivityFeed').mockResolvedValue(undefined); + + await expect(deployService.buildImage(deploy as any, 0, 'run-1')).resolves.toBe(false); + expect(github.getSHAForBranch).not.toHaveBeenCalled(); + expect(patch).toHaveBeenCalledWith( + deploy, + expect.objectContaining({ status: DeployStatus.BUILD_FAILED }), + 'run-1' + ); + }); + + test('preserves public charts that do not need a repository revision', async () => { + mockDetermineChartType.mockResolvedValue(ChartType.PUBLIC); + const deploy = createChartDeploy(); + const patch = jest.spyOn(deployService, 'patchAndUpdateActivityFeed').mockResolvedValue(undefined); + (github.getSHAForBranch as jest.Mock).mockRejectedValue(new Error('Not Found')); + + await expect(deployService.buildImage(deploy as any, 0, 'run-1')).resolves.toBe(true); + expect(patch).toHaveBeenCalledWith( + deploy, + { status: DeployStatus.BUILT, statusMessage: 'Helm chart does not need to be built' }, + 'run-1' + ); + }); + }); + describe('failure boundaries', () => { const createNativeAfterBuildDeploy = () => ({ id: 17, diff --git a/src/server/services/deploy.ts b/src/server/services/deploy.ts index 40f9548e..b3a0e2b1 100644 --- a/src/server/services/deploy.ts +++ b/src/server/services/deploy.ts @@ -553,10 +553,17 @@ export default class DeployService extends BaseService { getLogger().info('Image: skipped reason=public status=built'); return true; case DeployTypes.HELM: { + const skipImageBuild = deployable.helm?.skipImageBuild === true; try { const chartType = await determineChartType(deploy); - if (chartType !== ChartType.PUBLIC) { + if (skipImageBuild && (deployable.helm?.deploymentMethod !== 'native' || deployable.helm?.docker)) { + throw new Error( + 'helm.skipImageBuild requires deploymentMethod: native and no helm.docker configuration' + ); + } + + if (chartType !== ChartType.PUBLIC && !skipImageBuild) { return await this.buildImageForHelmAndGithub( deploy, runUUID, @@ -570,8 +577,12 @@ export default class DeployService extends BaseService { } let fullSha = null; + const requiresChartSource = skipImageBuild && chartType === ChartType.LOCAL; await deploy.$fetchGraph('deployable.repository'); + if (requiresChartSource && !deployable.repository) { + throw this.sourceResolutionFailure(null, deploy.branchName); + } if (deploy.deployable?.repository) { try { fullSha = await this.resolveSourceSha( @@ -584,6 +595,7 @@ export default class DeployService extends BaseService { acceptedSourcePins ); } catch (shaError) { + if (requiresChartSource) throw shaError; getLogger().debug( { error: shaError }, 'Could not get SHA for PUBLIC helm chart, continuing without it' @@ -594,14 +606,16 @@ export default class DeployService extends BaseService { deploy, { status: DeployStatus.BUILT, - statusMessage: 'Helm chart does not need to be built', + statusMessage: skipImageBuild + ? 'Image build skipped; Helm chart uses prebuilt images' + : 'Helm chart does not need to be built', ...(fullSha && { sha: fullSha }), }, runUUID ); return true; } catch (error) { - if (error instanceof AuthorityLockLostError) throw error; + if (error instanceof AuthorityLockLostError || skipImageBuild) throw error; getLogger().warn({ error }, 'Helm: deployment processing failed'); return false; }