Skip to content

Commit

Permalink
Adds support for pre-built charms in the upload-charm action (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
gruyaume authored May 24, 2023
1 parent 1077281 commit a4a19ea
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 50 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,11 @@ Finally, build the actions by running:
```
$ npm run build
```

### Tests

Run unit tests:

```
$ npm test
```
11 changes: 6 additions & 5 deletions dist/channel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22846,21 +22846,22 @@ class Charmcraft {
if (destructive)
args.push('--destructive-mode');
yield (0, exec_1.exec)('sudo', args, this.execOptions);
});
}
upload(channel, flags) {
return __awaiter(this, void 0, void 0, function* () {
// as we don't know the name of the name of the charm file output, we'll need to glob for it.
// however, we expect charmcraft pack to always output one charm file.
const globber = yield glob.create('./*.charm');
const paths = yield globber.glob();
return paths[0];
});
}
upload(charm, channel, flags) {
return __awaiter(this, void 0, void 0, function* () {
const args = [
'upload',
'--format',
'json',
'--release',
channel,
paths[0],
charm,
...flags,
];
const result = yield (0, exec_1.getExecOutput)('charmcraft', args, this.execOptions);
Expand Down
11 changes: 6 additions & 5 deletions dist/check-libraries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23039,21 +23039,22 @@ class Charmcraft {
if (destructive)
args.push('--destructive-mode');
yield (0, exec_1.exec)('sudo', args, this.execOptions);
});
}
upload(channel, flags) {
return __awaiter(this, void 0, void 0, function* () {
// as we don't know the name of the name of the charm file output, we'll need to glob for it.
// however, we expect charmcraft pack to always output one charm file.
const globber = yield glob.create('./*.charm');
const paths = yield globber.glob();
return paths[0];
});
}
upload(charm, channel, flags) {
return __awaiter(this, void 0, void 0, function* () {
const args = [
'upload',
'--format',
'json',
'--release',
channel,
paths[0],
charm,
...flags,
];
const result = yield (0, exec_1.getExecOutput)('charmcraft', args, this.execOptions);
Expand Down
11 changes: 6 additions & 5 deletions dist/release-charm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22938,21 +22938,22 @@ class Charmcraft {
if (destructive)
args.push('--destructive-mode');
yield (0, exec_1.exec)('sudo', args, this.execOptions);
});
}
upload(channel, flags) {
return __awaiter(this, void 0, void 0, function* () {
// as we don't know the name of the name of the charm file output, we'll need to glob for it.
// however, we expect charmcraft pack to always output one charm file.
const globber = yield glob.create('./*.charm');
const paths = yield globber.glob();
return paths[0];
});
}
upload(charm, channel, flags) {
return __awaiter(this, void 0, void 0, function* () {
const args = [
'upload',
'--format',
'json',
'--release',
channel,
paths[0],
charm,
...flags,
];
const result = yield (0, exec_1.getExecOutput)('charmcraft', args, this.execOptions);
Expand Down
11 changes: 6 additions & 5 deletions dist/release-libraries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23069,21 +23069,22 @@ class Charmcraft {
if (destructive)
args.push('--destructive-mode');
yield (0, exec_1.exec)('sudo', args, this.execOptions);
});
}
upload(channel, flags) {
return __awaiter(this, void 0, void 0, function* () {
// as we don't know the name of the name of the charm file output, we'll need to glob for it.
// however, we expect charmcraft pack to always output one charm file.
const globber = yield glob.create('./*.charm');
const paths = yield globber.glob();
return paths[0];
});
}
upload(charm, channel, flags) {
return __awaiter(this, void 0, void 0, function* () {
const args = [
'upload',
'--format',
'json',
'--release',
channel,
paths[0],
charm,
...flags,
];
const result = yield (0, exec_1.getExecOutput)('charmcraft', args, this.execOptions);
Expand Down
11 changes: 6 additions & 5 deletions dist/upload-bundle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22917,21 +22917,22 @@ class Charmcraft {
if (destructive)
args.push('--destructive-mode');
yield (0, exec_1.exec)('sudo', args, this.execOptions);
});
}
upload(channel, flags) {
return __awaiter(this, void 0, void 0, function* () {
// as we don't know the name of the name of the charm file output, we'll need to glob for it.
// however, we expect charmcraft pack to always output one charm file.
const globber = yield glob.create('./*.charm');
const paths = yield globber.glob();
return paths[0];
});
}
upload(charm, channel, flags) {
return __awaiter(this, void 0, void 0, function* () {
const args = [
'upload',
'--format',
'json',
'--release',
channel,
paths[0],
charm,
...flags,
];
const result = yield (0, exec_1.getExecOutput)('charmcraft', args, this.execOptions);
Expand Down
18 changes: 11 additions & 7 deletions dist/upload-charm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22523,6 +22523,7 @@ class UploadCharmAction {
constructor() {
this.channel = core.getInput('channel');
this.charmcraftChannel = core.getInput('charmcraft-channel');
this.builtCharmPath = core.getInput('built-charm-path');
this.charmPath = core.getInput('charm-path');
this.tagPrefix = core.getInput('tag-prefix');
this.token = core.getInput('github-token');
Expand All @@ -22549,7 +22550,9 @@ class UploadCharmAction {
try {
yield this.snap.install('charmcraft', this.charmcraftChannel);
process.chdir(this.charmPath);
yield this.charmcraft.pack(this.destructive);
const charm = this.builtCharmPath
? this.builtCharmPath
: yield this.charmcraft.pack(this.destructive);
const overrides = this.overrides;
const imageResults = yield this.charmcraft.uploadResources(overrides);
const fileResults = yield this.charmcraft.fetchFileFlags(overrides);
Expand All @@ -22564,7 +22567,7 @@ class UploadCharmAction {
...fileResults.flags,
...staticResults.flags,
];
const rev = yield this.charmcraft.upload(this.channel, flags);
const rev = yield this.charmcraft.upload(charm, this.channel, flags);
yield this.tagger.tag(rev, this.channel, resourceInfo, this.tagPrefix);
}
catch (error) {
Expand Down Expand Up @@ -22945,21 +22948,22 @@ class Charmcraft {
if (destructive)
args.push('--destructive-mode');
yield (0, exec_1.exec)('sudo', args, this.execOptions);
});
}
upload(channel, flags) {
return __awaiter(this, void 0, void 0, function* () {
// as we don't know the name of the name of the charm file output, we'll need to glob for it.
// however, we expect charmcraft pack to always output one charm file.
const globber = yield glob.create('./*.charm');
const paths = yield globber.glob();
return paths[0];
});
}
upload(charm, channel, flags) {
return __awaiter(this, void 0, void 0, function* () {
const args = [
'upload',
'--format',
'json',
'--release',
channel,
paths[0],
charm,
...flags,
];
const result = yield (0, exec_1.getExecOutput)('charmcraft', args, this.execOptions);
Expand Down
10 changes: 8 additions & 2 deletions src/actions/upload-charm/upload-charm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ export class UploadCharmAction {
private channel: string;
private destructive: boolean;
private charmcraftChannel: string;
private builtCharmPath: string;
private charmPath: string;
private tagPrefix?: string;
private token: string;

constructor() {
this.channel = core.getInput('channel');
this.charmcraftChannel = core.getInput('charmcraft-channel');
this.builtCharmPath = core.getInput('built-charm-path');
this.charmPath = core.getInput('charm-path');
this.tagPrefix = core.getInput('tag-prefix');
this.token = core.getInput('github-token');
Expand Down Expand Up @@ -51,7 +53,11 @@ export class UploadCharmAction {
try {
await this.snap.install('charmcraft', this.charmcraftChannel);
process.chdir(this.charmPath!);
await this.charmcraft.pack(this.destructive);

const charm = this.builtCharmPath
? this.builtCharmPath
: await this.charmcraft.pack(this.destructive);

const overrides = this.overrides!;

const imageResults = await this.charmcraft.uploadResources(overrides);
Expand All @@ -70,7 +76,7 @@ export class UploadCharmAction {
...staticResults.flags,
];

const rev = await this.charmcraft.upload(this.channel, flags);
const rev = await this.charmcraft.upload(charm, this.channel, flags);

await this.tagger.tag(rev, this.channel, resourceInfo, this.tagPrefix);
} catch (error: any) {
Expand Down
6 changes: 4 additions & 2 deletions src/services/charmcraft/charmcraft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ describe('the charmcraft service', () => {
stdout: '{"revision": 2}',
});

await charmcraft.upload('edge', ['--resource=resource_1:2']);
await charmcraft.upload('banana.charm', 'edge', [
'--resource=resource_1:2',
]);

expect(mockExec).toHaveBeenCalled();
expect(mockExec).toHaveBeenCalledWith(
Expand All @@ -54,7 +56,7 @@ describe('the charmcraft service', () => {
'json',
'--release',
'edge',
undefined,
'banana.charm',
'--resource=resource_1:2',
],
expect.anything()
Expand Down
12 changes: 8 additions & 4 deletions src/services/charmcraft/charmcraft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,25 @@ class Charmcraft {
if (destructive) args.push('--destructive-mode');

await exec('sudo', args, this.execOptions);
}

async upload(channel: string, flags: string[]): Promise<string> {
// as we don't know the name of the name of the charm file output, we'll need to glob for it.
// however, we expect charmcraft pack to always output one charm file.
const globber = await glob.create('./*.charm');
const paths = await globber.glob();
return paths[0];
}

async upload(
charm: string,
channel: string,
flags: string[]
): Promise<string> {
const args = [
'upload',
'--format',
'json',
'--release',
channel,
paths[0],
charm,
...flags,
];
const result = await getExecOutput('charmcraft', args, this.execOptions);
Expand Down
21 changes: 11 additions & 10 deletions upload-charm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ If you want to use a new resource, you'll have to cut a new resource revision **

### Inputs

| Key | Description | Required |
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------- |
| `charm-path` | Path to the charm we want to publish. Defaults to the current working directory. | |
| `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 | ✔️ |
| `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`. | |
| `charmcraft-channel` | Snap channel to use when installing charmcraft. Defaults to `latest/edge`. | |
| Key | Description | Required |
|----------------------|------------------------------------------------------------------------------------------------------------------| -------- |
| `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 | ✔️ |
| `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`. | |
| `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
4 changes: 4 additions & 0 deletions upload-charm/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ inputs:
default: '.'
description: |
Path to charm directory
built-charm-path:
required: false
description: |
Path to a pre-built charm.
charmcraft-channel:
required: false
default: 'latest/stable'
Expand Down

0 comments on commit a4a19ea

Please sign in to comment.