Skip to content

Commit

Permalink
Add a restore-cache option
Browse files Browse the repository at this point in the history
  • Loading branch information
tytan652 authored and bilelmoussaoui committed Mar 19, 2023
1 parent b192481 commit 746b7a9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
| `run-tests` | Enable/Disable running tests. This overrides the `flatpak-builder` option of the same name, which invokes `make check` or `ninja test`. Network and X11 access is enabled, with a display server provided by `xvfb-run`. | Optional | `false` |
| `branch` | The default flatpak branch | Optional | `master` |
| `cache` | Enable/Disable caching `.flatpak-builder` directory | Optional | `true` |
| `restore-cache` | Enable/Disable cache restoring. If caching is enabled. | Optional | `true` |
| `cache-key` | Specifies the cache key. CPU arch is automatically added, so there is no need to add it to the cache key. | Optional | `flatpak-builder-${sha256(manifestPath)}` |
| `arch` | Specifies the CPU architecture to build for | Optional | `x86_64` |
| `mirror-screenshots-url` | Specifies the URL to mirror screenshots | Optional | - |
Expand Down
6 changes: 6 additions & 0 deletions flatpak-builder/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ inputs:
Possible values: true, enabled, yes, y. Or something else to disable it.
default: "true"
required: false
restore-cache:
description: >
Toggles restoring the cache.
Possible values: true, enabled, yes, y. Or something else to disable it.
default: "true"
required: false
cache-key:
description: >
Defines the cache-key to use.
Expand Down
10 changes: 7 additions & 3 deletions flatpak-builder/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,19 @@ const build = async (manifest, manifestPath, stopAtModule, bundle, buildBundle,
* @param {string} repositoryName the repository name to install the runtime from
* @param {string} repositoryUrl the repository url
* @param {Boolean} cacheBuildDir whether to cache the build dir or not
* @param {Boolean} restoreCache whether to restore the cache of the build dir or not
* @param {string} cacheKey the default cache key if there are any
* @returns {Promise<String>} the cacheHitKey if a cache was hit
*/
const prepareBuild = async (repositoryName, repositoryUrl, cacheBuildDir, cacheKey) => {
const prepareBuild = async (repositoryName, repositoryUrl, cacheBuildDir, restoreCache, cacheKey) => {
/// If the user has set a different runtime source
if (repositoryUrl !== 'https://flathub.org/repo/flathub.flatpakrepo') {
await exec.exec('flatpak', ['remote-add', '--if-not-exists', repositoryName, repositoryUrl])
}

// Restore the cache in case caching is enabled
let cacheHitKey
if (cacheBuildDir) {
if (cacheBuildDir && restoreCache) {
cacheHitKey = await cache.restoreCache(
CACHE_PATH,
`${cacheKey}`,
Expand Down Expand Up @@ -272,6 +273,7 @@ const prepareBuild = async (repositoryName, repositoryUrl, cacheBuildDir, cacheK
* @param {string} buildDir Where to build the application
* @param {string} localRepoName The flatpak repository name
* @param {boolean} cacheBuildDir Whether to enable caching the build directory
* @param {boolean} restoreCache Whether to restore the cache or not
* @param {string} cacheKey the default cache key if there are any
* @param {string} arch The CPU architecture to build for
* @param {string} mirrorScreenshotsUrl The URL to mirror screenshots
Expand All @@ -287,6 +289,7 @@ const run = async (
buildDir,
localRepoName,
cacheBuildDir,
restoreCache,
cacheKey,
arch,
mirrorScreenshotsUrl
Expand All @@ -304,7 +307,7 @@ const run = async (

let cacheHitKey
try {
cacheHitKey = await prepareBuild(repositoryName, repositoryUrl, cacheBuildDir, cacheKey)
cacheHitKey = await prepareBuild(repositoryName, repositoryUrl, cacheBuildDir, restoreCache, cacheKey)
} catch (err) {
core.setFailed(`Failed to prepare the build ${err}`)
}
Expand Down Expand Up @@ -376,6 +379,7 @@ if (require.main === require.cache[eval('__filename')]) {
'flatpak_app',
'repo',
['y', 'yes', 'true', 'enabled', true].includes(core.getInput('cache')),
['y', 'yes', 'true', 'enabled', true].includes(core.getInput('restore-cache')),
core.getInput('cache-key'),
core.getInput('arch'),
core.getInput('mirror-screenshots-url')
Expand Down
10 changes: 7 additions & 3 deletions flatpak-builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,19 @@ const build = async (manifest, manifestPath, stopAtModule, bundle, buildBundle,
* @param {string} repositoryName the repository name to install the runtime from
* @param {string} repositoryUrl the repository url
* @param {Boolean} cacheBuildDir whether to cache the build dir or not
* @param {Boolean} restoreCache whether to restore the cache of the build dir or not
* @param {string} cacheKey the default cache key if there are any
* @returns {Promise<String>} the cacheHitKey if a cache was hit
*/
const prepareBuild = async (repositoryName, repositoryUrl, cacheBuildDir, cacheKey) => {
const prepareBuild = async (repositoryName, repositoryUrl, cacheBuildDir, restoreCache, cacheKey) => {
/// If the user has set a different runtime source
if (repositoryUrl !== 'https://flathub.org/repo/flathub.flatpakrepo') {
await exec.exec('flatpak', ['remote-add', '--if-not-exists', repositoryName, repositoryUrl])
}

// Restore the cache in case caching is enabled
let cacheHitKey
if (cacheBuildDir) {
if (cacheBuildDir && restoreCache) {
cacheHitKey = await cache.restoreCache(
CACHE_PATH,
`${cacheKey}`,
Expand Down Expand Up @@ -266,6 +267,7 @@ const prepareBuild = async (repositoryName, repositoryUrl, cacheBuildDir, cacheK
* @param {string} buildDir Where to build the application
* @param {string} localRepoName The flatpak repository name
* @param {boolean} cacheBuildDir Whether to enable caching the build directory
* @param {boolean} restoreCache Whether to restore the cache or not
* @param {string} cacheKey the default cache key if there are any
* @param {string} arch The CPU architecture to build for
* @param {string} mirrorScreenshotsUrl The URL to mirror screenshots
Expand All @@ -281,6 +283,7 @@ const run = async (
buildDir,
localRepoName,
cacheBuildDir,
restoreCache,
cacheKey,
arch,
mirrorScreenshotsUrl
Expand All @@ -298,7 +301,7 @@ const run = async (

let cacheHitKey
try {
cacheHitKey = await prepareBuild(repositoryName, repositoryUrl, cacheBuildDir, cacheKey)
cacheHitKey = await prepareBuild(repositoryName, repositoryUrl, cacheBuildDir, restoreCache, cacheKey)
} catch (err) {
core.setFailed(`Failed to prepare the build ${err}`)
}
Expand Down Expand Up @@ -370,6 +373,7 @@ if (require.main === module) {
'flatpak_app',
'repo',
['y', 'yes', 'true', 'enabled', true].includes(core.getInput('cache')),
['y', 'yes', 'true', 'enabled', true].includes(core.getInput('restore-cache')),
core.getInput('cache-key'),
core.getInput('arch'),
core.getInput('mirror-screenshots-url')
Expand Down

0 comments on commit 746b7a9

Please sign in to comment.