Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
run-rockcraft-pack-action:
strategy:
matrix:
os: [ubuntu-24.04, ubuntu-22.04, ubuntu-20.04]
os: [ubuntu-24.04, ubuntu-22.04]
revision: ['1206', '']
runs-on: ${{ matrix.os }}
steps:
Expand Down
21 changes: 13 additions & 8 deletions dist/rockcraft-pack-action/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20093,10 +20093,12 @@ var RockcraftBuilder = class {
rockcraftChannel;
rockcraftPackVerbosity;
rockcraftRevision;
envFile;
constructor(options) {
this.projectRoot = expandHome(options.projectRoot);
this.rockcraftChannel = options.rockcraftChannel;
this.rockcraftRevision = options.rockcraftRevision;
this.envFile = options.envFile;
if (allowedVerbosity.includes(options.rockcraftPackVerbosity)) {
this.rockcraftPackVerbosity = options.rockcraftPackVerbosity;
} else {
Expand All @@ -20117,13 +20119,14 @@ var RockcraftBuilder = class {
rockcraftPackArgs = `${rockcraftPackArgs} --verbosity ${this.rockcraftPackVerbosity}`;
}
rockcraft = `${rockcraft} ${rockcraftPackArgs.trim()}`;
await exec3.exec(
"sudo",
["--preserve-env", "--user", shellUser(), ...rockcraft.split(" ")],
{
cwd: this.projectRoot
}
);
let sourceCmd = "";
if (this.envFile) {
sourceCmd = `source ${this.envFile} && `;
}
const bashCommand = `${sourceCmd}sudo --preserve-env --user ${shellUser()} ${rockcraft}`;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to the PR, but this is an interesting example of where switching to sudo-rs could cause issues.

await exec3.exec("bash", ["-c", bashCommand], {
cwd: this.projectRoot
});
}
// This wrapper is for the benefit of the tests, due to the crazy
// typing of fs.promises.readdir()
Expand All @@ -20150,6 +20153,7 @@ async function run() {
core3.info(`Building rock in "${projectRoot}"...`);
const rockcraftRevision = core3.getInput("revision");
const rockcraftChannel = core3.getInput("rockcraft-channel") || "stable";
const envFile = core3.getInput("env-file") || "";
if (rockcraftRevision.length < 1) {
core3.warning(
`Rockcraft revision not provided. Installing from ${rockcraftChannel}`
Expand All @@ -20160,7 +20164,8 @@ async function run() {
projectRoot,
rockcraftChannel,
rockcraftPackVerbosity,
rockcraftRevision
rockcraftRevision,
envFile
});
await builder.pack();
const rock = await builder.outputRock();
Expand Down
5 changes: 5 additions & 0 deletions rockcraft-pack/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ inputs:

If not provided, it defaults to whatever revision is in latest/stable.
default: ''
env-file:
description: >
The path to the file containing the environment for execution.
Comment thread
cjdcordeiro marked this conversation as resolved.

default: ''
outputs:
rock:
description: 'The file name of the resulting rock.'
Expand Down
4 changes: 3 additions & 1 deletion src/rockcraft-pack-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ async function run(): Promise<void> {
core.info(`Building rock in "${projectRoot}"...`)
const rockcraftRevision = core.getInput('revision')
const rockcraftChannel = core.getInput('rockcraft-channel') || 'stable'
const envFile = core.getInput('env-file') || ''
if (rockcraftRevision.length < 1) {
core.warning(
`Rockcraft revision not provided. Installing from ${rockcraftChannel}`
Expand All @@ -20,7 +21,8 @@ async function run(): Promise<void> {
projectRoot,
rockcraftChannel,
rockcraftPackVerbosity,
rockcraftRevision
rockcraftRevision,
envFile
})
await builder.pack()
const rock = await builder.outputRock()
Expand Down
21 changes: 14 additions & 7 deletions src/rockcraft-pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ interface RockcraftBuilderOptions {
rockcraftChannel: string
rockcraftPackVerbosity: string
rockcraftRevision: string
envFile: string
}

export class RockcraftBuilder {
projectRoot: string
rockcraftChannel: string
rockcraftPackVerbosity: string
rockcraftRevision: string
envFile: string

constructor(options: RockcraftBuilderOptions) {
this.projectRoot = tools.expandHome(options.projectRoot)
this.rockcraftChannel = options.rockcraftChannel
this.rockcraftRevision = options.rockcraftRevision
this.envFile = options.envFile
if (allowedVerbosity.includes(options.rockcraftPackVerbosity)) {
this.rockcraftPackVerbosity = options.rockcraftPackVerbosity
} else {
Expand All @@ -49,13 +52,17 @@ export class RockcraftBuilder {
}

rockcraft = `${rockcraft} ${rockcraftPackArgs.trim()}`
await exec.exec(
'sudo',
['--preserve-env', '--user', tools.shellUser(), ...rockcraft.split(' ')],
{
cwd: this.projectRoot
}
)

// Source environment and run the command
let sourceCmd = ''
if (this.envFile) {
sourceCmd = `source "${this.envFile}" && `
}
const bashCommand = `${sourceCmd}sudo --preserve-env --user ${tools.shellUser()} ${rockcraft}`

await exec.exec('bash', ['-c', bashCommand], {
cwd: this.projectRoot
})
}

// This wrapper is for the benefit of the tests, due to the crazy
Expand Down
51 changes: 26 additions & 25 deletions tests/rockcraft-pack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ test('RockcraftBuilder expands tilde in project root', () => {
projectRoot: '~',
rockcraftChannel: 'edge',
rockcraftPackVerbosity: 'trace',
rockcraftRevision: '1'
rockcraftRevision: '1',
envFile: ''
})
expect(builder.projectRoot).toBe(os.homedir())

builder = new build.RockcraftBuilder({
projectRoot: '~/foo/bar',
rockcraftChannel: 'stable',
rockcraftPackVerbosity: 'trace',
rockcraftRevision: '1'
rockcraftRevision: '1',
envFile: ''
})
expect(builder.projectRoot).toBe(path.join(os.homedir(), 'foo/bar'))
})
Expand Down Expand Up @@ -59,7 +61,8 @@ test('RockcraftBuilder.pack runs a rock build', async () => {
projectRoot: projectDir,
rockcraftChannel: 'stable',
rockcraftPackVerbosity: 'debug',
rockcraftRevision: '1'
rockcraftRevision: '1',
envFile: 'my-env-file'
})
await builder.pack()

Expand All @@ -68,15 +71,12 @@ test('RockcraftBuilder.pack runs a rock build', async () => {
expect(ensureRockcraft).toHaveBeenCalled()
expect(shellUser).toHaveBeenCalled()
expect(execMock).toHaveBeenCalledWith(
'sudo',
'bash',
[
'--preserve-env',
'--user',
user,
'rockcraft',
'pack',
'--verbosity',
'debug'
'-c',
'source "my-env-file" && sudo --preserve-env --user ' +
user +
' rockcraft pack --verbosity debug'
],
{
cwd: projectDir
Expand Down Expand Up @@ -108,7 +108,8 @@ test('RockcraftBuilder.build can set the Rockcraft channel', async () => {
projectRoot: '.',
rockcraftChannel: 'test-channel',
rockcraftPackVerbosity: 'trace',
rockcraftRevision: ''
rockcraftRevision: '',
envFile: ''
})
await builder.pack()

Expand Down Expand Up @@ -139,7 +140,8 @@ test('RockcraftBuilder.build can set the Rockcraft revision', async () => {
projectRoot: '.',
rockcraftChannel: 'channel',
rockcraftPackVerbosity: 'trace',
rockcraftRevision: '123'
rockcraftRevision: '123',
envFile: ''
})
await builder.pack()

Expand Down Expand Up @@ -175,20 +177,16 @@ test('RockcraftBuilder.build can pass known verbosity', async () => {
projectRoot: '.',
rockcraftChannel: 'stable',
rockcraftPackVerbosity: 'trace',
rockcraftRevision: '1'
rockcraftRevision: '1',
envFile: ''
})
await builder.pack()

expect(execMock).toHaveBeenCalledWith(
'sudo',
'bash',
[
'--preserve-env',
'--user',
user,
'rockcraft',
'pack',
'--verbosity',
'trace'
'-c',
'sudo --preserve-env --user ' + user + ' rockcraft pack --verbosity trace'
],
expect.anything()
)
Expand All @@ -198,7 +196,8 @@ test('RockcraftBuilder.build can pass known verbosity', async () => {
projectRoot: '.',
rockcraftChannel: 'stable',
rockcraftPackVerbosity: 'fake-verbosity',
rockcraftRevision: '1'
rockcraftRevision: '1',
envFile: ''
})
}
expect(badBuilder).toThrow()
Expand All @@ -212,7 +211,8 @@ test('RockcraftBuilder.outputRock fails if there are no rocks', async () => {
projectRoot: projectDir,
rockcraftChannel: 'stable',
rockcraftPackVerbosity: 'trace',
rockcraftRevision: '1'
rockcraftRevision: '1',
envFile: ''
})

const readdir = jest
Expand All @@ -233,7 +233,8 @@ test('RockcraftBuilder.outputRock returns the first rock', async () => {
projectRoot: projectDir,
rockcraftChannel: 'stable',
rockcraftPackVerbosity: 'trace',
rockcraftRevision: '1'
rockcraftRevision: '1',
envFile: ''
})

const readdir = jest
Expand Down