Allow skipping nix-build

This commit is contained in:
Domen Kožar 2019-12-18 15:37:40 +01:00
commit 074294984a
No known key found for this signature in database
GPG key ID: C2FFBCAFD2C24246
4 changed files with 98 additions and 48 deletions

View file

@ -56,6 +56,19 @@ jobs:
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}' authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
``` ```
Alternatively, you can use this action to only configure cachix for substitution:
```yaml
...
- uses: cachix/cachix-action@v2
with:
name: mycache
skipNixBuild: true
# Only needed for private caches
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
...
```
See [action.yml](action.yml) for all options. See [action.yml](action.yml) for all options.
--- ---

View file

@ -2,10 +2,6 @@ name: 'Cachix'
description: 'nix-build with the help of caching to Cachix' description: 'nix-build with the help of caching to Cachix'
author: 'Domen Kožar' author: 'Domen Kožar'
inputs: inputs:
file:
description: 'Nix file to build. Defaults to default.nix'
attributes:
description: 'Nix attributes to nix-build. By default, all attributes are built.'
name: name:
description: 'Name of a cachix cache to push and pull/substitute' description: 'Name of a cachix cache to push and pull/substitute'
required: true required: true
@ -13,6 +9,12 @@ inputs:
description: 'Authentication token for Cachix, needed only for private cache access' description: 'Authentication token for Cachix, needed only for private cache access'
signingKey: signingKey:
description: 'Signing key secret retrieved after creating binary cache on https://cachix.org' 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.'
branding: branding:
color: 'blue' color: 'blue'
icon: 'database' icon: 'database'

View file

@ -18,31 +18,56 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core")); const core = __importStar(require("@actions/core"));
const exec = __importStar(require("@actions/exec")); const exec = __importStar(require("@actions/exec"));
const os_1 = require("os");
const strings_1 = require("./strings"); const strings_1 = require("./strings");
function home() {
if (os_1.type() == "Darwin") {
return "/Users/runner";
}
else {
return "/home/runner";
}
}
function run() { function run() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
// inputs // inputs
const file = core.getInput('file'); const file = core.getInput('file');
const skipNixBuild = core.getInput('skipNixBuild');
const attributes = core.getInput('attributes'); const attributes = core.getInput('attributes');
const name = core.getInput('name', { required: true }); const name = core.getInput('name', { required: true });
const signingKey = core.getInput('signingKey'); const signingKey = core.getInput('signingKey');
const authToken = core.getInput('authToken'); const authToken = core.getInput('authToken');
const cachixExecutable = "/nix/var/nix/profiles/per-user/runner/profile/bin/cachix";
core.startGroup('Installing Cachix'); core.startGroup('Installing Cachix');
// TODO: use cachix official installation link yield exec.exec('nix-env', ['-iA', 'cachix', '-f', 'https://cachix.org/api/v1/install']);
yield exec.exec('nix-env', ['-iA', 'cachix', '-f', 'https://github.com/NixOS/nixpkgs/tarball/ab5863afada3c1b50fc43bf774b75ea71b287cde']);
core.endGroup(); core.endGroup();
// for private caches // for private caches
if (authToken !== "") { if (authToken !== "") {
yield exec.exec('cachix', ['authtoken', authToken]); yield exec.exec(cachixExecutable, ['authtoken', authToken]);
} }
core.startGroup(`Cachix: using ` + name); core.startGroup(`Cachix: using ` + name);
yield exec.exec('cachix', ['use', name]); yield exec.exec('cachix', ['use', name]);
core.endGroup(); core.endGroup();
if (signingKey !== "") { if (signingKey !== "") {
core.startGroup('Cachix: Configuring push');
yield exec.exec("sudo", ["sh", "-c", `echo export HOME=${home()} > /etc/nix/cachix-push.sh`]);
yield exec.exec("sudo", ["sh", "-c", `echo export CACHIX_SIGNING_KEY=${signingKey} >> /etc/nix/cachix-push.sh`]);
yield exec.exec("sudo", ["sh", "-c", `echo ${cachixExecutable} push ${name} \$OUT_PATHS >> /etc/nix/cachix-push.sh`]);
yield exec.exec("sudo", ["sh", "-c", `chmod +x /etc/nix/cachix-push.sh`]);
yield exec.exec("sudo", ["sh", "-c", `echo post-build-hook = /etc/nix/cachix-push.sh >> /etc/nix/nix.conf`]);
core.exportVariable('CACHIX_SIGNING_KEY', signingKey); core.exportVariable('CACHIX_SIGNING_KEY', signingKey);
// Reload nix-daemon
if (os_1.type() == "Darwin") {
// kickstart awaits nix-daemon to get up again
yield exec.exec("sudo", ["launchctl", "kickstart", "-k", "system/org.nixos.nix-daemon"]);
} }
// TODO: cachix use --watch-store else {
yield exec.exec("sudo", ["pkill", "-HUP", "nix-daemon"]);
}
core.endGroup();
}
if (skipNixBuild !== 'true') {
core.startGroup(`Invoking nix-build`); core.startGroup(`Invoking nix-build`);
let paths = ''; let paths = '';
const options = { const options = {
@ -55,14 +80,6 @@ function run() {
const args = strings_1.prependEach('-A', strings_1.nonEmptySplit(attributes, /\s+/)).concat([file || "default.nix"]); const args = strings_1.prependEach('-A', strings_1.nonEmptySplit(attributes, /\s+/)).concat([file || "default.nix"]);
yield exec.exec('nix-build', args, options); yield exec.exec('nix-build', args, options);
core.endGroup(); core.endGroup();
// Needed for PRs
if (signingKey !== "") {
core.startGroup(`Cachix: pushing to ` + name);
yield exec.exec('cachix', ['push', name].concat(strings_1.nonEmptySplit(paths, /\s+/)));
core.endGroup();
}
else {
console.log("No signing key. Assuming it's a pull request, nothing will be pushed.");
} }
} }
catch (error) { catch (error) {

View file

@ -1,15 +1,26 @@
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
import {type} from 'os';
import {prependEach, nonEmptySplit} from './strings'; import {prependEach, nonEmptySplit} from './strings';
function home() {
if (type() == "Darwin") {
return "/Users/runner";
} else {
return "/home/runner";
}
}
async function run() { async function run() {
try { try {
// inputs // inputs
const file = core.getInput('file'); const file = core.getInput('file');
const skipNixBuild = core.getInput('skipNixBuild');
const attributes = core.getInput('attributes'); const attributes = core.getInput('attributes');
const name = core.getInput('name', { required: true }); const name = core.getInput('name', { required: true });
const signingKey = core.getInput('signingKey'); const signingKey = core.getInput('signingKey');
const authToken = core.getInput('authToken') const authToken = core.getInput('authToken')
const cachixExecutable = "/nix/var/nix/profiles/per-user/runner/profile/bin/cachix";
core.startGroup('Installing Cachix') core.startGroup('Installing Cachix')
await exec.exec('nix-env', ['-iA', 'cachix', '-f', 'https://cachix.org/api/v1/install']); await exec.exec('nix-env', ['-iA', 'cachix', '-f', 'https://cachix.org/api/v1/install']);
@ -17,18 +28,33 @@ async function run() {
// for private caches // for private caches
if (authToken !== "") { if (authToken !== "") {
await exec.exec('cachix', ['authtoken', authToken]); await exec.exec(cachixExecutable, ['authtoken', authToken]);
} }
core.startGroup(`Cachix: using ` + name); core.startGroup(`Cachix: using ` + name);
await exec.exec('cachix', ['use', name]); await exec.exec('cachix', ['use', name]);
core.endGroup() core.endGroup();
if (signingKey !== "") { if (signingKey !== "") {
core.startGroup('Cachix: Configuring push');
await exec.exec("sudo", ["sh", "-c", `echo export HOME=${home()} > /etc/nix/cachix-push.sh`]);
await exec.exec("sudo", ["sh", "-c", `echo export CACHIX_SIGNING_KEY=${signingKey} >> /etc/nix/cachix-push.sh`]);
await exec.exec("sudo", ["sh", "-c", `echo ${cachixExecutable} push ${name} \$OUT_PATHS >> /etc/nix/cachix-push.sh`]);
await exec.exec("sudo", ["sh", "-c", `chmod +x /etc/nix/cachix-push.sh`]);
await exec.exec("sudo", ["sh", "-c", `echo post-build-hook = /etc/nix/cachix-push.sh >> /etc/nix/nix.conf`]);
core.exportVariable('CACHIX_SIGNING_KEY', signingKey); core.exportVariable('CACHIX_SIGNING_KEY', signingKey);
}
// TODO: cachix use --watch-store
// Reload nix-daemon
if (type() == "Darwin") {
// kickstart awaits nix-daemon to get up again
await exec.exec("sudo", ["launchctl", "kickstart", "-k", "system/org.nixos.nix-daemon"]);
} else {
await exec.exec("sudo", ["pkill", "-HUP", "nix-daemon"]);
}
core.endGroup();
}
if (skipNixBuild !== 'true') {
core.startGroup(`Invoking nix-build`); core.startGroup(`Invoking nix-build`);
let paths = ''; let paths = '';
const options = { const options = {
@ -41,14 +67,6 @@ async function run() {
const args = prependEach('-A', nonEmptySplit(attributes, /\s+/)).concat([file || "default.nix"]); const args = prependEach('-A', nonEmptySplit(attributes, /\s+/)).concat([file || "default.nix"]);
await exec.exec('nix-build', args, options); await exec.exec('nix-build', args, options);
core.endGroup() core.endGroup()
// Needed for PRs
if (signingKey !== "") {
core.startGroup(`Cachix: pushing to ` + name);
await exec.exec('cachix', ['push', name].concat(nonEmptySplit(paths, /\s+/)));
core.endGroup()
} else {
console.log("No signing key. Assuming it's a pull request, nothing will be pushed.");
} }
} catch (error) { } catch (error) {
core.setFailed(`Action failed with error: ${error}`); core.setFailed(`Action failed with error: ${error}`);