Skip to content

Instantly share code, notes, and snippets.

@ralphbean
Last active May 22, 2024 19:35
Show Gist options
  • Save ralphbean/dbee2755b4077caa4056a46660d2907f to your computer and use it in GitHub Desktop.
Save ralphbean/dbee2755b4077caa4056a46660d2907f to your computer and use it in GitHub Desktop.
expand-taskref.sh
#!/bin/bash -e
# Given a filename with a pipelines-as-code pipelinerun and a task name within it
# remove the bundle taskref and replace it with an inline version of the task definition.
# For hacking.
filename=$1
taskname=$2
prefix=".spec.pipelineSpec"
kind=$(yq .kind "$filename")
if [ $kind == "Pipeline" ]; then
prefix=".spec"
fi
task=$(cat "$filename" | yq "$prefix.tasks[] | select(.name == \"$taskname\")")
bundle=$(echo "$task" | yq ".taskRef.params[] | select(.name == \"bundle\") | .value")
bundle=$(echo $bundle | awk -F '@' '{ print $1 }')
repo=$(echo $bundle | awk -F ':' '{ print $1 }')
for layer in $(skopeo inspect --no-tags "docker://$bundle" | jq -r '.Layers[]'); do
spec=$(oras blob fetch --no-tty $repo@$layer -o - | tar zxf - --to-stdout | yq .spec)
done
yq -i "del($prefix.tasks[] | select(.name == \"$taskname\") | .taskRef)" $filename
yq -i "($prefix.tasks[] | select(.name == \"$taskname\") | .taskSpec) = $spec" $filename
sed -i 's/\\\\/\\/g' $filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment