Merge pull request #48 from DeterminateSystems/double-action

Gracefully handle it if a user tries to register the action twice.
This commit is contained in:
Graham Christensen 2024-05-07 10:21:29 -04:00 committed by GitHub
commit c9cb2a2a7f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 92 additions and 34 deletions

48
dist/index.js generated vendored
View file

@ -57770,7 +57770,7 @@ module.exports = function () {
/***/ }), /***/ }),
/***/ 4791: /***/ 4736:
/***/ ((module) => { /***/ ((module) => {
@ -57985,7 +57985,7 @@ module.exports = {
const { maxNameValuePairSize, maxAttributeValueSize } = __nccwpck_require__(4791) const { maxNameValuePairSize, maxAttributeValueSize } = __nccwpck_require__(4736)
const { isCTLExcludingHtab } = __nccwpck_require__(3319) const { isCTLExcludingHtab } = __nccwpck_require__(3319)
const { collectASequenceOfCodePointsFast } = __nccwpck_require__(6932) const { collectASequenceOfCodePointsFast } = __nccwpck_require__(6932)
const assert = __nccwpck_require__(9491) const assert = __nccwpck_require__(9491)
@ -93752,7 +93752,7 @@ const got = source_create(defaults);
const external_node_stream_promises_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:stream/promises"); const external_node_stream_promises_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:stream/promises");
;// CONCATENATED MODULE: external "node:zlib" ;// CONCATENATED MODULE: external "node:zlib"
const external_node_zlib_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:zlib"); const external_node_zlib_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:zlib");
;// CONCATENATED MODULE: ./node_modules/.pnpm/github.com+DeterminateSystems+detsys-ts@b2ff406239cb9d311fa9dad332ccfee2669ce6dc_o2lwkok3rvt3rnal2ayts63upe/node_modules/detsys-ts/dist/index.js ;// CONCATENATED MODULE: ./node_modules/.pnpm/github.com+DeterminateSystems+detsys-ts@38e5bec8265276e0ffc60a474eeb0fe563690ad1_ohjjfxyz5zc5v7acp635bu77r4/node_modules/detsys-ts/dist/index.js
var __defProp = Object.defineProperty; var __defProp = Object.defineProperty;
var __export = (target, all) => { var __export = (target, all) => {
for (var name in all) for (var name in all)
@ -94117,7 +94117,11 @@ var getArrayOfStrings = (name, separator) => {
}; };
var handleString = (input, separator) => { var handleString = (input, separator) => {
const sepChar = separator === "comma" ? "," : /\s+/; const sepChar = separator === "comma" ? "," : /\s+/;
return input.trim().split(sepChar).map((s) => s.trim()); const trimmed = input.trim();
if (trimmed === "") {
return [];
}
return trimmed.split(sepChar).map((s) => s.trim());
}; };
var getMultilineStringOrNull = (name) => { var getMultilineStringOrNull = (name) => {
const value = core.getMultilineInput(name); const value = core.getMultilineInput(name);
@ -94741,9 +94745,11 @@ async function flakeHubLogin(netrc) {
var ENV_CACHE_DAEMONDIR = "MAGIC_NIX_CACHE_DAEMONDIR"; var ENV_DAEMON_DIR = "MAGIC_NIX_CACHE_DAEMONDIR";
var ENV_CACHE_STARTED = "MAGIC_NIX_CACHE_STARTED"; var STATE_DAEMONDIR = "MAGIC_NIX_CACHE_DAEMONDIR";
var STATE_STARTED = "MAGIC_NIX_CACHE_STARTED";
var STARTED_HINT = "true"; var STARTED_HINT = "true";
var NOOP_TEXT = "Magic Nix Cache is already running, this workflow job is in noop mode. Is the Magic Nix Cache in the workflow twice?";
var MagicNixCacheAction = class { var MagicNixCacheAction = class {
constructor() { constructor() {
this.idslib = new IdsToolbox({ this.idslib = new IdsToolbox({
@ -94767,14 +94773,21 @@ var MagicNixCacheAction = class {
] ]
} }
}); });
this.daemonStarted = process.env[ENV_CACHE_STARTED] === STARTED_HINT; this.daemonStarted = core.getState(STATE_STARTED) === STARTED_HINT;
if (process.env[ENV_CACHE_DAEMONDIR]) { if (core.getState(STATE_DAEMONDIR) !== "") {
this.daemonDir = process.env[ENV_CACHE_DAEMONDIR]; this.daemonDir = core.getState(STATE_DAEMONDIR);
} else { } else {
this.daemonDir = this.idslib.getTemporaryName(); this.daemonDir = this.idslib.getTemporaryName();
(0,external_node_fs_namespaceObject.mkdirSync)(this.daemonDir); (0,external_node_fs_namespaceObject.mkdirSync)(this.daemonDir);
core.exportVariable(ENV_CACHE_DAEMONDIR, this.daemonDir); core.saveState(STATE_DAEMONDIR, this.daemonDir);
} }
if (process.env[ENV_DAEMON_DIR] === void 0) {
this.noopMode = false;
core.exportVariable(ENV_DAEMON_DIR, this.daemonDir);
} else {
this.noopMode = process.env[ENV_DAEMON_DIR] !== this.daemonDir;
}
this.idslib.addFact("noop_mode", this.noopMode);
this.idslib.stapleFile( this.idslib.stapleFile(
"daemon.log", "daemon.log",
external_node_path_namespaceObject.join(this.daemonDir, "daemon.log") external_node_path_namespaceObject.join(this.daemonDir, "daemon.log")
@ -94795,14 +94808,19 @@ var MagicNixCacheAction = class {
); );
} }
} }
this.idslib.addFact("authenticated_env", !anyMissing);
if (anyMissing) { if (anyMissing) {
return; return;
} }
if (this.daemonStarted) {
core.debug("Already started.");
return;
}
core.debug( core.debug(
`GitHub Action Cache URL: ${process.env["ACTIONS_CACHE_URL"]}` `GitHub Action Cache URL: ${process.env["ACTIONS_CACHE_URL"]}`
); );
this.daemonStarted = true; this.daemonStarted = true;
core.exportVariable(ENV_CACHE_STARTED, STARTED_HINT); core.saveState(STATE_STARTED, STARTED_HINT);
const sourceBinary = inputs_exports.getStringOrNull("source-binary"); const sourceBinary = inputs_exports.getStringOrNull("source-binary");
const daemonBin = sourceBinary !== null ? sourceBinary : await this.fetchAutoCacher(); const daemonBin = sourceBinary !== null ? sourceBinary : await this.fetchAutoCacher();
let runEnv; let runEnv;
@ -94968,10 +94986,18 @@ var MagicNixCacheAction = class {
function main() { function main() {
const cacheAction = new MagicNixCacheAction(); const cacheAction = new MagicNixCacheAction();
cacheAction.idslib.onMain(async () => { cacheAction.idslib.onMain(async () => {
if (cacheAction.noopMode) {
core.warning(NOOP_TEXT);
return;
}
await cacheAction.setUpAutoCache(); await cacheAction.setUpAutoCache();
await cacheAction.notifyAutoCache(); await cacheAction.notifyAutoCache();
}); });
cacheAction.idslib.onPost(async () => { cacheAction.idslib.onPost(async () => {
if (cacheAction.noopMode) {
core.debug(NOOP_TEXT);
return;
}
await cacheAction.tearDownAutoCache(); await cacheAction.tearDownAutoCache();
}); });
cacheAction.idslib.execute(); cacheAction.idslib.execute();

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View file

@ -27,13 +27,13 @@
"dependencies": { "dependencies": {
"@actions/core": "^1.10.1", "@actions/core": "^1.10.1",
"@actions/exec": "^1.1.1", "@actions/exec": "^1.1.1",
"detsys-ts": "github:DeterminateSystems/detsys-ts", "detsys-ts": "github:DeterminateSystems/detsys-ts#array-inputs",
"got": "^14.2.1", "got": "^14.2.1",
"tail": "^2.2.6" "tail": "^2.2.6"
}, },
"devDependencies": { "devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.3.0", "@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/node": "^20.12.9", "@types/node": "^20.12.10",
"@types/tail": "^2.2.3", "@types/tail": "^2.2.3",
"@types/uuid": "^9.0.8", "@types/uuid": "^9.0.8",
"@typescript-eslint/eslint-plugin": "^7.8.0", "@typescript-eslint/eslint-plugin": "^7.8.0",

View file

@ -12,8 +12,8 @@ dependencies:
specifier: ^1.1.1 specifier: ^1.1.1
version: 1.1.1 version: 1.1.1
detsys-ts: detsys-ts:
specifier: github:DeterminateSystems/detsys-ts specifier: github:DeterminateSystems/detsys-ts#array-inputs
version: github.com/DeterminateSystems/detsys-ts/b2ff406239cb9d311fa9dad332ccfee2669ce6dc version: github.com/DeterminateSystems/detsys-ts/38e5bec8265276e0ffc60a474eeb0fe563690ad1
got: got:
specifier: ^14.2.1 specifier: ^14.2.1
version: 14.2.1 version: 14.2.1
@ -26,8 +26,8 @@ devDependencies:
specifier: ^4.3.0 specifier: ^4.3.0
version: 4.3.0(prettier@3.2.5) version: 4.3.0(prettier@3.2.5)
'@types/node': '@types/node':
specifier: ^20.12.9 specifier: ^20.12.10
version: 20.12.9 version: 20.12.10
'@types/tail': '@types/tail':
specifier: ^2.2.3 specifier: ^2.2.3
version: 2.2.3 version: 2.2.3
@ -896,12 +896,12 @@ packages:
/@types/node-fetch@2.6.11: /@types/node-fetch@2.6.11:
resolution: {integrity: sha512-24xFj9R5+rfQJLRyM56qh+wnVSYhyXC2tkoBndtY0U+vubqNsYXGjufB2nn8Q6gt0LrARwL6UBtMCSVCwl4B1g==} resolution: {integrity: sha512-24xFj9R5+rfQJLRyM56qh+wnVSYhyXC2tkoBndtY0U+vubqNsYXGjufB2nn8Q6gt0LrARwL6UBtMCSVCwl4B1g==}
dependencies: dependencies:
'@types/node': 20.12.9 '@types/node': 20.12.10
form-data: 4.0.0 form-data: 4.0.0
dev: false dev: false
/@types/node@20.12.9: /@types/node@20.12.10:
resolution: {integrity: sha512-o93r47yu04MHumPBCFg0bMPBMNgtMg3jzbhl7e68z50+BMHmRMGDJv13eBlUgOdc9i/uoJXGMGYLtJV4ReTXEg==} resolution: {integrity: sha512-Eem5pH9pmWBHoGAT8Dr5fdc5rYA+4NAovdM4EktRPVAAiJhmWWfQrA0cFhAbOsQdSfIHjAud6YdkbL69+zSKjw==}
dependencies: dependencies:
undici-types: 5.26.5 undici-types: 5.26.5
@ -916,7 +916,7 @@ packages:
/@types/tunnel@0.0.3: /@types/tunnel@0.0.3:
resolution: {integrity: sha512-sOUTGn6h1SfQ+gbgqC364jLFBw2lnFqkgF3q0WovEHRLMrVD1sd5aufqi/aJObLekJO+Aq5z646U4Oxy6shXMA==} resolution: {integrity: sha512-sOUTGn6h1SfQ+gbgqC364jLFBw2lnFqkgF3q0WovEHRLMrVD1sd5aufqi/aJObLekJO+Aq5z646U4Oxy6shXMA==}
dependencies: dependencies:
'@types/node': 20.12.9 '@types/node': 20.12.10
dev: false dev: false
/@types/uuid@9.0.8: /@types/uuid@9.0.8:
@ -1275,7 +1275,7 @@ packages:
hasBin: true hasBin: true
dependencies: dependencies:
caniuse-lite: 1.0.30001616 caniuse-lite: 1.0.30001616
electron-to-chromium: 1.4.756 electron-to-chromium: 1.4.757
node-releases: 2.0.14 node-releases: 2.0.14
update-browserslist-db: 1.0.15(browserslist@4.23.0) update-browserslist-db: 1.0.15(browserslist@4.23.0)
dev: true dev: true
@ -1533,8 +1533,8 @@ packages:
resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==} resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==}
dev: true dev: true
/electron-to-chromium@1.4.756: /electron-to-chromium@1.4.757:
resolution: {integrity: sha512-RJKZ9+vEBMeiPAvKNWyZjuYyUqMndcP1f335oHqn3BEQbs2NFtVrnK5+6Xg5wSM9TknNNpWghGDUCKGYF+xWXw==} resolution: {integrity: sha512-jftDaCknYSSt/+KKeXzH3LX5E2CvRLm75P3Hj+J/dv3CL0qUYcOt13d5FN1NiL5IJbbhzHrb3BomeG2tkSlZmw==}
dev: true dev: true
/emoji-regex@8.0.0: /emoji-regex@8.0.0:
@ -3831,8 +3831,8 @@ packages:
engines: {node: '>=10'} engines: {node: '>=10'}
dev: true dev: true
github.com/DeterminateSystems/detsys-ts/b2ff406239cb9d311fa9dad332ccfee2669ce6dc: github.com/DeterminateSystems/detsys-ts/38e5bec8265276e0ffc60a474eeb0fe563690ad1:
resolution: {tarball: https://codeload.github.com/DeterminateSystems/detsys-ts/tar.gz/b2ff406239cb9d311fa9dad332ccfee2669ce6dc} resolution: {tarball: https://codeload.github.com/DeterminateSystems/detsys-ts/tar.gz/38e5bec8265276e0ffc60a474eeb0fe563690ad1}
name: detsys-ts name: detsys-ts
version: 1.0.0 version: 1.0.0
dependencies: dependencies:

View file

@ -10,15 +10,23 @@ import * as os from "node:os";
import * as path from "node:path"; import * as path from "node:path";
import { inspect, promisify } from "node:util"; import { inspect, promisify } from "node:util";
const ENV_CACHE_DAEMONDIR = "MAGIC_NIX_CACHE_DAEMONDIR"; // The ENV_DAEMON_DIR is intended to determine if we "own" the daemon or not,
const ENV_CACHE_STARTED = "MAGIC_NIX_CACHE_STARTED"; // in the case that a user has put the magic nix cache into their workflow
// twice.
const ENV_DAEMON_DIR = "MAGIC_NIX_CACHE_DAEMONDIR";
const STATE_DAEMONDIR = "MAGIC_NIX_CACHE_DAEMONDIR";
const STATE_STARTED = "MAGIC_NIX_CACHE_STARTED";
const STARTED_HINT = "true"; const STARTED_HINT = "true";
const NOOP_TEXT =
"Magic Nix Cache is already running, this workflow job is in noop mode. Is the Magic Nix Cache in the workflow twice?";
class MagicNixCacheAction { class MagicNixCacheAction {
idslib: IdsToolbox; idslib: IdsToolbox;
private client: Got; private client: Got;
noopMode: boolean;
private daemonDir: string; private daemonDir: string;
private daemonStarted: boolean; private daemonStarted: boolean;
@ -46,16 +54,24 @@ class MagicNixCacheAction {
}, },
}); });
this.daemonStarted = process.env[ENV_CACHE_STARTED] === STARTED_HINT; this.daemonStarted = actionsCore.getState(STATE_STARTED) === STARTED_HINT;
if (process.env[ENV_CACHE_DAEMONDIR]) { if (actionsCore.getState(STATE_DAEMONDIR) !== "") {
this.daemonDir = process.env[ENV_CACHE_DAEMONDIR]; this.daemonDir = actionsCore.getState(STATE_DAEMONDIR);
} else { } else {
this.daemonDir = this.idslib.getTemporaryName(); this.daemonDir = this.idslib.getTemporaryName();
mkdirSync(this.daemonDir); mkdirSync(this.daemonDir);
actionsCore.exportVariable(ENV_CACHE_DAEMONDIR, this.daemonDir); actionsCore.saveState(STATE_DAEMONDIR, this.daemonDir);
} }
if (process.env[ENV_DAEMON_DIR] === undefined) {
this.noopMode = false;
actionsCore.exportVariable(ENV_DAEMON_DIR, this.daemonDir);
} else {
this.noopMode = process.env[ENV_DAEMON_DIR] !== this.daemonDir;
}
this.idslib.addFact("noop_mode", this.noopMode);
this.idslib.stapleFile( this.idslib.stapleFile(
"daemon.log", "daemon.log",
path.join(this.daemonDir, "daemon.log"), path.join(this.daemonDir, "daemon.log"),
@ -79,16 +95,22 @@ class MagicNixCacheAction {
} }
} }
this.idslib.addFact("authenticated_env", !anyMissing);
if (anyMissing) { if (anyMissing) {
return; return;
} }
if (this.daemonStarted) {
actionsCore.debug("Already started.");
return;
}
actionsCore.debug( actionsCore.debug(
`GitHub Action Cache URL: ${process.env["ACTIONS_CACHE_URL"]}`, `GitHub Action Cache URL: ${process.env["ACTIONS_CACHE_URL"]}`,
); );
this.daemonStarted = true; this.daemonStarted = true;
actionsCore.exportVariable(ENV_CACHE_STARTED, STARTED_HINT); actionsCore.saveState(STATE_STARTED, STARTED_HINT);
const sourceBinary = inputs.getStringOrNull("source-binary"); const sourceBinary = inputs.getStringOrNull("source-binary");
const daemonBin = const daemonBin =
sourceBinary !== null ? sourceBinary : await this.fetchAutoCacher(); sourceBinary !== null ? sourceBinary : await this.fetchAutoCacher();
@ -298,10 +320,20 @@ function main(): void {
const cacheAction = new MagicNixCacheAction(); const cacheAction = new MagicNixCacheAction();
cacheAction.idslib.onMain(async () => { cacheAction.idslib.onMain(async () => {
if (cacheAction.noopMode) {
actionsCore.warning(NOOP_TEXT);
return;
}
await cacheAction.setUpAutoCache(); await cacheAction.setUpAutoCache();
await cacheAction.notifyAutoCache(); await cacheAction.notifyAutoCache();
}); });
cacheAction.idslib.onPost(async () => { cacheAction.idslib.onPost(async () => {
if (cacheAction.noopMode) {
actionsCore.debug(NOOP_TEXT);
return;
}
await cacheAction.tearDownAutoCache(); await cacheAction.tearDownAutoCache();
}); });