Skip to content

Commit

Permalink
Merge pull request #2 from DeterminateSystems/attic-v2
Browse files Browse the repository at this point in the history
Add support for pushing to attic
  • Loading branch information
edolstra authored Dec 15, 2023
2 parents a04e627 + efc9e3b commit 8a8688f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 33 deletions.
15 changes: 15 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ branding:
color: "purple"
description: "Free, no-configuration Nix cache. Cut CI time by 50% or more by caching to GitHub Actions' cache."
inputs:
use-gha-cache:
description: "Whether to upload build results to the GitHub Actions cache."
default: true
required: false
listen:
description: The host and port to listen on.
default: 127.0.0.1:37515
Expand All @@ -19,6 +23,7 @@ inputs:
source-pr:
description: The PR of `magic-nix-cache` to use. Conflicts with all other `source-*` options.
required: false
default: 1
source-revision:
description: The revision of `nix-magic-nix-cache` to use. Conflicts with all other `source-*` options.
required: false
Expand All @@ -31,6 +36,16 @@ inputs:
diagnostic-endpoint:
description: "Diagnostic endpoint url where diagnostics and performance data is sent. To disable set this to an empty string."
default: "https://install.determinate.systems/magic-nix-cache/perf"
use-flakehub:
description: "Whether to upload build results to FlakeHub."
default: true
required: false
flakehub-cache-server:
description: "The FlakeHub binary cache server."
default: "https://attic-test.fly.dev"
flakehub-api-server:
description: "The FlakeHub API server."
default: "https://api.flakehub.com"

runs:
using: "node16"
Expand Down
Binary file modified bun.lockb
Binary file not shown.
38 changes: 22 additions & 16 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 24 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as fs from 'node:fs/promises';
import * as os from 'node:os';
import * as path from 'node:path';
import { spawn } from 'node:child_process';
import { createWriteStream, openSync, writeSync, close } from 'node:fs';
import { createWriteStream, openSync } from 'node:fs';
import { pipeline } from 'node:stream/promises';
import { setTimeout } from 'timers/promises';
import { inspect } from 'node:util';
Expand Down Expand Up @@ -34,7 +34,7 @@ function getCacherUrl() : string {
const runnerArch = process.env.RUNNER_ARCH;
const runnerOs = process.env.RUNNER_OS;
const binarySuffix = `${runnerArch}-${runnerOs}`;
const urlPrefix = `https://install.determinate.systems/magic-nix-cache`;
const urlPrefix = `https://magic-nix-cache-priv20231208150408868500000001.s3.us-east-2.amazonaws.com`;

if (core.getInput('source-url')) {
return core.getInput('source-url');
Expand All @@ -45,7 +45,7 @@ function getCacherUrl() : string {
}

if (core.getInput('source-pr')) {
return `${urlPrefix}/pr/${core.getInput('source-pr')}/${binarySuffix}`;
return `${urlPrefix}/pr_${core.getInput('source-pr')}/magic-nix-cache-${binarySuffix}`;
}

if (core.getInput('source-branch')) {
Expand All @@ -66,7 +66,7 @@ async function fetchAutoCacher(destination: string) {
});

const binary_url = getCacherUrl();
core.debug(`Fetching the Magic Nix Cache from ${binary_url}`);
core.info(`Fetching the Magic Nix Cache from ${binary_url}`);

return pipeline(
gotClient.stream(binary_url),
Expand Down Expand Up @@ -113,40 +113,47 @@ async function setUpAutoCache() {
runEnv = process.env;
}

const output = openSync(`${daemonDir}/parent.log`, 'a');
const outputPath = `${daemonDir}/parent.log`;
const output = openSync(outputPath, 'a');
const launch = spawn(
daemonBin,
[
'--daemon-dir', daemonDir,
'--listen', core.getInput('listen'),
'--upstream', core.getInput('upstream-cache'),
'--diagnostic-endpoint', core.getInput('diagnostic-endpoint')
],
'--diagnostic-endpoint', core.getInput('diagnostic-endpoint'),
'--nix-conf', `${process.env["HOME"]}/.config/nix/nix.conf`
].concat(
core.getInput('use-flakehub') === 'true' ? [
'--use-flakehub',
'--flakehub-cache-server', core.getInput('flakehub-cache-server'),
'--flakehub-api-server', core.getInput('flakehub-api-server'),
'--flakehub-api-server-netrc', path.join(process.env['RUNNER_TEMP'], 'determinate-nix-installer-netrc'),
] : []).concat(
core.getInput('use-gha-cache') === 'true' ? [
'--use-gha-cache'
] : []),
{
stdio: ['ignore', output, output],
env: runEnv
}
);

await new Promise<void>((resolve, reject) => {
launch.on('exit', (code, signal) => {
launch.on('exit', async (code, signal) => {
const log: string = await fs.readFile(outputPath, 'utf-8');
console.log(log);
if (signal) {
reject(new Error(`Daemon was killed by signal ${signal}`));
reject(new Error(`Daemon was killed by signal ${signal}: ${log}`));
} else if (code) {
reject(new Error(`Daemon exited with code ${code}`));
reject(new Error(`Daemon exited with code ${code}: ${log}`));
} else {
resolve();
}
});
});

await fs.mkdir(`${process.env["HOME"]}/.config/nix`, { recursive: true });
const nixConf = openSync(`${process.env["HOME"]}/.config/nix/nix.conf`, 'a');
writeSync(nixConf, `${"\n"}extra-substituters = http://${core.getInput('listen')}/?trusted=1&compression=zstd&parallel-compression=true${"\n"}`);
writeSync(nixConf, `fallback = true${"\n"}`);
close(nixConf);

core.debug('Launched Magic Nix Cache');
core.info('Launched Magic Nix Cache');
core.exportVariable(ENV_CACHE_DAEMONDIR, daemonDir);
}

Expand Down

0 comments on commit 8a8688f

Please sign in to comment.