2024-04-26 16:37:43 +02:00
import * as actionsCore from "@actions/core" ;
import * as fs from "node:fs/promises" ;
import * as os from "node:os" ;
import path from "node:path" ;
import { Tail } from "tail" ;
export function tailLog ( daemonDir : string ) : Tail {
const log = new Tail ( path . join ( daemonDir , "daemon.log" ) ) ;
actionsCore . debug ( ` tailing daemon.log... ` ) ;
log . on ( "line" , ( line ) = > {
actionsCore . info ( line ) ;
} ) ;
return log ;
}
export async function netrcPath ( ) : Promise < string > {
const expectedNetrcPath = path . join (
2024-06-05 18:54:12 +02:00
process . env [ "RUNNER_TEMP" ] ? ? os . tmpdir ( ) ,
2024-04-26 16:37:43 +02:00
"determinate-nix-installer-netrc" ,
) ;
try {
await fs . access ( expectedNetrcPath ) ;
return expectedNetrcPath ;
} catch {
// `nix-installer` was not used, the user may be registered with FlakeHub though.
const destinedNetrcPath = path . join (
2024-06-05 18:54:12 +02:00
process . env [ "RUNNER_TEMP" ] ? ? os . tmpdir ( ) ,
2024-04-26 16:37:43 +02:00
"magic-nix-cache-netrc" ,
) ;
try {
await flakeHubLogin ( destinedNetrcPath ) ;
} catch ( e ) {
2024-07-16 02:38:01 +02:00
actionsCore . info (
"FlakeHub Cache is disabled due to missing or invalid token" ,
) ;
actionsCore . info (
` If you're signed up for FlakeHub Cache, make sure that your Actions config has a \` permissions \` block with \` id-token \` set to "write" and \` contents \` set to "read" ` ,
) ;
2024-04-26 16:37:43 +02:00
actionsCore . debug ( ` Error while logging into FlakeHub: ${ e } ` ) ;
}
return destinedNetrcPath ;
}
}
async function flakeHubLogin ( netrc : string ) : Promise < void > {
const jwt = await actionsCore . getIDToken ( "api.flakehub.com" ) ;
await fs . writeFile (
netrc ,
[
` machine api.flakehub.com login flakehub password ${ jwt } ` ,
` machine flakehub.com login flakehub password ${ jwt } ` ,
` machine cache.flakehub.com login flakehub password ${ jwt } ` ,
] . join ( "\n" ) ,
) ;
actionsCore . info ( "Logged in to FlakeHub." ) ;
}