Skip to content

Commit

Permalink
Added the caching implementation (#113)
Browse files Browse the repository at this point in the history
* Updated the .gitignore to exclude Webstorm files and the yarn-error.log

* Added the @actions/cache package

* Added caching with @actions/cache

* Added a CACHE_ACTIVE constant to prepare for the optional input

* Added the cache input

* Added the cache info to the README.md

* Code cleanup

* Chore: build and update index.js
  • Loading branch information
Marko19907 authored Jun 26, 2023
1 parent 443fe44 commit 48ea6c4
Show file tree
Hide file tree
Showing 9 changed files with 58,872 additions and 5,792 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.vscode/
.idea/
node_modules/
/yarn-error.log
lib/
.mono/
.godot
.godot
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ Define at least 1 export preset by going to `Project -> Export` in the Godot edi
### Action Inputs

| Input Name | Description | Type | Default | Required |
| ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------- | ------- | -------- |
|---------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------|---------|----------|
| `godot_executable_download_url` | The **Linux Headless** version of Godot that you want to use to export your project. If you do not use the Linux Headless version exporting will fail. | `string` | | Yes |
| `godot_export_templates_download_url` | The link to the `.tpz` archive of export templates. | `string` | | Yes |
| `relative_project_path` | The relative path to the directory containing your `project.godot` file. If your `project.godot` file is at the root of your repository then this value should be `./`. Do _not_ include `project.godot` as part of this path. | `string` | | Yes |
| `cache` | Use the GitHub Actions workflow cache to cache the Godot export templates and Godot executable. Helps speed up builds by not having to download them every time. | `boolean` | `true` | No |
| `export_debug` | Export builds in debug mode. | `boolean` | `false` | No |
| `archive_output` | Archive each export into a `.zip` file. | `boolean` | `false` | No |
| `archive_root_folder` | Place exported files under a root folder when archiving, rather than placing the files themselves at the root of the archive. | `boolean` | `false` | No |
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ inputs:
description: If "true", files that are archived will be placed in a root folder within the archive.
default: false
required: false
cache:
description: If "true", the Godot executable and export templates will be cached in the GitHub Actions workspace.
default: true
required: false
relative_export_path:
description: If provided, exports will be moved to this directory relative to the root of the Git repository. This setting is overridden by "use_preset_export_path".
default: ''
Expand Down
62,483 changes: 58,489 additions & 3,994 deletions dist/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"homepage": "https://github.com/firebelley/godot-export#readme",
"dependencies": {
"@actions/cache": "^3.2.1",
"@actions/core": "^1.10.0",
"@actions/exec": "^1.1.1",
"@actions/io": "^1.1.3",
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import * as os from 'os';

const ARCHIVE_OUTPUT = core.getInput('archive_output') === 'true';
const CACHE_ACTIVE = core.getInput('cache') === 'true';
const GENERATE_RELEASE_NOTES = core.getInput('generate_release_notes') === 'true';
const GODOT_DOWNLOAD_URL = core.getInput('godot_executable_download_url');
const GODOT_TEMPLATES_DOWNLOAD_URL = core.getInput('godot_export_templates_download_url');
Expand Down Expand Up @@ -34,6 +35,7 @@ const GODOT_PROJECT_FILE_PATH = path.join(GODOT_PROJECT_PATH, 'project.godot');
export {
ARCHIVE_OUTPUT,
ARCHIVE_ROOT_FOLDER,
CACHE_ACTIVE,
EXPORT_DEBUG,
EXPORT_PACK_ONLY,
GENERATE_RELEASE_NOTES,
Expand Down
60 changes: 53 additions & 7 deletions src/godot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { exec, ExecOptions } from '@actions/exec';
import * as core from '@actions/core';
import { isFeatureAvailable, restoreCache, saveCache } from '@actions/cache';
import * as io from '@actions/io';
import * as path from 'path';
import * as fs from 'fs';
Expand All @@ -20,6 +21,7 @@ import {
EXPORT_PACK_ONLY,
USE_GODOT_3,
GODOT_EXPORT_TEMPLATES_PATH,
CACHE_ACTIVE,
} from './constants';

const GODOT_EXECUTABLE = 'godot_executable';
Expand Down Expand Up @@ -85,18 +87,62 @@ async function setupWorkingPath(): Promise<void> {
core.info(`Working path created ${GODOT_WORKING_PATH}`);
}

async function downloadTemplates(): Promise<void> {
core.info(`Downloading Godot export templates from ${GODOT_TEMPLATES_DOWNLOAD_URL}`);
async function downloadFile(
filePath: string,
downloadUrl: string,
cacheKey: string,
restoreKey: string,
): Promise<void> {
if (CACHE_ACTIVE && isCacheFeatureAvailable()) {
const cacheHit = await restoreCache([filePath], cacheKey, [restoreKey]);
if (cacheHit) {
core.info(`Restored cached file from ${cacheHit}`);
return;
}
}
core.info(`Downloading file from ${downloadUrl}`);
await exec('wget', ['-nv', downloadUrl, '-O', filePath]);
if (CACHE_ACTIVE && isCacheFeatureAvailable()) {
await saveCache([filePath], cacheKey);
}
}

const file = path.join(GODOT_WORKING_PATH, GODOT_TEMPLATES_FILENAME);
await exec('wget', ['-nv', GODOT_TEMPLATES_DOWNLOAD_URL, '-O', file]);
async function downloadTemplates(): Promise<void> {
const templatesPath = path.join(GODOT_WORKING_PATH, GODOT_TEMPLATES_FILENAME);
const cacheKey = `godot-templates-${GODOT_TEMPLATES_DOWNLOAD_URL}`;
const restoreKey = `godot-templates-`;
await downloadFile(templatesPath, GODOT_TEMPLATES_DOWNLOAD_URL, cacheKey, restoreKey);
}

async function downloadExecutable(): Promise<void> {
core.info(`Downloading Godot executable from ${GODOT_DOWNLOAD_URL}`);
const executablePath = path.join(GODOT_WORKING_PATH, GODOT_ZIP);
const cacheKey = `godot-executable-${GODOT_DOWNLOAD_URL}`;
const restoreKey = `godot-executable-`;
await downloadFile(executablePath, GODOT_DOWNLOAD_URL, cacheKey, restoreKey);
}

function isGhes(): boolean {
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}

/**
* Checks if the cache service is available for this runner.
* Taken from https://github.com/actions/setup-node/blob/main/src/cache-utils.ts
*/
function isCacheFeatureAvailable(): boolean {
if (isFeatureAvailable()) return true;

if (isGhes()) {
core.warning(
'Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.',
);
return false;
}

core.warning('The runner was not able to contact the cache service. Caching will be skipped');

const file = path.join(GODOT_WORKING_PATH, GODOT_ZIP);
await exec('wget', ['-nv', GODOT_DOWNLOAD_URL, '-O', file]);
return false;
}

async function prepareExecutable(): Promise<void> {
Expand Down
Loading

0 comments on commit 48ea6c4

Please sign in to comment.