Skip to content

Commit 3b814dd

Browse files
davidmhewittbilelmoussaoui
authored andcommitted
flatpak-builder: Allow specifying CPU arch
1 parent ee69fa9 commit 3b814dd

File tree

4 files changed

+16895
-20577
lines changed

4 files changed

+16895
-20577
lines changed

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Build and deploy your Flatpak application using Github Actions
88
<img src="https://user-images.githubusercontent.com/15098724/55282117-f8253380-52fa-11e9-95a3-ccae83b23034.png" alt="Flatpak logo" />
99
</p>
1010

11-
## How to use
11+
## How to use
1212

1313
### Building stage
1414

@@ -48,6 +48,50 @@ jobs:
4848
| `branch` | The default flatpak branch | Optional | `master` |
4949
| `cache` | Enable/Disable caching `.flatpak-builder` directory | Optional | `true` |
5050
| `cache-key` | Specifies the cache key | Optional | `flatpak-builder-${sha256(manifestPath)}` |
51+
| `arch` | Specifies the CPU architecture to build for | Optional | `x86_64` |
52+
53+
#### Building for multiple CPU architectures
54+
55+
To build for CPU architectures other than `x86_64`, the GitHub Actions workflow has to either natively be running on that architecture (e.g. on an `aarch64` self-hosted GitHub Actions runner), or the container used must be configured to emulate the requested architecture (e.g. with QEMU).
56+
57+
For example, to built a Flatpak for both `x86_64` and `aarch64` using emulation, use the following workflow as a guide:
58+
59+
```yaml
60+
on:
61+
push:
62+
branches: [main]
63+
pull_request:
64+
name: CI
65+
jobs:
66+
flatpak:
67+
name: "Flatpak"
68+
runs-on: ubuntu-latest
69+
container:
70+
image: bilelmoussaoui/flatpak-github-actions:gnome-40
71+
options: --privileged
72+
strategy:
73+
matrix:
74+
arch: [x86_64, aarch64]
75+
# Don't fail the whole workflow if one architecture fails
76+
fail-fast: false
77+
steps:
78+
- uses: actions/checkout@v2
79+
# Docker is required by the docker/setup-qemu-action which enables emulation
80+
- name: Install deps
81+
run: |
82+
dnf -y install docker
83+
- name: Set up QEMU
84+
id: qemu
85+
uses: docker/setup-qemu-action@v1
86+
with:
87+
platforms: arm64
88+
- uses: bilelmoussaoui/flatpak-github-actions/flatpak-builder@v4
89+
with:
90+
bundle: palette.flatpak
91+
manifest-path: org.gnome.zbrown.Palette.yml
92+
cache-key: flatpak-builder-${{ github.sha }}
93+
arch: ${{ matrix.arch }}
94+
```
5195

5296
### Deployment stage
5397

flatpak-builder/action.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ inputs:
3232
required: false
3333
cache:
3434
description: >
35-
Toggles caching the flatpak-builder directory.
35+
Toggles caching the flatpak-builder directory.
3636
Possible values: true, enabled, yes, y. Or something else to disable it.
3737
default: "true"
3838
required: false
@@ -45,6 +45,11 @@ inputs:
4545
description: The flatpak branch.
4646
default: "master"
4747
required: false
48+
arch:
49+
description: >
50+
The CPU architecture to build for.
51+
default: "x86_64"
52+
required: false
4853
runs:
4954
using: "node12"
5055
main: "dist/index.js"

flatpak-builder/dist/index.js

Lines changed: 16825 additions & 20566 deletions
Large diffs are not rendered by default.

flatpak-builder/index.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ const modifyManifest = (manifest, runTests = false) => {
125125
* @param {string} localRepoName The flatpak repository name
126126
* @param {boolean} cacheBuildDir Whether to enable caching the build directory
127127
* @param {string} cacheKey The key used to cache the build directory
128+
* @param {string} arch The CPU architecture to build for
128129
*/
129-
const build = async (manifest, manifestPath, bundle, repositoryUrl, repositoryName, buildDir, localRepoName, cacheBuildDir, cacheKey) => {
130+
const build = async (manifest, manifestPath, bundle, repositoryUrl, repositoryName, buildDir, localRepoName, cacheBuildDir, cacheKey, arch) => {
130131
const appId = manifest['app-id'] || manifest.id
131132
const branch = manifest.branch || core.getInput('branch') || 'master'
132133

@@ -137,7 +138,8 @@ const build = async (manifest, manifestPath, bundle, repositoryUrl, repositoryNa
137138
'--disable-rofiles-fuse',
138139
`--install-deps-from=${repositoryName}`,
139140
'--force-clean',
140-
`--default-branch=${branch}`
141+
`--default-branch=${branch}`,
142+
`--arch=${arch}`
141143
]
142144
if (cacheBuildDir) {
143145
args.push('--ccache')
@@ -160,6 +162,7 @@ const build = async (manifest, manifestPath, bundle, repositoryUrl, repositoryNa
160162
localRepoName,
161163
bundle,
162164
`--runtime-repo=${repositoryUrl}`,
165+
`--arch=${arch}`,
163166
appId,
164167
branch
165168
])
@@ -175,9 +178,10 @@ const build = async (manifest, manifestPath, bundle, repositoryUrl, repositoryNa
175178
* @param {PathLike} manifestPath the manifest path
176179
* @param {Boolean} cacheBuildDir whether to cache the build dir or not
177180
* @param {string | undefined} cacheKey the default cache key if there are any
181+
* @param {string} arch The CPU architecture to build for
178182
* @returns {Promise<String>} the new cacheKey if none was set before
179183
*/
180-
const prepareBuild = async (repositoryName, repositoryUrl, manifestPath, cacheBuildDir, cacheKey = undefined) => {
184+
const prepareBuild = async (repositoryName, repositoryUrl, manifestPath, cacheBuildDir, cacheKey = undefined, arch) => {
181185
/// If the user has set a different runtime source
182186
if (repositoryUrl !== 'https://flathub.org/repo/flathub.flatpakrepo') {
183187
await exec.exec('flatpak', ['remote-add', '--if-not-exists', repositoryName, repositoryUrl])
@@ -204,7 +208,8 @@ const prepareBuild = async (repositoryName, repositoryUrl, manifestPath, cacheBu
204208
core.info('No cache was found')
205209
}
206210
}
207-
return cacheKey
211+
// Ensure the cache key is unique if we're building multiple architectures in the same job
212+
return `${cacheKey}-${arch}`
208213
}
209214

210215
/**
@@ -219,6 +224,7 @@ const prepareBuild = async (repositoryName, repositoryUrl, manifestPath, cacheBu
219224
* @param {string} localRepoName The flatpak repository name
220225
* @param {boolean} cacheBuildDir Whether to enable caching the build directory
221226
* @param {string | undefined} cacheKey the default cache key if there are any
227+
* @param {string} arch The CPU architecture to build for
222228
*/
223229
const run = async (
224230
manifestPath,
@@ -229,10 +235,11 @@ const run = async (
229235
buildDir,
230236
localRepoName,
231237
cacheBuildDir,
232-
cacheKey = undefined
238+
cacheKey = undefined,
239+
arch
233240
) => {
234241
try {
235-
cacheKey = await prepareBuild(repositoryName, repositoryUrl, manifestPath, cacheBuildDir, cacheKey)
242+
cacheKey = await prepareBuild(repositoryName, repositoryUrl, manifestPath, cacheBuildDir, cacheKey, arch)
236243
} catch (err) {
237244
core.setFailed(`Failed to prepare the build ${err}`)
238245
}
@@ -243,13 +250,15 @@ const run = async (
243250
return saveManifest(modifiedManifest, manifestPath)
244251
})
245252
.then((manifest) => {
246-
return build(manifest, manifestPath, bundle, repositoryUrl, repositoryName, buildDir, localRepoName, cacheBuildDir, cacheKey)
253+
return build(manifest, manifestPath, bundle, repositoryUrl, repositoryName, buildDir, localRepoName, cacheBuildDir, cacheKey, arch)
247254
})
248255
.then(() => {
249256
core.info('Uploading artifact...')
250257
const artifactClient = artifact.create()
251258

252-
return artifactClient.uploadArtifact(bundle.replace('.flatpak', ''), [bundle], '.', {
259+
// Append the arch to the bundle name to prevent conflicts in multi-arch jobs
260+
const bundleName = bundle.replace('.flatpak', '') + `-${arch}`
261+
return artifactClient.uploadArtifact(bundleName, [bundle], '.', {
253262
continueOnError: false
254263
})
255264
})
@@ -277,6 +286,7 @@ if (require.main === module) {
277286
'flatpak_app',
278287
'repo',
279288
['y', 'yes', 'true', 'enabled', true].includes(core.getInput('cache')),
280-
core.getInput('cache-key')
289+
core.getInput('cache-key'),
290+
core.getInput('arch')
281291
)
282292
}

0 commit comments

Comments
 (0)