Skip to content

Commit cfd7a24

Browse files
authored
Merge pull request #21 from diddlesnaps/updates-for-snapname-and-podman-arch
Update for snap-name output and podman architecture support
2 parents b718db6 + 586b0b1 commit cfd7a24

File tree

4 files changed

+96
-51
lines changed

4 files changed

+96
-51
lines changed

.github/workflows/test.yml

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
npm install
2020
npm run all
2121
22-
integration: # make sure the action works on a clean machine without building
22+
docker-integration: # make sure the action works on a clean machine without building
2323
strategy:
2424
matrix:
2525
base:
@@ -32,60 +32,26 @@ jobs:
3232
- armhf
3333
- arm64
3434
- ppc64el
35-
usePodman:
36-
- ''
37-
- 'true'
38-
- 'false'
3935
runner:
4036
- ubuntu-latest
4137
include:
4238
- base: core
4339
arch: ''
44-
usePodman: 'false'
45-
runner: ubuntu-20.04
46-
- base: core
47-
arch: ''
48-
usePodman: 'true'
49-
runner: ubuntu-20.04
50-
- base: core
51-
arch: i386
52-
usePodman: 'false'
5340
runner: ubuntu-20.04
5441
- base: core
5542
arch: i386
56-
usePodman: 'true'
57-
runner: ubuntu-20.04
58-
- base: core
59-
arch: amd64
60-
usePodman: 'false'
6143
runner: ubuntu-20.04
6244
- base: core
6345
arch: amd64
64-
usePodman: 'true'
6546
runner: ubuntu-20.04
6647
- base: core
6748
arch: armhf
68-
usePodman: 'false'
69-
runner: ubuntu-20.04
70-
- base: core
71-
arch: armhf
72-
usePodman: 'true'
73-
runner: ubuntu-20.04
74-
- base: core
75-
arch: arm64
76-
usePodman: 'false'
7749
runner: ubuntu-20.04
7850
- base: core
7951
arch: arm64
80-
usePodman: 'true'
8152
runner: ubuntu-20.04
8253
- base: core18
8354
arch: i386
84-
usePodman: 'false'
85-
runner: ubuntu-latest
86-
- base: core18
87-
arch: i386
88-
usePodman: 'true'
8955
runner: ubuntu-latest
9056
runs-on: ${{ matrix.runner }}
9157
steps:
@@ -100,4 +66,50 @@ jobs:
10066
with:
10167
path: './test-projects/${{ matrix.base }}'
10268
architecture: ${{ matrix.arch }}
103-
use-podman: ${{ matrix.usePodman }}
69+
70+
podman-integration: # make sure the action works on a clean machine without building
71+
strategy:
72+
matrix:
73+
base:
74+
- core20
75+
- core22
76+
arch:
77+
- name: ''
78+
runner: ubuntu-latest
79+
- name: ''
80+
runner: [self-hosted, linux, ARM64]
81+
- name: amd64
82+
runner: ubuntu-latest
83+
- name: arm64
84+
runner: [self-hosted, linux, ARM64]
85+
include:
86+
- base: core
87+
arch:
88+
name: ''
89+
runner: ubuntu-20.04
90+
- base: core
91+
arch:
92+
name: i386
93+
runner: ubuntu-20.04
94+
- base: core
95+
arch:
96+
name: amd64
97+
runner: ubuntu-20.04
98+
- base: core18
99+
arch:
100+
name: i386
101+
runner: ubuntu-latest
102+
- base: core18
103+
arch:
104+
name: amd64
105+
runner: ubuntu-latest
106+
runs-on: ${{ matrix.arch.runner }}
107+
steps:
108+
- uses: docker/setup-qemu-action@v2
109+
- uses: actions/checkout@v3
110+
- uses: ./
111+
id: snapcraft
112+
with:
113+
path: './test-projects/${{ matrix.base }}'
114+
architecture: ${{ matrix.arch.name }}
115+
use-podman: 'true'

__tests__/build.test.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ for (const base of ['core', 'core18', 'core20', 'core22']) {
6767
}
6868
for (const [base, arch, channel] of matrix) {
6969
test(`SnapcraftBuilder.build runs a snap build using Docker with base: ${base}; and arch: ${arch}`, async () => {
70-
expect.assertions(4)
70+
expect.assertions(5)
7171

7272
const ensureDockerExperimentalMock = jest
7373
.spyOn(tools, 'ensureDockerExperimental')
@@ -113,7 +113,14 @@ for (const [base, arch, channel] of matrix) {
113113
} else {
114114
expect(detectCGroupsV1Mock).not.toHaveBeenCalled()
115115
}
116-
expect(execMock).toHaveBeenCalledWith(
116+
expect(execMock).toHaveBeenNthCalledWith(
117+
1,
118+
'docker',
119+
['pull', ...platform, `diddledani/snapcraft:${base}`],
120+
expect.anything()
121+
)
122+
expect(execMock).toHaveBeenNthCalledWith(
123+
2,
117124
'docker',
118125
[
119126
'run',
@@ -141,7 +148,7 @@ for (const [base, arch, channel] of matrix) {
141148
})
142149

143150
test(`SnapcraftBuilder.build runs a snap build using Podman with base: ${base}; and arch: ${arch}`, async () => {
144-
expect.assertions(4)
151+
expect.assertions(5)
145152
const ensureDockerExperimentalMock = jest
146153
.spyOn(tools, 'ensureDockerExperimental')
147154
.mockImplementation(async (): Promise<void> => Promise.resolve())
@@ -174,14 +181,26 @@ for (const [base, arch, channel] of matrix) {
174181
)
175182
await builder.build()
176183

184+
let platform: string[] = []
185+
if (arch && arch in build.platforms) {
186+
platform = ['--platform', build.platforms[arch]]
187+
}
188+
177189
expect(ensureDockerExperimentalMock).not.toHaveBeenCalled()
178190
expect(detectBaseMock).toHaveBeenCalled()
179191
if (base === 'core') {
180192
expect(detectCGroupsV1Mock).toHaveBeenCalled()
181193
} else {
182194
expect(detectCGroupsV1Mock).not.toHaveBeenCalled()
183195
}
184-
expect(execMock).toHaveBeenCalledWith(
196+
expect(execMock).toHaveBeenNthCalledWith(
197+
1,
198+
'sudo podman',
199+
['pull', ...platform, `docker.io/diddledani/snapcraft:${base}`],
200+
expect.anything()
201+
)
202+
expect(execMock).toHaveBeenNthCalledWith(
203+
2,
185204
'sudo podman',
186205
[
187206
'run',
@@ -192,6 +211,7 @@ for (const [base, arch, channel] of matrix) {
192211
`${process.cwd()}/${projectDir}:/data`,
193212
'--workdir',
194213
'/data',
214+
...platform,
195215
'--env',
196216
`SNAPCRAFT_IMAGE_INFO={"build_url":"https://github.com/user/repo/actions/runs/42"}`,
197217
'--env',
@@ -239,7 +259,7 @@ test('SnapcraftBuilder.build can disable build info', async () => {
239259
)
240260
await builder.build()
241261

242-
expect(execMock).toHaveBeenCalledWith(
262+
expect(execMock).toHaveBeenLastCalledWith(
243263
'docker',
244264
[
245265
'run',
@@ -292,7 +312,7 @@ test('SnapcraftBuilder.build can pass additional arguments', async () => {
292312
)
293313
await builder.build()
294314

295-
expect(execMock).toHaveBeenCalledWith(
315+
expect(execMock).toHaveBeenLastCalledWith(
296316
'docker',
297317
[
298318
'run',
@@ -345,7 +365,7 @@ test('SnapcraftBuilder.build can pass extra environment variables', async () =>
345365
)
346366
await builder.build()
347367

348-
expect(execMock).toHaveBeenCalledWith(
368+
expect(execMock).toHaveBeenLastCalledWith(
349369
'docker',
350370
[
351371
'run',
@@ -402,7 +422,7 @@ test('SnapcraftBuilder.build adds store credentials', async () => {
402422
)
403423
await builder.build()
404424

405-
expect(execMock).toHaveBeenCalledWith(
425+
expect(execMock).toHaveBeenLastCalledWith(
406426
'docker',
407427
[
408428
'run',
@@ -486,8 +506,6 @@ test('SnapcraftBuilder.outputSnap returns the first snap', async () => {
486506
.spyOn(core, 'warning')
487507
.mockImplementation((_message: string | Error): void => {})
488508

489-
await expect(builder.outputSnap()).resolves.toEqual(
490-
path.join(process.cwd(), 'project-root/one.snap')
491-
)
509+
await expect(builder.outputSnap()).resolves.toEqual('./project-root/one.snap')
492510
expect(readdir).toHaveBeenCalled()
493511
})

dist/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8160,8 +8160,10 @@ class SnapcraftBuilder {
81608160
env['SNAPCRAFT_STORE_CREDENTIALS'] = this.storeAuth;
81618161
}
81628162
let dockerArgs = [];
8163-
if (this.architecture in platforms && !this.usePodman) {
8163+
let pullArgs = [];
8164+
if (this.architecture in platforms) {
81648165
dockerArgs = dockerArgs.concat('--platform', platforms[this.architecture]);
8166+
pullArgs = pullArgs.concat('--platform', platforms[this.architecture]);
81658167
}
81668168
for (const key in env) {
81678169
dockerArgs = dockerArgs.concat('--env', `${key}=${env[key]}`);
@@ -8173,6 +8175,9 @@ class SnapcraftBuilder {
81738175
containerImage = `docker.io/${containerImage}`;
81748176
dockerArgs = dockerArgs.concat('--systemd', 'always');
81758177
}
8178+
await exec.exec(command, ['pull', ...pullArgs, containerImage], {
8179+
cwd: this.projectRoot
8180+
});
81768181
await exec.exec(command, [
81778182
'run',
81788183
'--rm',
@@ -8196,6 +8201,7 @@ class SnapcraftBuilder {
81968201
return await external_fs_.promises.readdir(dir);
81978202
}
81988203
async outputSnap() {
8204+
const workspace = external_process_namespaceObject.env.GITHUB_WORKSPACE ?? external_process_namespaceObject.cwd();
81998205
const files = await this._readdir(this.projectRoot);
82008206
const snaps = files.filter(name => name.endsWith('.snap'));
82018207
if (snaps.length === 0) {
@@ -8204,7 +8210,7 @@ class SnapcraftBuilder {
82048210
if (snaps.length > 1) {
82058211
core.warning(`Multiple snaps found in ${this.projectRoot}`);
82068212
}
8207-
const snap = external_path_.join(this.projectRoot, snaps[0]);
8213+
const snap = external_path_.join(this.projectRoot, snaps[0]).replace(workspace, '.');
82088214
await exec.exec('sudo', ['chown', external_process_namespaceObject.getuid().toString(), snap]);
82098215
return snap;
82108216
}

src/build.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ export class SnapcraftBuilder {
107107
}
108108

109109
let dockerArgs: string[] = []
110-
if (this.architecture in platforms && !this.usePodman) {
110+
let pullArgs: string[] = []
111+
if (this.architecture in platforms) {
111112
dockerArgs = dockerArgs.concat('--platform', platforms[this.architecture])
113+
pullArgs = pullArgs.concat('--platform', platforms[this.architecture])
112114
}
113115

114116
for (const key in env) {
@@ -122,6 +124,11 @@ export class SnapcraftBuilder {
122124
containerImage = `docker.io/${containerImage}`
123125
dockerArgs = dockerArgs.concat('--systemd', 'always')
124126
}
127+
128+
await exec.exec(command, ['pull', ...pullArgs, containerImage], {
129+
cwd: this.projectRoot
130+
})
131+
125132
await exec.exec(
126133
command,
127134
[
@@ -151,6 +158,8 @@ export class SnapcraftBuilder {
151158
}
152159

153160
async outputSnap(): Promise<string> {
161+
const workspace = process.env['GITHUB_WORKSPACE'] ?? process.cwd()
162+
154163
const files = await this._readdir(this.projectRoot)
155164
const snaps = files.filter(name => name.endsWith('.snap'))
156165

@@ -160,7 +169,7 @@ export class SnapcraftBuilder {
160169
if (snaps.length > 1) {
161170
core.warning(`Multiple snaps found in ${this.projectRoot}`)
162171
}
163-
const snap = path.join(this.projectRoot, snaps[0])
172+
const snap = path.join(this.projectRoot, snaps[0]).replace(workspace, '.')
164173
await exec.exec('sudo', ['chown', process.getuid().toString(), snap])
165174
return snap
166175
}

0 commit comments

Comments
 (0)