Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid composite action #4

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ Thanks to [Alexander Pravdin](https://github.com/speller) for the basic idea in
v2.x unifies `reproducible-containers/buildkit-cache-dance/inject` and `reproducible-containers/buildkit-cache-dance/extract`
into a single `reproducible-containers/buildkit-cache-dance` action.

However, v2.x seems unstable: [`[v2] "post" steps are executed in a random order`](https://github.com/reproducible-containers/buildkit-cache-dance/issues/1)
v2.x is still experimental.
48 changes: 3 additions & 45 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,6 @@ inputs:
default: scratch
description: "Where the action is stores some temporary files for its processing. Default: `scratch`"
runs:
using: composite
steps:
- uses: pyTooling/Actions/with-post-step@adef08d3bdef092282614f3b683897cefae82ee3 # v0.4.6
with:
main: |-
set -eux
# Clean Directories
rm -Rf ${{ inputs.scratch-dir }} && mkdir -p ${{ inputs.scratch-dir }} ${{ inputs.cache-source }}
# Prepare Timestamp for Layer Cache Busting
date --iso=ns | tee ${{ inputs.cache-source }}/buildstamp
# Prepare Dancefile to Access Caches
cat > ${{ inputs.scratch-dir }}/Dancefile.inject <<EOF
FROM busybox:1
COPY buildstamp buildstamp
RUN --mount=type=cache,target=${{ inputs.cache-target }} \
--mount=type=bind,source=.,target=/var/dance-cache \
cp -p -R /var/dance-cache/. ${{ inputs.cache-target }} || true
EOF
cat ${{ inputs.scratch-dir }}/Dancefile.inject
# Inject Data into Docker Cache
docker buildx build -f ${{ inputs.scratch-dir }}/Dancefile.inject --tag dance:inject ${{ inputs.cache-source }}
# Clean Directories
sudo rm -rf ${{ inputs.cache-source }}
post: |-
set -eux
# Prepare Timestamp for Layer Cache Busting
date --iso=ns | tee ${{ inputs.scratch-dir }}/buildstamp
# Prepare Dancefile to Access Caches
cat > ${{ inputs.scratch-dir }}/Dancefile.extract <<EOF
FROM busybox:1
COPY buildstamp buildstamp
RUN --mount=type=cache,target=${{ inputs.cache-target }} \
mkdir -p /var/dance-cache/ \
&& cp -p -R ${{ inputs.cache-target }}/. /var/dance-cache/ || true
EOF
cat ${{ inputs.scratch-dir }}/Dancefile.extract
# Extract Data into Docker Image
docker buildx build -f ${{ inputs.scratch-dir }}/Dancefile.extract --tag dance:extract --load ${{ inputs.scratch-dir }}
# Create Extraction Image
docker rm -f cache-container && docker create -ti --name cache-container dance:extract
# Unpack Docker Image into Scratch
docker cp -L cache-container:/var/dance-cache - | tar -H posix -x -C ${{ inputs.scratch-dir }}
# Move Cache into Its Place
sudo rm -rf ${{ inputs.cache-source }}
mv ${{ inputs.scratch-dir }}/dance-cache ${{ inputs.cache-source }}
using: 'node16'
main: 'entrypoint.js'
post: 'entrypoint.js'
49 changes: 49 additions & 0 deletions entrypoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Forked from https://github.com/pyTooling/Actions/blob/v0.4.6/with-post-step/main.js
// The following copyright header is from the upstream.

/* ================================================================================================================== *
* Authors: *
* Unai Martinez-Corral *
* *
* ================================================================================================================== *
* Copyright 2021-2022 Unai Martinez-Corral <[email protected]> *
* Copyright 2022 Unai Martinez-Corral <[email protected]> *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
* SPDX-License-Identifier: Apache-2.0 *
* ================================================================================================================== *
* *
* Context: *
* * https://github.com/docker/login-action/issues/72 *
* * https://github.com/actions/runner/issues/1478 *
* ================================================================================================================== */
const { spawn } = require("child_process");
const { appendFileSync } = require("fs");
const { EOL } = require("os");

function run(cmd) {
const subprocess = spawn(cmd, { stdio: "inherit", shell: false });
subprocess.on("exit", (exitCode) => {
process.exitCode = exitCode;
});
}

const key = "POST"

if ( process.env[`STATE_${key}`] !== undefined ) { // Are we in the 'post' step?
run("./post");
} else { // Otherwise, this is the main step
appendFileSync(process.env.GITHUB_STATE, `${key}=true${EOL}`);
run("./main");
}
20 changes: 20 additions & 0 deletions main
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
set -eux -o pipefail
: "Argv0: $0"
: "Clean Directories"
rm -Rf "$(./read-action-input scratch-dir)" && mkdir -p "$(./read-action-input scratch-dir)" "$(./read-action-input cache-source)"
: "Prepare Timestamp for Layer Cache Busting"
date --iso=ns | tee "$(./read-action-input cache-source)"/buildstamp
: "Prepare Dancefile to Access Caches"
cat >"$(./read-action-input scratch-dir)"/Dancefile.inject <<EOF
FROM busybox:1
COPY buildstamp buildstamp
RUN --mount=type=cache,target="$(./read-action-input cache-target)" \
--mount=type=bind,source=.,target=/var/dance-cache \
cp -p -R /var/dance-cache/. "$(./read-action-input cache-target)" || true
EOF
cat "$(./read-action-input scratch-dir)"/Dancefile.inject
: "Inject Data into Docker Cache"
docker buildx build -f "$(./read-action-input scratch-dir)"/Dancefile.inject --tag dance:inject "$(./read-action-input cache-source)"
: "Clean Directories"
sudo rm -rf "$(./read-action-input cache-source)"
23 changes: 23 additions & 0 deletions post
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
set -eux -o pipefail
: "Argv0: $0"
: "Prepare Timestamp for Layer Cache Busting"
date --iso=ns | tee "$(./read-action-input scratch-dir)"/buildstamp
: "Prepare Dancefile to Access Caches"
cat >"$(./read-action-input scratch-dir)"/Dancefile.extract <<EOF
FROM busybox:1
COPY buildstamp buildstamp
RUN --mount=type=cache,target="$(./read-action-input cache-target)" \
mkdir -p /var/dance-cache/ \
&& cp -p -R "$(./read-action-input cache-target)"/. /var/dance-cache/ || true
EOF
cat "$(./read-action-input scratch-dir)"/Dancefile.extract
: "Extract Data into Docker Image"
docker buildx build -f "$(./read-action-input scratch-dir)"/Dancefile.extract --tag dance:extract --load "$(./read-action-input scratch-dir)"
: "Create Extraction Image"
docker rm -f cache-container && docker create -ti --name cache-container dance:extract
: "Unpack Docker Image into Scratch"
docker cp -L cache-container:/var/dance-cache - | tar -H posix -x -C "$(./read-action-input scratch-dir)"
: "Move Cache into Its Place"
sudo rm -rf "$(./read-action-input cache-source)"
mv "$(./read-action-input scratch-dir)"/dance-cache "$(./read-action-input cache-source)"
13 changes: 13 additions & 0 deletions read-action-input
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env node
// helper script for printing kebab-cased env vars.
// e.g., "foo-bar" -> "INPUT_FOO-BAR".
//
// This does not need to be implemented in nodejs,
// but reimplementing this in sh is hard.

const arg = process.argv[2]; // Equates to "$1" in sh
const k = "INPUT_" + arg.toUpperCase();
const v = process.env[k];
if ( v !== undefined ) {
console.log(v);
}