Do not set --iidfile flag if local and tar exporters are used
More tests for context module Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									d90dfadeb5
								
							
						
					
					
						commit
						08566ae0e1
					
				| @ -1,10 +1,24 @@ | |||||||
| import fs from 'fs'; | import * as fs from 'fs'; | ||||||
|  | import * as path from 'path'; | ||||||
| import * as semver from 'semver'; | import * as semver from 'semver'; | ||||||
| import * as buildx from '../src/buildx'; | import * as buildx from '../src/buildx'; | ||||||
| import * as exec from '@actions/exec'; | import * as exec from '@actions/exec'; | ||||||
|  | import * as context from '../src/context'; | ||||||
| 
 | 
 | ||||||
| const digest = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9'; | const digest = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9'; | ||||||
| 
 | 
 | ||||||
|  | jest.spyOn(context, 'tmpDir').mockImplementation((): string => { | ||||||
|  |   const tmpDir = path.join('/tmp/.docker-build-push-jest').split(path.sep).join(path.posix.sep); | ||||||
|  |   if (!fs.existsSync(tmpDir)) { | ||||||
|  |     fs.mkdirSync(tmpDir, {recursive: true}); | ||||||
|  |   } | ||||||
|  |   return tmpDir; | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => { | ||||||
|  |   return path.join('/tmp/.docker-build-push-jest', '.tmpname-jest').split(path.sep).join(path.posix.sep); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
| describe('getImageID', () => { | describe('getImageID', () => { | ||||||
|   it('matches', async () => { |   it('matches', async () => { | ||||||
|     const imageIDFile = await buildx.getImageIDFile(); |     const imageIDFile = await buildx.getImageIDFile(); | ||||||
|  | |||||||
| @ -1,4 +1,125 @@ | |||||||
|  | import * as fs from 'fs'; | ||||||
|  | import * as path from 'path'; | ||||||
| import * as context from '../src/context'; | import * as context from '../src/context'; | ||||||
|  | import * as buildx from '../src/buildx'; | ||||||
|  | 
 | ||||||
|  | jest.spyOn(context, 'defaultContext').mockImplementation((): string => { | ||||||
|  |   return 'https://github.com/docker/build-push-action.git#test-jest'; | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | jest.spyOn(context, 'tmpDir').mockImplementation((): string => { | ||||||
|  |   const tmpDir = path.join('/tmp/.docker-build-push-jest').split(path.sep).join(path.posix.sep); | ||||||
|  |   if (!fs.existsSync(tmpDir)) { | ||||||
|  |     fs.mkdirSync(tmpDir, {recursive: true}); | ||||||
|  |   } | ||||||
|  |   return tmpDir; | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | jest.spyOn(context, 'tmpNameSync').mockImplementation((): string => { | ||||||
|  |   return path.join('/tmp/.docker-build-push-jest', '.tmpname-jest').split(path.sep).join(path.posix.sep); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | describe('getArgs', () => { | ||||||
|  |   beforeEach(() => { | ||||||
|  |     process.env = Object.keys(process.env).reduce((object, key) => { | ||||||
|  |       if (!key.startsWith('INPUT_')) { | ||||||
|  |         object[key] = process.env[key]; | ||||||
|  |       } | ||||||
|  |       return object; | ||||||
|  |     }, {}); | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  |   // prettier-ignore
 | ||||||
|  |   test.each([ | ||||||
|  |     [ | ||||||
|  |       '0.4.2', | ||||||
|  |       new Map<string, string>([ | ||||||
|  |         // noop
 | ||||||
|  |       ]), | ||||||
|  |       [ | ||||||
|  |         'buildx', | ||||||
|  |         'build', | ||||||
|  |         '--iidfile', | ||||||
|  |         '/tmp/.docker-build-push-jest/iidfile', | ||||||
|  |         '--file', | ||||||
|  |         'Dockerfile', | ||||||
|  |         'https://github.com/docker/build-push-action.git#test-jest' | ||||||
|  |       ] | ||||||
|  |     ], | ||||||
|  |     [ | ||||||
|  |       '0.4.2', | ||||||
|  |       new Map<string, string>([ | ||||||
|  |         ['context', '.'], | ||||||
|  |         ['outputs', 'type=local,dest=./release-out'] | ||||||
|  |       ]), | ||||||
|  |       [ | ||||||
|  |         'buildx', | ||||||
|  |         'build', | ||||||
|  |         '--output', 'type=local,dest=./release-out', | ||||||
|  |         '--file', 'Dockerfile', | ||||||
|  |         '.' | ||||||
|  |       ] | ||||||
|  |     ], | ||||||
|  |     [ | ||||||
|  |       '0.4.1', | ||||||
|  |       new Map<string, string>([ | ||||||
|  |         ['context', '.'] | ||||||
|  |       ]), | ||||||
|  |       [ | ||||||
|  |         'buildx', | ||||||
|  |         'build', | ||||||
|  |         '--file', 'Dockerfile', | ||||||
|  |         '.' | ||||||
|  |       ] | ||||||
|  |     ], | ||||||
|  |     [ | ||||||
|  |       '0.4.2', | ||||||
|  |       new Map<string, string>([ | ||||||
|  |         ['context', '.'], | ||||||
|  |         ['secrets', 'GIT_AUTH_TOKEN=abcdefghijklmno0123456789'], | ||||||
|  |       ]), | ||||||
|  |       [ | ||||||
|  |         'buildx', | ||||||
|  |         'build', | ||||||
|  |         '--iidfile', | ||||||
|  |         '/tmp/.docker-build-push-jest/iidfile', | ||||||
|  |         '--secret', | ||||||
|  |         'id=GIT_AUTH_TOKEN,src=/tmp/.docker-build-push-jest/.tmpname-jest', | ||||||
|  |         '--file', 'Dockerfile', | ||||||
|  |         '.' | ||||||
|  |       ] | ||||||
|  |     ], | ||||||
|  |     [ | ||||||
|  |       '0.4.2', | ||||||
|  |       new Map<string, string>([ | ||||||
|  |         ['github-token', 'abcdefghijklmno0123456789'] | ||||||
|  |       ]), | ||||||
|  |       [ | ||||||
|  |         'buildx', | ||||||
|  |         'build', | ||||||
|  |         '--iidfile', | ||||||
|  |         '/tmp/.docker-build-push-jest/iidfile', | ||||||
|  |         '--secret', | ||||||
|  |         'id=GIT_AUTH_TOKEN,src=/tmp/.docker-build-push-jest/.tmpname-jest', | ||||||
|  |         '--file', 'Dockerfile', | ||||||
|  |         'https://github.com/docker/build-push-action.git#test-jest' | ||||||
|  |       ] | ||||||
|  |     ] | ||||||
|  |   ])( | ||||||
|  |     'given %p with %p as inputs, returns %p', | ||||||
|  |     async (buildxVersion: string, inputs: Map<string, any>, expected: Array<string>) => { | ||||||
|  |       await inputs.forEach((value: string, name: string) => { | ||||||
|  |         setInput(name, value); | ||||||
|  |       }); | ||||||
|  |       const defContext = context.defaultContext(); | ||||||
|  |       const inp = await context.getInputs(defContext); | ||||||
|  |       console.log(inp); | ||||||
|  |       const res = await context.getArgs(inp, defContext, buildxVersion); | ||||||
|  |       console.log(res); | ||||||
|  |       expect(res).toEqual(expected); | ||||||
|  |     } | ||||||
|  |   ); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| describe('getInputList', () => { | describe('getInputList', () => { | ||||||
|   it('handles single line correctly', async () => { |   it('handles single line correctly', async () => { | ||||||
|  | |||||||
							
								
								
									
										511
									
								
								dist/index.js
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										511
									
								
								dist/index.js
									
									
									
										generated
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| @ -1,5 +1,5 @@ | |||||||
| module.exports = { | module.exports = { | ||||||
|   clearMocks: true, |   clearMocks: false, | ||||||
|   moduleFileExtensions: ['js', 'ts'], |   moduleFileExtensions: ['js', 'ts'], | ||||||
|   setupFiles: ["dotenv/config"], |   setupFiles: ["dotenv/config"], | ||||||
|   testEnvironment: 'node', |   testEnvironment: 'node', | ||||||
|  | |||||||
| @ -1,12 +1,11 @@ | |||||||
| import fs from 'fs'; | import fs from 'fs'; | ||||||
| import path from 'path'; | import path from 'path'; | ||||||
| import tmp from 'tmp'; |  | ||||||
| import * as semver from 'semver'; | import * as semver from 'semver'; | ||||||
| import * as context from './context'; | import * as context from './context'; | ||||||
| import * as exec from './exec'; | import * as exec from './exec'; | ||||||
| 
 | 
 | ||||||
| export async function getImageIDFile(): Promise<string> { | export async function getImageIDFile(): Promise<string> { | ||||||
|   return path.join(context.tmpDir, 'iidfile'); |   return path.join(context.tmpDir(), 'iidfile').split(path.sep).join(path.posix.sep); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function getImageID(): Promise<string | undefined> { | export async function getImageID(): Promise<string | undefined> { | ||||||
| @ -19,8 +18,8 @@ export async function getImageID(): Promise<string | undefined> { | |||||||
| 
 | 
 | ||||||
| export async function getSecret(kvp: string): Promise<string> { | export async function getSecret(kvp: string): Promise<string> { | ||||||
|   const [key, value] = kvp.split('='); |   const [key, value] = kvp.split('='); | ||||||
|   const secretFile = tmp.tmpNameSync({ |   const secretFile = context.tmpNameSync({ | ||||||
|     tmpdir: context.tmpDir |     tmpdir: context.tmpDir() | ||||||
|   }); |   }); | ||||||
|   await fs.writeFileSync(secretFile, value); |   await fs.writeFileSync(secretFile, value); | ||||||
|   return `id=${key},src=${secretFile}`; |   return `id=${key},src=${secretFile}`; | ||||||
|  | |||||||
| @ -2,15 +2,11 @@ import * as fs from 'fs'; | |||||||
| import * as os from 'os'; | import * as os from 'os'; | ||||||
| import * as path from 'path'; | import * as path from 'path'; | ||||||
| import * as semver from 'semver'; | import * as semver from 'semver'; | ||||||
|  | import * as tmp from 'tmp'; | ||||||
| import * as buildx from './buildx'; | import * as buildx from './buildx'; | ||||||
| import * as core from '@actions/core'; | import * as core from '@actions/core'; | ||||||
| import * as github from '@actions/github'; | import * as github from '@actions/github'; | ||||||
| 
 | 
 | ||||||
| export const tmpDir: string = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-build-push-')); |  | ||||||
| const defaultContext: string = `https://github.com/${github.context.repo.owner}/${ |  | ||||||
|   github.context.repo.repo |  | ||||||
| }.git#${github.context.ref.replace(/^refs\//, '')}`;
 |  | ||||||
| 
 |  | ||||||
| export interface Inputs { | export interface Inputs { | ||||||
|   context: string; |   context: string; | ||||||
|   file: string; |   file: string; | ||||||
| @ -32,7 +28,21 @@ export interface Inputs { | |||||||
|   githubToken: string; |   githubToken: string; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function getInputs(): Promise<Inputs> { | export function defaultContext(): string { | ||||||
|  |   return `https://github.com/${github.context.repo.owner}/${ | ||||||
|  |     github.context.repo.repo | ||||||
|  |   }.git#${github.context?.ref?.replace(/^refs\//, '')}`;
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export function tmpDir(): string { | ||||||
|  |   return fs.mkdtempSync(path.join(os.tmpdir(), 'docker-build-push-')).split(path.sep).join(path.posix.sep); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export function tmpNameSync(options?: tmp.TmpNameOptions): string { | ||||||
|  |   return tmp.tmpNameSync(options); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export async function getInputs(defaultContext: string): Promise<Inputs> { | ||||||
|   return { |   return { | ||||||
|     context: core.getInput('context') || defaultContext, |     context: core.getInput('context') || defaultContext, | ||||||
|     file: core.getInput('file') || 'Dockerfile', |     file: core.getInput('file') || 'Dockerfile', | ||||||
| @ -55,15 +65,15 @@ export async function getInputs(): Promise<Inputs> { | |||||||
|   }; |   }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function getArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> { | export async function getArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> { | ||||||
|   let args: Array<string> = ['buildx']; |   let args: Array<string> = ['buildx']; | ||||||
|   args.push.apply(args, await getBuildArgs(inputs, buildxVersion)); |   args.push.apply(args, await getBuildArgs(inputs, defaultContext, buildxVersion)); | ||||||
|   args.push.apply(args, await getCommonArgs(inputs)); |   args.push.apply(args, await getCommonArgs(inputs)); | ||||||
|   args.push(inputs.context); |   args.push(inputs.context); | ||||||
|   return args; |   return args; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function getBuildArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> { | async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> { | ||||||
|   let args: Array<string> = ['build']; |   let args: Array<string> = ['build']; | ||||||
|   await asyncForEach(inputs.buildArgs, async buildArg => { |   await asyncForEach(inputs.buildArgs, async buildArg => { | ||||||
|     args.push('--build-arg', buildArg); |     args.push('--build-arg', buildArg); | ||||||
| @ -83,12 +93,17 @@ async function getBuildArgs(inputs: Inputs, buildxVersion: string): Promise<Arra | |||||||
|   if (inputs.platforms.length > 0) { |   if (inputs.platforms.length > 0) { | ||||||
|     args.push('--platform', inputs.platforms.join(',')); |     args.push('--platform', inputs.platforms.join(',')); | ||||||
|   } |   } | ||||||
|   if (inputs.platforms.length == 0 || semver.satisfies(buildxVersion, '>=0.4.2')) { |   let isLocalOrTarExporter: boolean = false; | ||||||
|     args.push('--iidfile', await buildx.getImageIDFile()); |  | ||||||
|   } |  | ||||||
|   await asyncForEach(inputs.outputs, async output => { |   await asyncForEach(inputs.outputs, async output => { | ||||||
|  |     if (output.startsWith('type=local') || output.startsWith('type=tar')) { | ||||||
|  |       isLocalOrTarExporter = true; | ||||||
|  |     } | ||||||
|     args.push('--output', output); |     args.push('--output', output); | ||||||
|   }); |   }); | ||||||
|  |   // TODO: Remove platforms length cond when buildx >0.4.2 available on runner (docker/buildx#351)
 | ||||||
|  |   if (inputs.platforms.length == 0 && !isLocalOrTarExporter && semver.satisfies(buildxVersion, '>=0.4.2')) { | ||||||
|  |     args.push('--iidfile', await buildx.getImageIDFile()); | ||||||
|  |   } | ||||||
|   await asyncForEach(inputs.cacheFrom, async cacheFrom => { |   await asyncForEach(inputs.cacheFrom, async cacheFrom => { | ||||||
|     args.push('--cache-from', cacheFrom); |     args.push('--cache-from', cacheFrom); | ||||||
|   }); |   }); | ||||||
|  | |||||||
| @ -17,15 +17,16 @@ async function run(): Promise<void> { | |||||||
|       core.setFailed(`Buildx is required. See https://github.com/docker/setup-buildx-action to set up buildx.`); |       core.setFailed(`Buildx is required. See https://github.com/docker/setup-buildx-action to set up buildx.`); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     stateHelper.setTmpDir(context.tmpDir); |     stateHelper.setTmpDir(context.tmpDir()); | ||||||
| 
 | 
 | ||||||
|     const buildxVersion = await buildx.getVersion(); |     const buildxVersion = await buildx.getVersion(); | ||||||
|     core.info(`📣 Buildx version: ${buildxVersion}`); |     core.info(`📣 Buildx version: ${buildxVersion}`); | ||||||
| 
 | 
 | ||||||
|     let inputs: context.Inputs = await context.getInputs(); |     const defContext = context.defaultContext(); | ||||||
|  |     let inputs: context.Inputs = await context.getInputs(defContext); | ||||||
| 
 | 
 | ||||||
|     core.info(`🏃 Starting build...`); |     core.info(`🏃 Starting build...`); | ||||||
|     const args: string[] = await context.getArgs(inputs, buildxVersion); |     const args: string[] = await context.getArgs(inputs, defContext, buildxVersion); | ||||||
|     await exec.exec('docker', args); |     await exec.exec('docker', args); | ||||||
| 
 | 
 | ||||||
|     const imageID = await buildx.getImageID(); |     const imageID = await buildx.getImageID(); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 CrazyMax
						CrazyMax