Skip to content

Commit

Permalink
Add option to add --build-arg's to docker
Browse files Browse the repository at this point in the history
Add test for build args
Update jest and ts-jest to resolve an issue where the test file is missing: kulshekhar/ts-jest#1506
  • Loading branch information
bas-l committed Mar 8, 2021
1 parent e52348a commit 222811c
Show file tree
Hide file tree
Showing 4 changed files with 1,133 additions and 681 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -15,8 +15,8 @@
"@types/jest": "^25.2.1",
"@types/node": "^13.11.0",
"@zeit/ncc": "^0.22.1",
"jest": "^25.2.7",
"ts-jest": "^25.3.1",
"jest": "^26.1.4",
"ts-jest": "^26.1.4",
"typescript": "^3.8.3"
}
}
17 changes: 17 additions & 0 deletions src/docker.test.ts
Expand Up @@ -17,6 +17,7 @@ describe('Docker action', () => {
process.env['INPUT_DOCKERFILE'] = 'Dockerfile';
process.env['INPUT_LATESTBRANCH'] = 'master';
process.env['GITHUB_REF'] = 'refs/heads/develop';
delete process.env['INPUT_BUILDARGS'];
delete process.env['INPUT_CURRENTBRANCH'];
});

Expand Down Expand Up @@ -98,6 +99,22 @@ describe('Docker action', () => {
await expect(setOutput).toHaveBeenCalledWith('dockerTag', 'latest');
});

it('should add the build args as --build-arg to the docker build', async () => {
process.env['INPUT_BUILDARGS'] = 'HOST=localhost\nPORT=8080';
const mockedExec = exec as jest.Mock<Promise<number>>;
mockedExec.mockReturnValue(Promise.resolve(0));

await docker();

await expect(exec).toHaveBeenCalledWith(
'docker build --build-arg HOST=localhost --build-arg PORT=8080 -f Dockerfile -t test-registry.io/HelloWorld:develop .',
[],
{
cwd: 'src',
},
);
});

it('should build the docker image', async () => {
const mockedExec = exec as jest.Mock<Promise<number>>;
mockedExec.mockReturnValue(Promise.resolve(0));
Expand Down
4 changes: 3 additions & 1 deletion src/docker.ts
Expand Up @@ -30,6 +30,7 @@ export const docker = async () => {
const dockerfile = core.getInput('dockerfile', { required: true });
const latestBranch = core.getInput('latestBranch', { required: true });
const currentBranch = core.getInput('currentBranch');
const buildArgs: string[] = core.getInput('buildArgs').split("\n").filter(x => x !== "");

const dockerTag = getDockerTag(
currentBranch !== '' ? currentBranch : process.env['GITHUB_REF']!,
Expand All @@ -45,11 +46,12 @@ export const docker = async () => {
WorkingDirectory: ${workingDirectory}
LatestBranch : ${latestBranch}
DockerTag : ${dockerTag}
BuildArgs : ${buildArgs.join(' ')}
`);

await runInGroup('Building image', async () => {
const buildErrorCode = await exec(
`docker build -f ${dockerfile} -t ${dockerRegistry}/${imageName}:${dockerTag} .`,
`docker build ${buildArgs.map(a => '--build-arg ' + a + ' ').join('')}-f ${dockerfile} -t ${dockerRegistry}/${imageName}:${dockerTag} .`,
[],
{
cwd: workingDirectory,
Expand Down

0 comments on commit 222811c

Please sign in to comment.