dagger-for-github/action.yml
Kyle Penfound 4e0ad19142
Proper support for shell input (#189)
* add options for shell

Signed-off-by: kpenfound <kyle@dagger.io>

assemble dagger command in new step with output

Signed-off-by: kpenfound <kyle@dagger.io>

fixing some inline bash

Signed-off-by: kpenfound <kyle@dagger.io>

wrap conditional inputs in quotes

Signed-off-by: kpenfound <kyle@dagger.io>

use cat for heredoc script file

Signed-off-by: kpenfound <kyle@dagger.io>

missing space in heredoc

Signed-off-by: kpenfound <kyle@dagger.io>

add tab ignore and dagger shebang

Signed-off-by: kpenfound <kyle@dagger.io>

use toJson and jq to safely write the shell input to a file

Signed-off-by: kpenfound <kyle@dagger.io>

put DAGGER_COMMAND in a variable

Signed-off-by: kpenfound <kyle@dagger.io>

missing semicolon for the dagger command

Signed-off-by: kpenfound <kyle@dagger.io>

safely evaluate if shell is set

Signed-off-by: kpenfound <kyle@dagger.io>

extra > in shell file write

Signed-off-by: kpenfound <kyle@dagger.io>

add some logging

Signed-off-by: kpenfound <kyle@dagger.io>

strip extra newline from jq when shell is not set

Signed-off-by: kpenfound <kyle@dagger.io>

debugging

Signed-off-by: kpenfound <kyle@dagger.io>

toJSON and jq will send null instead of empty string with no input

Signed-off-by: kpenfound <kyle@dagger.io>

safely handle all inputs

Signed-off-by: kpenfound <kyle@dagger.io>

did i fix the wrong thing?

Signed-off-by: kpenfound <kyle@dagger.io>

change up the approach a bit for an easier diff

Signed-off-by: kpenfound <kyle@dagger.io>

fix shell test

Signed-off-by: kpenfound <kyle@dagger.io>

single quotes around shell input

Signed-off-by: kpenfound <kyle@dagger.io>

try with piped input

Signed-off-by: kpenfound <kyle@dagger.io>

try single quotes again

Signed-off-by: kpenfound <kyle@dagger.io>

debug

Signed-off-by: kpenfound <kyle@dagger.io>

debug

Signed-off-by: kpenfound <kyle@dagger.io>

fix shell test

Signed-off-by: kpenfound <kyle@dagger.io>

does shell emit a newline?

Signed-off-by: kpenfound <kyle@dagger.io>

remove the trailing newline if its in stdout

Signed-off-by: kpenfound <kyle@dagger.io>

break the trailing output test

Signed-off-by: kpenfound <kyle@dagger.io>

fix shell test

Signed-off-by: kpenfound <kyle@dagger.io>

missed !

Signed-off-by: kpenfound <kyle@dagger.io>

add comment about weird test

Signed-off-by: kpenfound <kyle@dagger.io>

* use echo -n in shell test

Signed-off-by: kpenfound <kyle@dagger.io>

---------

Signed-off-by: kpenfound <kyle@dagger.io>
2025-09-16 13:19:53 -04:00

138 lines
4.4 KiB
YAML

name: "Dagger for GitHub"
description: "Run dagger commands in Github Actions"
inputs:
version:
description: "Dagger Version"
required: true
commit:
description: "Dagger Dev Commit"
required: false
default: ""
dagger-flags:
description: "Dagger CLI Flags"
required: false
default: "--progress plain"
verb:
description: "CLI verb (call, run, download, up, functions, shell, query)"
required: false
default: "call"
workdir:
description: "The working directory in which to run the Dagger CLI"
required: false
default: "."
cloud-token:
description: "Dagger Cloud Token"
required: false
default: ""
module:
description: "Dagger module to call. Local or Git"
required: false
default: ""
args:
description: "Arguments to pass to CLI"
required: false
default: ""
engine-stop:
description: "Whether to stop the Dagger Engine after this run"
required: false
default: "true"
call:
description: "Function and arguments for dagger call"
required: false
default: ""
shell:
description: "Function and arguments for dagger shell"
required: false
default: ""
outputs:
output:
description: "Job output"
value: ${{ steps.exec.outputs.stdout }}
traceURL:
description: "Dagger Cloud trace URL"
value: ${{ steps.exec.outputs.traceURL }}
runs:
using: "composite"
steps:
- shell: bash
run: |
set -o pipefail
# Fallback to /usr/local for backwards compatability
prefix_dir="${RUNNER_TEMP:-/usr/local}"
# Ensure the dir is writable otherwise fallback to tmpdir
if [[ ! -d "$prefix_dir" ]] || [[ ! -w "$prefix_dir" ]]; then
prefix_dir="$(mktemp -d)"
fi
printf '%s/bin' "$prefix_dir" >> $GITHUB_PATH
# If the dagger version is 'latest', set the version back to an empty
# string. This allows the install script to detect and install the latest
# version itself
VERSION=${{ inputs.version }}
if [[ "$VERSION" == "latest" ]]; then
VERSION=
fi
COMMIT=${{ inputs.commit }}
# The install.sh script creates path ${prefix_dir}/bin
curl -fsS https://dl.dagger.io/dagger/install.sh \
| BIN_DIR=${prefix_dir}/bin DAGGER_VERSION="$VERSION" DAGGER_COMMIT="$COMMIT" sh
- id: assemble
if: inputs.call != '' || inputs.shell != '' || inputs.args != ''
shell: bash
env:
INPUT_MODULE: ${{ inputs.module }}
run: |
verb=${{ inputs.verb }}
shell=$(echo '${{ toJSON(inputs.shell) }}' | jq -rj .)
if [[ -n "${{ inputs.call }}" ]]; then
verb="call"
elif [[ "$shell" != "" ]]; then
verb=""
script=$(mktemp)
printf '%s' "$shell" > $script
fi
echo "script=$script" >> "$GITHUB_OUTPUT"
echo "verb=$verb" >> "$GITHUB_OUTPUT"
- id: exec
if: inputs.call != '' || inputs.shell != '' || inputs.args != ''
shell: bash
env:
INPUT_MODULE: ${{ inputs.module }}
run: |
tmpout=$(mktemp)
tmperr=$(mktemp)
cd ${{ inputs.workdir }} && { \
DAGGER_CLOUD_TOKEN=${{ inputs.cloud-token }} \
dagger \
${{ inputs.dagger-flags }} \
${{ steps.assemble.outputs.verb }} \
${INPUT_MODULE:+-m $INPUT_MODULE} \
${{ inputs.args || inputs.call || steps.assemble.outputs.script }}; } 1> >(tee "${tmpout}") 2> >(tee "${tmperr}" >&2)
{
# we need a delim that doesn't appear in the output - a hash of the
# file itself *probably* won't (if it does, we have larger
# cryptographic problems)
delim="$(sha256sum $tmpout | cut -d " " -f1)"
echo "stdout<<${delim}"
cat "${tmpout}"
echo
echo "${delim}"
} >> "$GITHUB_OUTPUT"
# Extract trace URL from stderr and set as traceURL output
trace_url=$((grep -Eo 'https://dagger.cloud(/[^ ]+/traces/[a-zA-Z0-9]+|/traces/setup)' "${tmperr}" || true) | head -n1)
if [[ -n "$trace_url" ]]; then
echo "traceURL=$trace_url" >> "$GITHUB_OUTPUT"
fi
- if: (inputs.call != '' || inputs.args != '') && inputs.engine-stop == 'true'
shell: bash
run: |
mapfile -t containers < <(docker ps --filter name="dagger-engine-*" -q)
if [[ "${#containers[@]}" -gt 0 ]]; then
docker stop -t 300 "${containers[@]}";
fi