Skip to content

Commit

Permalink
Merge pull request #14 from cjp256/ua-token
Browse files Browse the repository at this point in the history
src: add support for --ua-token
  • Loading branch information
jhenstridge authored Apr 27, 2021
2 parents 9f97bba + 5c810b8 commit aaae009
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 12 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,29 @@ The `snapcraft-args` parameter can be used to pass additional
arguments to Snapcraft. This is primarily intended to allow the use
of experimental features by passing `--enable-experimental-*`
arguments to Snapcraft.

### `ua-token`

The `ua-token` parameter can be used to tell Snapcraft to attach an Ubuntu
Advantage (UA) token inside the build environment. Snapcraft will ensure
the token is detached before exiting, but be warned that it is possible
some failures may prevent detaching (e.g. aborted jobs).

In order to make the UA token available to the workflow, it should be stored
as a repository secret:

1. choose the "Settings" tab.
2. choose "Secrets" from the menu on the left.
3. click "Add a new secret".
4. set the name to `UA_TOKEN` (or whatever is referenced in the workflow),
and paste the UA token as the value.

An example workflow with UA token stored as secret `UA_TOKEN`:

```yaml
...
- uses: snapcore/action-build@v1
with:
path: path-to-snapcraft-project
ua-token: ${{ secrets.UA_TOKEN }}
```
51 changes: 43 additions & 8 deletions __tests__/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ afterEach(() => {
})

test('SnapcraftBuilder expands tilde in project root', () => {
let builder = new build.SnapcraftBuilder('~', true, 'stable', '')
let builder = new build.SnapcraftBuilder('~', true, 'stable', '', '')
expect(builder.projectRoot).toBe(os.homedir())

builder = new build.SnapcraftBuilder('~/foo/bar', true, 'stable', '')
builder = new build.SnapcraftBuilder('~/foo/bar', true, 'stable', '', '')
expect(builder.projectRoot).toBe(path.join(os.homedir(), 'foo/bar'))
})

Expand All @@ -41,7 +41,7 @@ test('SnapcraftBuilder.build runs a snap build', async () => {
process.env['GITHUB_RUN_ID'] = '42'

const projectDir = 'project-root'
const builder = new build.SnapcraftBuilder(projectDir, true, 'stable', '')
const builder = new build.SnapcraftBuilder(projectDir, true, 'stable', '', '')
await builder.build()

expect(ensureSnapd).toHaveBeenCalled()
Expand Down Expand Up @@ -76,7 +76,7 @@ test('SnapcraftBuilder.build can disable build info', async () => {
}
)

const builder = new build.SnapcraftBuilder('.', false, 'stable', '')
const builder = new build.SnapcraftBuilder('.', false, 'stable', '', '')
await builder.build()

expect(execMock).toHaveBeenCalledWith('sg', expect.any(Array), {
Expand Down Expand Up @@ -106,7 +106,7 @@ test('SnapcraftBuilder.build can set the Snapcraft channel', async () => {
}
)

const builder = new build.SnapcraftBuilder('.', false, 'edge', '')
const builder = new build.SnapcraftBuilder('.', false, 'edge', '', '')
await builder.build()

expect(ensureSnapcraft).toHaveBeenCalledWith('edge')
Expand Down Expand Up @@ -134,7 +134,8 @@ test('SnapcraftBuilder.build can pass additional arguments', async () => {
'.',
false,
'stable',
'--foo --bar'
'--foo --bar',
''
)
await builder.build()

Expand All @@ -145,11 +146,45 @@ test('SnapcraftBuilder.build can pass additional arguments', async () => {
)
})

test('SnapcraftBuilder.build can pass UA token', async () => {
expect.assertions(1)

const ensureSnapd = jest
.spyOn(tools, 'ensureSnapd')
.mockImplementation(async (): Promise<void> => {})
const ensureLXD = jest
.spyOn(tools, 'ensureLXD')
.mockImplementation(async (): Promise<void> => {})
const ensureSnapcraft = jest
.spyOn(tools, 'ensureSnapcraft')
.mockImplementation(async (channel): Promise<void> => {})
const execMock = jest.spyOn(exec, 'exec').mockImplementation(
async (program: string, args?: string[]): Promise<number> => {
return 0
}
)

const builder = new build.SnapcraftBuilder(
'.',
false,
'stable',
'',
'test-ua-token'
)
await builder.build()

expect(execMock).toHaveBeenCalledWith(
'sg',
['lxd', '-c', 'snapcraft --ua-token test-ua-token'],
expect.anything()
)
})

test('SnapcraftBuilder.outputSnap fails if there are no snaps', async () => {
expect.assertions(2)

const projectDir = 'project-root'
const builder = new build.SnapcraftBuilder(projectDir, true, 'stable', '')
const builder = new build.SnapcraftBuilder(projectDir, true, 'stable', '', '')

const readdir = jest
.spyOn(builder, '_readdir')
Expand All @@ -167,7 +202,7 @@ test('SnapcraftBuilder.outputSnap returns the first snap', async () => {
expect.assertions(2)

const projectDir = 'project-root'
const builder = new build.SnapcraftBuilder(projectDir, true, 'stable', '')
const builder = new build.SnapcraftBuilder(projectDir, true, 'stable', '', '')

const readdir = jest
.spyOn(builder, '_readdir')
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ inputs:
must be turned on via a `--enable-experimental-*` command line
argument. This parameter can be used to turn on such features.
default: ''
ua-token:
description: >
UA token to attach in build environment.
Snapcraft will detach the token when no longer required.
default: ''
outputs:
snap:
description: 'The file name of the resulting snap.'
Expand Down
9 changes: 7 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1499,11 +1499,12 @@ function expandHome(p) {
return p;
}
class build_SnapcraftBuilder {
constructor(projectRoot, includeBuildInfo, snapcraftChannel, snapcraftArgs) {
constructor(projectRoot, includeBuildInfo, snapcraftChannel, snapcraftArgs, uaToken) {
this.projectRoot = expandHome(projectRoot);
this.includeBuildInfo = includeBuildInfo;
this.snapcraftChannel = snapcraftChannel;
this.snapcraftArgs = snapcraftArgs;
this.uaToken = uaToken;
}
build() {
return build_awaiter(this, void 0, void 0, function* () {
Expand All @@ -1528,6 +1529,9 @@ class build_SnapcraftBuilder {
if (this.snapcraftArgs) {
snapcraft = `${snapcraft} ${this.snapcraftArgs}`;
}
if (this.uaToken) {
snapcraft = `${snapcraft} --ua-token ${this.uaToken}`;
}
yield Object(exec.exec)('sg', ['lxd', '-c', snapcraft], {
cwd: this.projectRoot,
env
Expand Down Expand Up @@ -1577,7 +1581,8 @@ function run() {
Object(core.info)(`Building Snapcraft project in "${path}"...`);
const snapcraftChannel = Object(core.getInput)('snapcraft-channel');
const snapcraftArgs = Object(core.getInput)('snapcraft-args');
const builder = new build_SnapcraftBuilder(path, buildInfo, snapcraftChannel, snapcraftArgs);
const uaToken = Object(core.getInput)('ua-token');
const builder = new build_SnapcraftBuilder(path, buildInfo, snapcraftChannel, snapcraftArgs, uaToken);
yield builder.build();
const snap = yield builder.outputSnap();
Object(core.setOutput)('snap', snap);
Expand Down
8 changes: 7 additions & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@ export class SnapcraftBuilder {
includeBuildInfo: boolean
snapcraftChannel: string
snapcraftArgs: string
uaToken: string

constructor(
projectRoot: string,
includeBuildInfo: boolean,
snapcraftChannel: string,
snapcraftArgs: string
snapcraftArgs: string,
uaToken: string
) {
this.projectRoot = expandHome(projectRoot)
this.includeBuildInfo = includeBuildInfo
this.snapcraftChannel = snapcraftChannel
this.snapcraftArgs = snapcraftArgs
this.uaToken = uaToken
}

async build(): Promise<void> {
Expand All @@ -64,6 +67,9 @@ export class SnapcraftBuilder {
if (this.snapcraftArgs) {
snapcraft = `${snapcraft} ${this.snapcraftArgs}`
}
if (this.uaToken) {
snapcraft = `${snapcraft} --ua-token ${this.uaToken}`
}

await exec.exec('sg', ['lxd', '-c', snapcraft], {
cwd: this.projectRoot,
Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ async function run(): Promise<void> {
core.info(`Building Snapcraft project in "${path}"...`)
const snapcraftChannel = core.getInput('snapcraft-channel')
const snapcraftArgs = core.getInput('snapcraft-args')
const uaToken = core.getInput('ua-token')

const builder = new SnapcraftBuilder(
path,
buildInfo,
snapcraftChannel,
snapcraftArgs
snapcraftArgs,
uaToken
)
await builder.build()
const snap = await builder.outputSnap()
Expand Down

0 comments on commit aaae009

Please sign in to comment.