Skip to content

Commit

Permalink
Skip prep steps if using pathsToPush (#180)
Browse files Browse the repository at this point in the history
* Skip prep steps if using `pathsToPush`

* ci: add a test for pushPaths

* Update test.yml

* fix

* fix

* fix
  • Loading branch information
sandydoo committed May 7, 2024
1 parent 74587ee commit 991af99
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 12 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ jobs:
useDaemon: ${{ matrix.useDaemon }}
- run: nix-build test.nix

push-paths:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- run: yarn install --frozen-lockfile
- run: yarn build
- uses: cachix/install-nix-action@v26
- id: paths
run: |
paths=$(nix-instantiate test.nix | tr '\n' ' ')
echo "OUT_PATHS=$paths" >> $GITHUB_OUTPUT
- name: Test pushPaths
uses: ./
with:
name: cachix-action
signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
pathsToPush: '${{ steps.paths.outputs.OUT_PATHS }}'

installCommand:
strategy:
matrix:
Expand Down
18 changes: 15 additions & 3 deletions dist/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7754,6 +7754,8 @@ var PushMode;
(function (PushMode) {
// Disable pushing entirely.
PushMode["None"] = "None";
// Push paths provided via the `pathsToPush` input.
PushMode["PushPaths"] = "PushPaths";
// Scans the entire store during the pre- and post-hooks and uploads the difference.
// This is a very simple method and is likely to work in any environment.
// There are two downsides:
Expand Down Expand Up @@ -7821,7 +7823,10 @@ async function setup() {
// Determine the push mode to use
let pushMode = PushMode.None;
if (hasPushTokens && !skipPush) {
if (useDaemon) {
if (pathsToPush) {
pushMode = PushMode.PushPaths;
}
else if (useDaemon) {
let supportsDaemonInterface = (cachixVersion) ? semver_1.default.gte(cachixVersion, '1.7.0') : false;
let supportsPostBuildHook = await isTrustedUser();
if (!supportsDaemonInterface) {
Expand All @@ -7846,7 +7851,7 @@ async function setup() {
'daemon', 'run',
'--socket', `${daemonDir}/daemon.sock`,
name,
...cachixArgs.split(' ').filter((arg) => arg !== ''),
...splitArgs(cachixArgs),
], {
stdio: ['ignore', daemonLog, daemonLog],
detached: true,
Expand Down Expand Up @@ -7895,6 +7900,10 @@ async function upload() {
}
break;
}
case PushMode.PushPaths: {
await exec.exec(cachixBin, ["push", ...splitArgs(cachixArgs), name, ...splitArgs(pathsToPush)]);
break;
}
case PushMode.Daemon: {
const daemonDir = process.env[ENV_CACHIX_DAEMON_DIR];
if (!daemonDir) {
Expand All @@ -7921,7 +7930,7 @@ async function upload() {
break;
}
case PushMode.StoreScan: {
await exec.exec(`${__dirname}/push-paths.sh`, [cachixBin, cachixArgs, name, pathsToPush, pushFilter]);
await exec.exec(`${__dirname}/push-paths.sh`, [cachixBin, cachixArgs, name, pushFilter]);
break;
}
}
Expand Down Expand Up @@ -8074,6 +8083,9 @@ function partitionUsersAndGroups(mixedUsers) {
});
return [users, groups];
}
function splitArgs(args) {
return args.split(' ').filter((arg) => arg !== '');
}
const isPost = !!core.getState('isPost');
// Main
try {
Expand Down
10 changes: 4 additions & 6 deletions dist/main/push-paths.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
set -euo pipefail

cachix=$1 cachixArgs=${2:--j8} cache=$3 pathsToPush=$4 pushFilter=$5
cachix=$1 cachixArgs=${2:--j8} cache=$3 pushFilter=$4

filterPaths() {
local regex=$1
Expand All @@ -12,12 +12,10 @@ filterPaths() {
done | xargs
}

if [[ -z $pathsToPush ]]; then
pathsToPush=$(comm -13 <(sort /tmp/store-path-pre-build) <("$(dirname "$0")"/list-nix-store.sh))
pathsToPush=$(comm -13 <(sort /tmp/store-path-pre-build) <("$(dirname "$0")"/list-nix-store.sh))

if [[ -n $pushFilter ]]; then
pathsToPush=$(filterPaths $pushFilter "$pathsToPush")
fi
if [[ -n $pushFilter ]]; then
pathsToPush=$(filterPaths $pushFilter "$pathsToPush")
fi

echo "$pathsToPush" | "$cachix" push $cachixArgs "$cache"
21 changes: 18 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const ENV_CACHIX_DAEMON_DIR = 'CACHIX_DAEMON_DIR';
enum PushMode {
// Disable pushing entirely.
None = 'None',
// Push paths provided via the `pathsToPush` input.
PushPaths = 'PushPaths',
// Scans the entire store during the pre- and post-hooks and uploads the difference.
// This is a very simple method and is likely to work in any environment.
// There are two downsides:
Expand Down Expand Up @@ -107,7 +109,9 @@ async function setup() {
let pushMode = PushMode.None;

if (hasPushTokens && !skipPush) {
if (useDaemon) {
if (pathsToPush) {
pushMode = PushMode.PushPaths;
} else if (useDaemon) {
let supportsDaemonInterface = (cachixVersion) ? semver.gte(cachixVersion, '1.7.0') : false;
let supportsPostBuildHook = await isTrustedUser();

Expand Down Expand Up @@ -138,7 +142,7 @@ async function setup() {
'daemon', 'run',
'--socket', `${daemonDir}/daemon.sock`,
name,
...cachixArgs.split(' ').filter((arg) => arg !== ''),
...splitArgs(cachixArgs),
],
{
stdio: ['ignore', daemonLog, daemonLog],
Expand Down Expand Up @@ -169,6 +173,7 @@ async function setup() {

break;
}

case PushMode.StoreScan: {
// Remember existing store paths
await exec.exec("sh", ["-c", `${__dirname}/list-nix-store.sh > /tmp/store-path-pre-build`]);
Expand Down Expand Up @@ -200,6 +205,12 @@ async function upload() {

break;
}

case PushMode.PushPaths: {
await exec.exec(cachixBin, ["push", ...splitArgs(cachixArgs), name, ...splitArgs(pathsToPush)]);
break;
}

case PushMode.Daemon: {
const daemonDir = process.env[ENV_CACHIX_DAEMON_DIR];

Expand Down Expand Up @@ -233,7 +244,7 @@ async function upload() {
}

case PushMode.StoreScan: {
await exec.exec(`${__dirname}/push-paths.sh`, [cachixBin, cachixArgs, name, pathsToPush, pushFilter]);
await exec.exec(`${__dirname}/push-paths.sh`, [cachixBin, cachixArgs, name, pushFilter]);
break;
}
}
Expand Down Expand Up @@ -414,6 +425,10 @@ function partitionUsersAndGroups(mixedUsers: string[]): [string[], string[]] {
return [users, groups];
}

function splitArgs(args: string): string[] {
return args.split(' ').filter((arg) => arg !== '');
}

const isPost = !!core.getState('isPost');

// Main
Expand Down

0 comments on commit 991af99

Please sign in to comment.