Skip to content

Commit

Permalink
Add pull image option (#102)
Browse files Browse the repository at this point in the history
Added option to skip pulling OCI images, instead trying to use images already locally cached.  

This is useful when you want to build or download the image separately and then run the upload action just for uploading the charm.
  • Loading branch information
kian99 authored Jul 24, 2023
1 parent b05490d commit 631c2d9
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 36 deletions.
12 changes: 8 additions & 4 deletions dist/channel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22723,8 +22723,10 @@ const yaml = __importStar(__nccwpck_require__(1917));
const docker_1 = __nccwpck_require__(7585);
/* eslint-disable camelcase */
class Charmcraft {
constructor(token) {
constructor(token, pullImage) {
this.uploadImage = core.getInput('upload-image').toLowerCase() === 'true';
this.pullImage =
pullImage || core.getInput('pull-image').toLowerCase() === 'true';
this.token = token || core.getInput('credentials');
this.execOptions = {
env: Object.assign(Object.assign({}, process.env), { CHARMCRAFT_AUTH: this.token }),
Expand Down Expand Up @@ -22785,9 +22787,11 @@ class Charmcraft {
}
uploadResource(resource_image, name, resource_name) {
return __awaiter(this, void 0, void 0, function* () {
const pullExitCode = yield (0, exec_1.exec)('docker', ['pull', resource_image], this.execOptions);
if (pullExitCode !== 0) {
throw new Error('Could not pull the docker image.');
if (this.pullImage) {
const pullExitCode = yield (0, exec_1.exec)('docker', ['pull', resource_image], this.execOptions);
if (pullExitCode !== 0) {
throw new Error('Could not pull the docker image.');
}
}
const resourceDigest = yield (0, docker_1.getImageDigest)(resource_image);
const args = [
Expand Down
12 changes: 8 additions & 4 deletions dist/check-libraries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22916,8 +22916,10 @@ const yaml = __importStar(__nccwpck_require__(1917));
const docker_1 = __nccwpck_require__(7585);
/* eslint-disable camelcase */
class Charmcraft {
constructor(token) {
constructor(token, pullImage) {
this.uploadImage = core.getInput('upload-image').toLowerCase() === 'true';
this.pullImage =
pullImage || core.getInput('pull-image').toLowerCase() === 'true';
this.token = token || core.getInput('credentials');
this.execOptions = {
env: Object.assign(Object.assign({}, process.env), { CHARMCRAFT_AUTH: this.token }),
Expand Down Expand Up @@ -22978,9 +22980,11 @@ class Charmcraft {
}
uploadResource(resource_image, name, resource_name) {
return __awaiter(this, void 0, void 0, function* () {
const pullExitCode = yield (0, exec_1.exec)('docker', ['pull', resource_image], this.execOptions);
if (pullExitCode !== 0) {
throw new Error('Could not pull the docker image.');
if (this.pullImage) {
const pullExitCode = yield (0, exec_1.exec)('docker', ['pull', resource_image], this.execOptions);
if (pullExitCode !== 0) {
throw new Error('Could not pull the docker image.');
}
}
const resourceDigest = yield (0, docker_1.getImageDigest)(resource_image);
const args = [
Expand Down
12 changes: 8 additions & 4 deletions dist/release-charm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22815,8 +22815,10 @@ const yaml = __importStar(__nccwpck_require__(1917));
const docker_1 = __nccwpck_require__(7585);
/* eslint-disable camelcase */
class Charmcraft {
constructor(token) {
constructor(token, pullImage) {
this.uploadImage = core.getInput('upload-image').toLowerCase() === 'true';
this.pullImage =
pullImage || core.getInput('pull-image').toLowerCase() === 'true';
this.token = token || core.getInput('credentials');
this.execOptions = {
env: Object.assign(Object.assign({}, process.env), { CHARMCRAFT_AUTH: this.token }),
Expand Down Expand Up @@ -22877,9 +22879,11 @@ class Charmcraft {
}
uploadResource(resource_image, name, resource_name) {
return __awaiter(this, void 0, void 0, function* () {
const pullExitCode = yield (0, exec_1.exec)('docker', ['pull', resource_image], this.execOptions);
if (pullExitCode !== 0) {
throw new Error('Could not pull the docker image.');
if (this.pullImage) {
const pullExitCode = yield (0, exec_1.exec)('docker', ['pull', resource_image], this.execOptions);
if (pullExitCode !== 0) {
throw new Error('Could not pull the docker image.');
}
}
const resourceDigest = yield (0, docker_1.getImageDigest)(resource_image);
const args = [
Expand Down
12 changes: 8 additions & 4 deletions dist/release-libraries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22946,8 +22946,10 @@ const yaml = __importStar(__nccwpck_require__(1917));
const docker_1 = __nccwpck_require__(7585);
/* eslint-disable camelcase */
class Charmcraft {
constructor(token) {
constructor(token, pullImage) {
this.uploadImage = core.getInput('upload-image').toLowerCase() === 'true';
this.pullImage =
pullImage || core.getInput('pull-image').toLowerCase() === 'true';
this.token = token || core.getInput('credentials');
this.execOptions = {
env: Object.assign(Object.assign({}, process.env), { CHARMCRAFT_AUTH: this.token }),
Expand Down Expand Up @@ -23008,9 +23010,11 @@ class Charmcraft {
}
uploadResource(resource_image, name, resource_name) {
return __awaiter(this, void 0, void 0, function* () {
const pullExitCode = yield (0, exec_1.exec)('docker', ['pull', resource_image], this.execOptions);
if (pullExitCode !== 0) {
throw new Error('Could not pull the docker image.');
if (this.pullImage) {
const pullExitCode = yield (0, exec_1.exec)('docker', ['pull', resource_image], this.execOptions);
if (pullExitCode !== 0) {
throw new Error('Could not pull the docker image.');
}
}
const resourceDigest = yield (0, docker_1.getImageDigest)(resource_image);
const args = [
Expand Down
12 changes: 8 additions & 4 deletions dist/upload-bundle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22794,8 +22794,10 @@ const yaml = __importStar(__nccwpck_require__(1917));
const docker_1 = __nccwpck_require__(7585);
/* eslint-disable camelcase */
class Charmcraft {
constructor(token) {
constructor(token, pullImage) {
this.uploadImage = core.getInput('upload-image').toLowerCase() === 'true';
this.pullImage =
pullImage || core.getInput('pull-image').toLowerCase() === 'true';
this.token = token || core.getInput('credentials');
this.execOptions = {
env: Object.assign(Object.assign({}, process.env), { CHARMCRAFT_AUTH: this.token }),
Expand Down Expand Up @@ -22856,9 +22858,11 @@ class Charmcraft {
}
uploadResource(resource_image, name, resource_name) {
return __awaiter(this, void 0, void 0, function* () {
const pullExitCode = yield (0, exec_1.exec)('docker', ['pull', resource_image], this.execOptions);
if (pullExitCode !== 0) {
throw new Error('Could not pull the docker image.');
if (this.pullImage) {
const pullExitCode = yield (0, exec_1.exec)('docker', ['pull', resource_image], this.execOptions);
if (pullExitCode !== 0) {
throw new Error('Could not pull the docker image.');
}
}
const resourceDigest = yield (0, docker_1.getImageDigest)(resource_image);
const args = [
Expand Down
12 changes: 8 additions & 4 deletions dist/upload-charm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22825,8 +22825,10 @@ const yaml = __importStar(__nccwpck_require__(1917));
const docker_1 = __nccwpck_require__(7585);
/* eslint-disable camelcase */
class Charmcraft {
constructor(token) {
constructor(token, pullImage) {
this.uploadImage = core.getInput('upload-image').toLowerCase() === 'true';
this.pullImage =
pullImage || core.getInput('pull-image').toLowerCase() === 'true';
this.token = token || core.getInput('credentials');
this.execOptions = {
env: Object.assign(Object.assign({}, process.env), { CHARMCRAFT_AUTH: this.token }),
Expand Down Expand Up @@ -22887,9 +22889,11 @@ class Charmcraft {
}
uploadResource(resource_image, name, resource_name) {
return __awaiter(this, void 0, void 0, function* () {
const pullExitCode = yield (0, exec_1.exec)('docker', ['pull', resource_image], this.execOptions);
if (pullExitCode !== 0) {
throw new Error('Could not pull the docker image.');
if (this.pullImage) {
const pullExitCode = yield (0, exec_1.exec)('docker', ['pull', resource_image], this.execOptions);
if (pullExitCode !== 0) {
throw new Error('Could not pull the docker image.');
}
}
const resourceDigest = yield (0, docker_1.getImageDigest)(resource_image);
const args = [
Expand Down
2 changes: 1 addition & 1 deletion src/services/charmcraft/charmcraft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('the charmcraft service', () => {
describe('pulling images', () => {
it('should succeed when image digest is available', async () => {
const digest = `somedigest`;
const charmcraft = new Charmcraft('token');
const charmcraft = new Charmcraft('token', true);

const mockedExec = jest.spyOn(exec, 'exec').mockResolvedValue(0);

Expand Down
21 changes: 13 additions & 8 deletions src/services/charmcraft/charmcraft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ import { Base, Mapping, Status, Track } from './types';

class Charmcraft {
private uploadImage: boolean;
private pullImage: boolean;
private token: string;
private execOptions: ExecOptions;

constructor(token?: string) {
constructor(token?: string, pullImage?: boolean) {
this.uploadImage = core.getInput('upload-image').toLowerCase() === 'true';
this.pullImage =
pullImage || core.getInput('pull-image').toLowerCase() === 'true';
this.token = token || core.getInput('credentials');
this.execOptions = {
env: {
Expand Down Expand Up @@ -108,13 +111,15 @@ class Charmcraft {
name: string,
resource_name: string
) {
const pullExitCode = await exec(
'docker',
['pull', resource_image],
this.execOptions
);
if (pullExitCode !== 0) {
throw new Error('Could not pull the docker image.');
if (this.pullImage) {
const pullExitCode = await exec(
'docker',
['pull', resource_image],
this.execOptions
);
if (pullExitCode !== 0) {
throw new Error('Could not pull the docker image.');
}
}

const resourceDigest = await getImageDigest(resource_image);
Expand Down
7 changes: 4 additions & 3 deletions upload-charm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ If you want to use a new resource, you'll have to cut a new resource revision **
| `charm-path` | Path to the charm we want to publish. Defaults to the current working directory. | |
| `built-charm-path` | Path to a pre-built charm we want to publish. | |
| `channel` | Channel on charmhub to publish the charm in. Defaults to `latest/edge`. | |
| `credentials` | Credentials [exported](https://juju.is/docs/sdk/remote-env-auth) using `charmcraft login --export`. | ✔️ |
| `destructive-mode` | Whether or not to pack using destructive mode. Defaults to `true`. | |
| `github-token` | Github Token needed for automatic tagging when publishing | ✔️ |
| `credentials` | Credentials [exported](https://juju.is/docs/sdk/remote-env-auth) using `charmcraft login --export`. | ✔️ |
| `destructive-mode` | Whether or not to pack using destructive mode. Defaults to `true`. | |
| `github-token` | Github Token needed for automatic tagging when publishing | ✔️ |
| `tag-prefix` | Tag prefix, useful when bundling multiple charms in the same repo using a matrix. | |
| `upload-image` | Toggles whether image resources are uploaded to CharmHub or not. Defaults to `true`. | |
| `pull-image` | Toggles whether image resources are pulled. Defaults to `true`. | |
| `charmcraft-channel` | Snap channel to use when installing charmcraft. Defaults to `latest/edge`. | |
| `resource-overrides` | Charm resource revision overrides. Separate entries using commas, ie. `"promql-transform:2,prometheus-image:12"` | |
### Outputs
Expand Down
8 changes: 8 additions & 0 deletions upload-charm/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ inputs:
default: 'true'
description: |
Set to false if you don't want to update the OCI image
pull-image:
required: false
default: 'true'
description: |
Set to false if you want to skip pulling OCI images. If false,
the OCI images must be present in the local docker registry prior
to running the action. Set the upstream-source key in metadata.yaml
to the name of the local image.
credentials:
required: true
description: |
Expand Down

0 comments on commit 631c2d9

Please sign in to comment.