diff --git a/README.md b/README.md index a533dc89..d8f0bcb5 100644 --- a/README.md +++ b/README.md @@ -51,19 +51,7 @@ jobs: signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}' # Only needed for private caches authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}' -``` - -Alternatively, you can use this action to only configure cachix for substitution: - -```yaml -... - - uses: cachix/cachix-action@v5 - with: - name: mycache - skipNixBuild: true - # Only needed for private caches - authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}' -... + - run: nix-build ``` See [action.yml](action.yml) for all options. diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts deleted file mode 100644 index 9a95d3e8..00000000 --- a/__tests__/main.test.ts +++ /dev/null @@ -1,13 +0,0 @@ -import {prependEach, nonEmptySplit} from '../src/strings' - -test('prependEach', async() => { - expect(prependEach('-A', ["foo", "bar"])).toEqual(["-A", "foo", "-A", "bar"]); - expect(prependEach('-A', [])).toEqual([]); -}); - -test('nonEmptySplit', async() => { - expect(nonEmptySplit("", /\s+/)).toEqual([]); - expect(nonEmptySplit("foo bar", /\s+/)).toEqual(["foo", "bar"]); -}) - -// TODO: hopefully github actions will support integration tests \ No newline at end of file diff --git a/action.yml b/action.yml index bb2d5efd..113f67e4 100644 --- a/action.yml +++ b/action.yml @@ -9,17 +9,12 @@ inputs: description: 'Authentication token for Cachix, needed only for private cache access' signingKey: description: 'Signing key secret retrieved after creating binary cache on https://cachix.org' - skipNixBuild: - description: 'Set to true to not invoke nix-build after setup.' - file: - description: 'Nix file to build. Defaults to default.nix' - attributes: - description: 'Nix attributes to nix-build. By default, all attributes are built.' - nixBuildArgs: - description: 'Additional arguments for nix-build.' + skipPush: + description: 'Set to true to disable pushing build results to the cache' branding: color: 'blue' icon: 'database' runs: using: 'node12' main: 'dist/main/index.js' + post: 'dist/main/index.js' diff --git a/dist/main/index.js b/dist/main/index.js index 6c991a67..c3390a7d 100644 --- a/dist/main/index.js +++ b/dist/main/index.js @@ -971,20 +971,18 @@ var __importStar = (this && this.__importStar) || function (mod) { }; Object.defineProperty(exports, "__esModule", { value: true }); const core = __importStar(__webpack_require__(470)); +const coreCommand = __importStar(__webpack_require__(431)); const exec = __importStar(__webpack_require__(986)); -const strings_1 = __webpack_require__(443); -function run() { +exports.IsPost = !!process.env['STATE_isPost']; +// inputs +const name = core.getInput('name', { required: true }); +const signingKey = core.getInput('signingKey'); +const authToken = core.getInput('authToken'); +const skipPush = core.getInput('skipPush'); +const cachixExecutable = '/nix/var/nix/profiles/per-user/runner/profile/bin/cachix'; +function setup() { return __awaiter(this, void 0, void 0, function* () { try { - // inputs - const name = core.getInput('name', { required: true }); - const file = core.getInput('file'); - const skipNixBuild = core.getInput('skipNixBuild'); - const attributes = core.getInput('attributes'); - const nixBuildArgs = core.getInput('nixBuildArgs'); - const signingKey = core.getInput('signingKey'); - const authToken = core.getInput('authToken'); - const cachixExecutable = "/nix/var/nix/profiles/per-user/runner/profile/bin/cachix"; core.startGroup('Cachix: installing'); yield exec.exec('nix-env', ['--quiet', '-iA', 'cachix', '-f', 'https://cachix.org/api/v1/install']); core.endGroup(); @@ -997,31 +995,23 @@ function run() { core.endGroup(); if (signingKey !== "") { core.exportVariable('CACHIX_SIGNING_KEY', signingKey); + // Remember existing store paths + yield exec.exec("sh", ["-c", `nix path-info --all | grep -v '\.drv$' > /tmp/store-path-pre-build`]); } - if (skipNixBuild !== 'true') { - const args = strings_1.prependEach('-A', strings_1.nonEmptySplit(attributes, /\s+/)).concat([file || "default.nix"]); - const additionalArgs = strings_1.nonEmptySplit(nixBuildArgs, /\s+/); - const allArgs = additionalArgs.concat(args); - core.startGroup(`nix-build ${allArgs.join(' ')}`); - if (signingKey !== "") { - // Remember existing store paths - yield exec.exec("sh", ["-c", `nix path-info --all | grep -v '\.drv$' > store-path-pre-build`]); - } - let paths = ''; - const options = { - listeners: { - stdout: (data) => { - paths += data.toString(); - }, - } - }; - yield exec.exec('nix-build', allArgs, options); + } + catch (error) { + core.setFailed(`Action failed with error: ${error}`); + throw (error); + } + }); +} +function upload() { + return __awaiter(this, void 0, void 0, function* () { + try { + if (signingKey !== "" && skipPush !== 'true') { + core.startGroup('Cachix: pushing paths'); + yield exec.exec("sh", ["-c", `nix path-info --all | grep -v '\.drv$' | cat - /tmp/store-path-pre-build | sort | uniq -u | ${cachixExecutable} push ${name}`]); core.endGroup(); - if (signingKey !== "") { - core.startGroup('Cachix: pushing paths'); - yield exec.exec("sh", ["-c", `nix path-info --all | grep -v '\.drv$' | cat - store-path-pre-build | sort | uniq -u | ${cachixExecutable} push ${name}`]); - core.endGroup(); - } } } catch (error) { @@ -1030,7 +1020,17 @@ function run() { } }); } -run(); +// Main +if (!exports.IsPost) { + // Publish a variable so that when the POST action runs, it can determine it should run the cleanup logic. + // This is necessary since we don't have a separate entry point. + coreCommand.issueCommand('save-state', { name: 'isPost' }, 'true'); + setup(); +} +else { + // Post + upload(); +} /***/ }), @@ -1125,26 +1125,6 @@ function escapeProperty(s) { } //# sourceMappingURL=command.js.map -/***/ }), - -/***/ 443: -/***/ (function(__unusedmodule, exports) { - -"use strict"; - -Object.defineProperty(exports, "__esModule", { value: true }); -function prependEach(elem, array) { - const init = []; - return array.reduce((r, a) => r.concat(elem, a), init); -} -exports.prependEach = prependEach; -; -function nonEmptySplit(str, separator) { - return str.split(separator).filter(word => word != ""); -} -exports.nonEmptySplit = nonEmptySplit; - - /***/ }), /***/ 470: diff --git a/package.json b/package.json index 4b2d3cf7..1e978369 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "main": "dist/main/index.js", "scripts": { "build": "tsc && ncc build -o dist/main src/main.ts", - "test": "jest" + "test": "echo NO TEST" }, "repository": { "type": "git", diff --git a/src/main.ts b/src/main.ts index 3f9587dc..57fd5fe7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,21 +1,18 @@ import * as core from '@actions/core'; +import * as coreCommand from '@actions/core/lib/command' import * as exec from '@actions/exec'; -import { prependEach, nonEmptySplit } from './strings'; -import { exit } from 'process'; +export const IsPost = !!process.env['STATE_isPost'] -async function run() { - try { - // inputs - const name = core.getInput('name', { required: true }); - const file = core.getInput('file'); - const skipNixBuild = core.getInput('skipNixBuild'); - const attributes = core.getInput('attributes'); - const nixBuildArgs = core.getInput('nixBuildArgs'); - const signingKey = core.getInput('signingKey'); - const authToken = core.getInput('authToken') - const cachixExecutable = "/nix/var/nix/profiles/per-user/runner/profile/bin/cachix"; +// inputs +const name = core.getInput('name', { required: true }); +const signingKey = core.getInput('signingKey'); +const authToken = core.getInput('authToken') +const skipPush = core.getInput('skipPush'); +const cachixExecutable = '/nix/var/nix/profiles/per-user/runner/profile/bin/cachix'; +async function setup() { + try { core.startGroup('Cachix: installing') await exec.exec('nix-env', ['--quiet', '-iA', 'cachix', '-f', 'https://cachix.org/api/v1/install']); core.endGroup() @@ -31,36 +28,21 @@ async function run() { if (signingKey !== "") { core.exportVariable('CACHIX_SIGNING_KEY', signingKey); + // Remember existing store paths + await exec.exec("sh", ["-c", `nix path-info --all | grep -v '\.drv$' > /tmp/store-path-pre-build`]); } + } catch (error) { + core.setFailed(`Action failed with error: ${error}`); + throw (error); + } +} - if (skipNixBuild !== 'true') { - const args = prependEach('-A', nonEmptySplit(attributes, /\s+/)).concat([file || "default.nix"]); - const additionalArgs = nonEmptySplit(nixBuildArgs, /\s+/); - const allArgs = additionalArgs.concat(args); - - core.startGroup(`nix-build ${allArgs.join(' ')}`); - - if (signingKey !== "") { - // Remember existing store paths - await exec.exec("sh", ["-c", `nix path-info --all | grep -v '\.drv$' > store-path-pre-build`]); - } - - let paths = ''; - const options = { - listeners: { - stdout: (data: Buffer) => { - paths += data.toString(); - }, - } - }; - await exec.exec('nix-build', allArgs, options); - core.endGroup() - - if (signingKey !== "") { - core.startGroup('Cachix: pushing paths'); - await exec.exec("sh", ["-c", `nix path-info --all | grep -v '\.drv$' | cat - store-path-pre-build | sort | uniq -u | ${cachixExecutable} push ${name}`]); - core.endGroup(); - } +async function upload() { + try { + if (signingKey !== "" && skipPush !== 'true') { + core.startGroup('Cachix: pushing paths'); + await exec.exec("sh", ["-c", `nix path-info --all | grep -v '\.drv$' | cat - /tmp/store-path-pre-build | sort | uniq -u | ${cachixExecutable} push ${name}`]); + core.endGroup(); } } catch (error) { core.setFailed(`Action failed with error: ${error}`); @@ -68,4 +50,13 @@ async function run() { } } -run(); +// Main +if (!IsPost) { + // Publish a variable so that when the POST action runs, it can determine it should run the cleanup logic. + // This is necessary since we don't have a separate entry point. + coreCommand.issueCommand('save-state', {name: 'isPost'}, 'true') + setup() +} else { + // Post + upload() +} diff --git a/src/strings.ts b/src/strings.ts deleted file mode 100644 index fc6eb0c3..00000000 --- a/src/strings.ts +++ /dev/null @@ -1,8 +0,0 @@ -export function prependEach (elem: string, array: string[]): string[] { - const init: string[] = []; - return array.reduce((r, a) => r.concat(elem, a), init) -}; - -export function nonEmptySplit (str: string, separator): string[] { - return str.split(separator).filter(word => word != "") -} \ No newline at end of file