Repository navigation
Expand file tree
/
Copy pathaction.yml
More file actions
92 lines (87 loc) · 3.11 KB
/
Copy pathaction.yml
File metadata and controls
92 lines (87 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
name: 'Setup ko'
description: 'Install and authorize ko'
branding:
icon: box
color: green
inputs:
version:
description: 'Version of ko to install (tip, latest-release, v0.18.1, etc.)'
required: true
default: 'latest-release'
use-sudo:
description: 'set to true if install-dir location requires sudo privs'
required: false
default: 'false'
runs:
using: "composite"
steps:
- shell: bash
env:
KO_VERSION: ${{ inputs.version }}
USE_SUDO: ${{ inputs.use-sudo }}
GH_TOKEN: ${{ github.token }}
RUNNER_OS: ${{ runner.os }}
RUNNER_ARCH: ${{ runner.arch }}
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
set -ex
SUDO=
if [[ "${USE_SUDO}" == "true" ]] && command -v sudo >/dev/null; then
SUDO=sudo
fi
# Install ko:
# - if version is "tip", install from tip of main.
# - if version is "latest-release", look up latest release.
# - otherwise, install the specified version.
case ${KO_VERSION} in
tip)
echo "Installing ko using go install"
go install github.com/google/ko@main
;;
latest-release)
tag=$(curl -L -s -u "username:${GH_TOKEN}" https://api.github.com/repos/ko-build/ko/releases/latest | jq -r '.tag_name')
;;
*)
tag="${KO_VERSION}"
esac
os="${RUNNER_OS}"
arch=$(echo "${RUNNER_ARCH}" | tr '[:upper:]' '[:lower:]')
if [[ $os == "macOS" ]]; then
os="Darwin"
fi
if [[ $arch == "x64" ]]; then
arch="x86_64"
fi
if [[ ! -z ${tag} ]]; then
echo "Installing ko @ ${tag} for ${os} ${arch}"
archive=$(mktemp)
trap 'rm -f "$archive"' EXIT
url="https://github.com/ko-build/ko/releases/download/${tag}/ko_${tag:1}_${os}_${arch}.tar.gz"
for attempt in 1 2 3 4 5; do
# Validate the whole archive before extracting it. A successful
# download can still contain a truncated tarball.
if curl -fsL "$url" -o "$archive" && tar tzf "$archive" ko >/dev/null; then
$SUDO tar xzf "$archive" -C /usr/local/bin ko
break
fi
if [[ "$attempt" -eq 5 ]]; then
echo "Failed to download ko after $attempt attempts" >&2
exit 1
fi
sleep "$((5 * attempt))"
done
fi
if [[ ! -z ${KO_DOCKER_REPO} ]]; then
echo "KO_DOCKER_REPO is already set"
echo "Skipping login to ghcr.io and passing KO_DOCKER_REPO=${KO_DOCKER_REPO} on to future steps"
echo "KO_DOCKER_REPO=${KO_DOCKER_REPO}" >> $GITHUB_ENV
else
# NB: username doesn't seem to matter.
echo "${GH_TOKEN}" | ko login ghcr.io --username "dummy" --password-stdin
# Set KO_DOCKER_REPO for future steps.
# We need to get the repository name in lowercase, otherwise it could fail
repo=$(printf '%s' "${GITHUB_REPOSITORY}" | tr -d '\n\r' | tr '[:upper:]' '[:lower:]')
ko_docker_repo=$(printf 'ghcr.io/%s' "${repo}")
echo "KO_DOCKER_REPO=${ko_docker_repo}"
echo "KO_DOCKER_REPO=${ko_docker_repo}" >> "${GITHUB_ENV}"
fi