Skip to content

Commit

Permalink
Merge pull request #150 from crazy-max/check-img
Browse files Browse the repository at this point in the history
check image exists before pulling
  • Loading branch information
crazy-max authored May 3, 2023
2 parents 1226180 + 4d7b2b1 commit 94ca053
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions src/docker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as exec from '@actions/exec';

export async function imageExists(name: string): Promise<boolean> {
return await exec
.getExecOutput('docker', ['images', '-q', name], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
return res.stdout.trim().length > 0;
});
}
54 changes: 29 additions & 25 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as child_process from 'child_process';
import * as core from '@actions/core';
import * as exec from '@actions/exec';

import * as docker from './docker';

async function run() {
try {
if (os.platform() == 'darwin') {
Expand All @@ -22,31 +24,33 @@ async function run() {
return;
}

core.startGroup(`Pulling chocolatey Docker image`);
await exec.exec('docker', ['pull', image]);
core.endGroup();

core.startGroup('Running choco');
fs.writeFileSync('/tmp/env.txt', child_process.execSync(`env`, {encoding: 'utf8'}).trim());
await exec.exec('docker', [
'run',
'--rm',
'--env-file',
'/tmp/env.txt',
'--workdir',
'/wksp',
'--volume',
`${workspace}:/wksp`,
image,
args
]);
core.endGroup();

core.startGroup('Fixing perms');
const uid = parseInt(child_process.execSync(`id -u`, {encoding: 'utf8'}).trim());
const gid = parseInt(child_process.execSync(`id -g`, {encoding: 'utf8'}).trim());
await exec.exec('sudo', ['chown', '-R', `${uid}:${gid}`, workspace]);
core.endGroup();
if (!(await docker.imageExists(image))) {
await core.group(`Pulling chocolatey Docker image`, async () => {
await exec.exec('docker', ['pull', image]);
});
}

await core.group(`Running choco`, async () => {
fs.writeFileSync('/tmp/env.txt', child_process.execSync(`env`, {encoding: 'utf8'}).trim());
await exec.exec('docker', [
'run',
'--rm',
'--env-file',
'/tmp/env.txt',
'--workdir',
'/wksp',
'--volume',
`${workspace}:/wksp`,
image,
args
]);
});

await core.group(`Fixing perms`, async () => {
const uid = parseInt(child_process.execSync(`id -u`, {encoding: 'utf8'}).trim());
const gid = parseInt(child_process.execSync(`id -g`, {encoding: 'utf8'}).trim());
await exec.exec('sudo', ['chown', '-R', `${uid}:${gid}`, workspace]);
});
} catch (error) {
core.setFailed(error.message);
}
Expand Down

0 comments on commit 94ca053

Please sign in to comment.