Skip to content

Commit

Permalink
passing crane on create-pull request action as argumnet
Browse files Browse the repository at this point in the history
adding crane on tools.sh
  • Loading branch information
pacostas committed Jun 25, 2024
2 parents 764172a + a01b70a commit a3f6d1b
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 6 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/update-tools.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ jobs:
token: ${{ secrets.PAKETO_BOT_GITHUB_TOKEN }}
repo: anchore/syft

- name: Fetch Latest crane
id: latest-crane
uses: paketo-buildpacks/github-config/actions/tools/latest@main
with:
token: ${{ secrets.PAKETO_BOT_GITHUB_TOKEN }}
repo: google/go-containerregistry

- name: Update builder tools.json
env:
JAM_VERSION: ${{ steps.latest-jam.outputs.version }}
Expand Down Expand Up @@ -90,13 +97,14 @@ jobs:
JAM_VERSION: ${{ steps.latest-jam.outputs.version }}
PACK_VERSION: ${{ steps.latest-pack.outputs.version }}
SYFT_VERSION: ${{ steps.latest-syft.outputs.version }}
CRANE_VERSION: ${{ steps.latest-crane.outputs.version }}
run: |
jq --null-input \
--sort-keys \
--arg pack "${PACK_VERSION}" \
--arg jam "${JAM_VERSION}" \
--arg syft "${SYFT_VERSION}" \
'{ pack: $pack, jam: $jam, syft: $syft }' > ./stack/scripts/.util/tools.json
'{ pack: $pack, jam: $jam, syft: $syft, crane: $crane }' > ./stack/scripts/.util/tools.json
- name: Commit
id: commit
Expand Down
7 changes: 7 additions & 0 deletions actions/tools/latest/entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ shopt -s inherit_errexit

function main() {
local token repo
token=""
repo=""

while [[ "${#}" != 0 ]]; do
case "${1}" in
Expand All @@ -17,6 +19,11 @@ function main() {
shift 2
;;

"")
# skip if the argument is empty
shift 1
;;

*)
echo "unknown argument \"${1}\""
exit 1
Expand Down
3 changes: 2 additions & 1 deletion stack/scripts/.util/tools.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"jam": "v2.7.3",
"pack": "v0.34.2",
"syft": "v1.8.0"
"syft": "v1.8.0",
"crane": "v0.19.2"
}
88 changes: 88 additions & 0 deletions stack/scripts/.util/tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ function util::tools::arch() {
amd64|x86_64)
if [[ "${1:-}" == "--blank-amd64" ]]; then
echo ""
elif [[ "${1:-}" == "--uname-format-amd64" ]]; then
echo "x86_64"
else
echo "amd64"
fi
Expand Down Expand Up @@ -251,3 +253,89 @@ function util::tools::tests::checkfocus() {
fi
rm "${testout}"
}

function util::tools::crane::install() {
local dir token
token=""

while [[ "${#}" != 0 ]]; do
case "${1}" in
--directory)
dir="${2}"
shift 2
;;

--token)
token="${2}"
shift 2
;;

*)
util::print::error "unknown argument \"${1}\""
esac
done

mkdir -p "${dir}"
util::tools::path::export "${dir}"

if [[ ! -f "${dir}/crane" ]]; then
local version curl_args os arch

version="$(jq -r .crane "$(dirname "${BASH_SOURCE[0]}")/tools.json")"

curl_args=(
"--fail"
"--silent"
"--location"
)

if [[ "${token}" != "" ]]; then
curl_args+=("--header" "Authorization: Token ${token}")
fi

util::print::title "Installing crane ${version}"

os=$(util::tools::os)
arch=$(util::tools::arch --uname-format-amd64)


curl "https://github.com/google/go-containerregistry/releases/download/${version}/go-containerregistry_Linux_${arch}.tar.gz" \
"${curl_args[@]}" | tar -C "${dir}" -xz crane

chmod +x "${dir}/crane"
fi
}


# Returns a random unused port
function get::random::port() {
local port=$(shuf -i 50000-65000 -n 1)
netstat -lat | grep $port > /dev/null
if [[ $? == 1 ]] ; then
echo $port
else
echo get::random::port
fi
}

# Starts a local registry on the given port and returns the pid
function local::registry::start() {
local registryPort registryPid localRegistry

registryPort="$1"
localRegistry="127.0.0.1:$registryPort"

# Start a local in-memory registry so we can work with oci archives
PORT=$registryPort crane registry serve --insecure > /dev/null 2>&1 &
registryPid=$!

# Stop the registry if execution is interrupted
trap "kill $registryPid" 1 2 3 6

# Wait for the registry to be available
until crane catalog $localRegistry > /dev/null 2>&1; do
sleep 1
done

echo $registryPid
}
30 changes: 26 additions & 4 deletions stack/scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set -o pipefail
readonly PROG_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly STACK_DIR="$(cd "${PROG_DIR}/.." && pwd)"
readonly OUTPUT_DIR="${STACK_DIR}/build"
readonly INTEGRATION_JSON="${STACK_DIR}/integration.json"

# shellcheck source=SCRIPTDIR/.util/tools.sh
source "${PROG_DIR}/.util/tools.sh"
Expand All @@ -14,9 +15,11 @@ source "${PROG_DIR}/.util/tools.sh"
source "${PROG_DIR}/.util/print.sh"

function main() {
local clean token
local clean token registryPort registryPid localRegistry setupLocalRegistry
clean="false"
token=""
registryPid=""
setupLocalRegistry=""

while [[ "${#}" != 0 ]]; do
case "${1}" in
Expand Down Expand Up @@ -58,7 +61,22 @@ function main() {
"${STACK_DIR}/scripts/create.sh"
fi

if [[ -f $INTEGRATION_JSON ]]; then
setupLocalRegistry=$(jq '.setup_local_registy' $INTEGRATION_JSON)
fi

if [[ "${setupLocalRegistry}" == "true" ]]; then
registryPort=$(get::random::port)
registryPid=$(local::registry::start $registryPort)
localRegistry="127.0.0.1:$registryPort"
export REGISTRY_URL="${localRegistry}"
fi

tests::run

if [[ "${setupLocalRegistry}" == "true" ]]; then
kill $registryPid
fi
}

function usage() {
Expand All @@ -72,9 +90,9 @@ ${STACK_DIR}/build/run.oci
if they exist. Otherwise, first runs create.sh to create them.
OPTIONS
--clean -c clears contents of stack output directory before running tests
--token <token> Token used to download assets from GitHub (e.g. jam, pack, etc) (optional)
--help -h prints the command usage
--clean -c clears contents of stack output directory before running tests
--token <token> -t Token used to download assets from GitHub (e.g. jam, pack, etc) (optional)
--help -h prints the command usage
USAGE
}

Expand All @@ -91,6 +109,10 @@ function tools::install() {
--token "${token}"

util::tools::skopeo::check

util::tools::crane::install \
--directory "${STACK_DIR}/.bin" \
--token "${token}"
}

function tests::run() {
Expand Down

0 comments on commit a3f6d1b

Please sign in to comment.