Skip to content

Commit 05db15b

Browse files
flatpak-builder: fail properly in case we can't restore cache/setup a flatpak remote
1 parent 8db89dc commit 05db15b

File tree

2 files changed

+96
-48
lines changed

2 files changed

+96
-48
lines changed

flatpak-builder/dist/index.js

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,60 @@ const build = async (manifest, manifestPath, bundle, repositoryUrl, repositoryNa
171171
])
172172
}
173173

174+
/**
175+
* Initialize the build
176+
* It consists mostly of setting up the flatpak remote if one other than the default is set
177+
* along with restoring the cache from the latest build
178+
*
179+
* @param {string} repositoryName the repository name to install the runtime from
180+
* @param {string} repositoryUrl the repository url
181+
* @param {PathLike} manifestPath the manifest path
182+
* @param {Boolean} cacheBuildDir whether to cache the build dir or not
183+
* @param {string | undefined} cacheKey the default cache key if there are any
184+
* @returns {Promise<String>} the new cacheKey if none was set before
185+
*/
186+
const prepareBuild = async (repositoryName, repositoryUrl, manifestPath, cacheBuildDir, cacheKey = undefined) => {
187+
/// If the user has set a different runtime source
188+
if (repositoryUrl !== 'https://flathub.org/repo/flathub.flatpakrepo') {
189+
await exec.exec('flatpak', ['remote-add', '--if-not-exists', repositoryName, repositoryUrl])
190+
}
191+
192+
// Restore the cache in case caching is enabled
193+
if (cacheBuildDir) {
194+
if (cacheKey === undefined) {
195+
const manifestHash = (await computeHash(manifestPath)).substring(0, 20)
196+
cacheKey = `flatpak-builder-${manifestHash}`
197+
}
198+
199+
const cacheHitKey = await cache.restoreCache(
200+
CACHE_PATH,
201+
cacheKey,
202+
[
203+
'flatpak-builder-',
204+
'flatpak-'
205+
]
206+
)
207+
if (cacheHitKey !== undefined) {
208+
core.info(`Restored cache with key: ${cacheHitKey}`)
209+
} else {
210+
core.info('No cache was found')
211+
}
212+
}
213+
return cacheKey
214+
}
215+
174216
/**
175217
* Run a complete build
176218
*
177219
* @param {object} manifestPath The flatpak manifest path
178220
* @param {boolean} runTests Whether to run tests or not
179221
* @param {string} bundle The bundle's name
180222
* @param {string} repositoryUrl The repository used to install the runtime from
223+
* @param {string} repositoryName the repository name to install the runtime from
181224
* @param {string} buildDir Where to build the application
182225
* @param {string} localRepoName The flatpak repository name
183226
* @param {boolean} cacheBuildDir Whether to enable caching the build directory
227+
* @param {string | undefined} cacheKey the default cache key if there are any
184228
*/
185229
const run = async (
186230
manifestPath,
@@ -193,32 +237,12 @@ const run = async (
193237
cacheBuildDir,
194238
cacheKey = undefined
195239
) => {
196-
if (cacheKey === undefined) {
197-
const manifestHash = (await computeHash(manifestPath)).substring(0, 20)
198-
cacheKey = `flatpak-builder-${manifestHash}`
199-
}
200-
201-
/// If the user has set a different runtime source
202-
if (repositoryUrl !== 'https://flathub.org/repo/flathub.flatpakrepo') {
203-
await exec.exec('flatpak', ['remote-add', '--if-not-exists', repositoryName, repositoryUrl])
240+
try {
241+
cacheKey = await prepareBuild(repositoryName, repositoryUrl, manifestPath, cacheBuildDir, cacheKey)
242+
} catch (err) {
243+
core.setFailed(`Failed to prepare the build ${err}`)
204244
}
205245

206-
// Restore the cache in case caching is enabled
207-
if (cacheBuildDir) {
208-
const cacheHitKey = await cache.restoreCache(
209-
CACHE_PATH,
210-
cacheKey,
211-
[
212-
'flatpak-builder-',
213-
'flatpak-'
214-
]
215-
)
216-
if (cacheHitKey !== undefined) {
217-
core.info(`Restored cache with key: ${cacheHitKey}`)
218-
} else {
219-
core.info('No cache was found')
220-
}
221-
}
222246
parseManifest(manifestPath)
223247
.then((manifest) => {
224248
const modifiedManifest = modifyManifest(manifest, runTests)

flatpak-builder/index.js

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -165,39 +165,30 @@ const build = async (manifest, manifestPath, bundle, repositoryUrl, repositoryNa
165165
}
166166

167167
/**
168-
* Run a complete build
168+
* Initialize the build
169+
* It consists mostly of setting up the flatpak remote if one other than the default is set
170+
* along with restoring the cache from the latest build
169171
*
170-
* @param {object} manifestPath The flatpak manifest path
171-
* @param {boolean} runTests Whether to run tests or not
172-
* @param {string} bundle The bundle's name
173-
* @param {string} repositoryUrl The repository used to install the runtime from
174-
* @param {string} buildDir Where to build the application
175-
* @param {string} localRepoName The flatpak repository name
176-
* @param {boolean} cacheBuildDir Whether to enable caching the build directory
172+
* @param {string} repositoryName the repository name to install the runtime from
173+
* @param {string} repositoryUrl the repository url
174+
* @param {PathLike} manifestPath the manifest path
175+
* @param {Boolean} cacheBuildDir whether to cache the build dir or not
176+
* @param {string | undefined} cacheKey the default cache key if there are any
177+
* @returns {Promise<String>} the new cacheKey if none was set before
177178
*/
178-
const run = async (
179-
manifestPath,
180-
runTests,
181-
bundle,
182-
repositoryUrl,
183-
repositoryName,
184-
buildDir,
185-
localRepoName,
186-
cacheBuildDir,
187-
cacheKey = undefined
188-
) => {
189-
if (cacheKey === undefined) {
190-
const manifestHash = (await computeHash(manifestPath)).substring(0, 20)
191-
cacheKey = `flatpak-builder-${manifestHash}`
192-
}
193-
179+
const prepareBuild = async (repositoryName, repositoryUrl, manifestPath, cacheBuildDir, cacheKey = undefined) => {
194180
/// If the user has set a different runtime source
195181
if (repositoryUrl !== 'https://flathub.org/repo/flathub.flatpakrepo') {
196182
await exec.exec('flatpak', ['remote-add', '--if-not-exists', repositoryName, repositoryUrl])
197183
}
198184

199185
// Restore the cache in case caching is enabled
200186
if (cacheBuildDir) {
187+
if (cacheKey === undefined) {
188+
const manifestHash = (await computeHash(manifestPath)).substring(0, 20)
189+
cacheKey = `flatpak-builder-${manifestHash}`
190+
}
191+
201192
const cacheHitKey = await cache.restoreCache(
202193
CACHE_PATH,
203194
cacheKey,
@@ -212,6 +203,39 @@ const run = async (
212203
core.info('No cache was found')
213204
}
214205
}
206+
return cacheKey
207+
}
208+
209+
/**
210+
* Run a complete build
211+
*
212+
* @param {object} manifestPath The flatpak manifest path
213+
* @param {boolean} runTests Whether to run tests or not
214+
* @param {string} bundle The bundle's name
215+
* @param {string} repositoryUrl The repository used to install the runtime from
216+
* @param {string} repositoryName the repository name to install the runtime from
217+
* @param {string} buildDir Where to build the application
218+
* @param {string} localRepoName The flatpak repository name
219+
* @param {boolean} cacheBuildDir Whether to enable caching the build directory
220+
* @param {string | undefined} cacheKey the default cache key if there are any
221+
*/
222+
const run = async (
223+
manifestPath,
224+
runTests,
225+
bundle,
226+
repositoryUrl,
227+
repositoryName,
228+
buildDir,
229+
localRepoName,
230+
cacheBuildDir,
231+
cacheKey = undefined
232+
) => {
233+
try {
234+
cacheKey = await prepareBuild(repositoryName, repositoryUrl, manifestPath, cacheBuildDir, cacheKey)
235+
} catch (err) {
236+
core.setFailed(`Failed to prepare the build ${err}`)
237+
}
238+
215239
parseManifest(manifestPath)
216240
.then((manifest) => {
217241
const modifiedManifest = modifyManifest(manifest, runTests)

0 commit comments

Comments
 (0)