forked from shipwright-io/build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildstrategy_ko_cr.yaml
143 lines (129 loc) · 4.96 KB
/
buildstrategy_ko_cr.yaml
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
---
apiVersion: shipwright.io/v1alpha1
kind: ClusterBuildStrategy
metadata:
name: ko
spec:
parameters:
- name: go-flags
description: "Value for the GOFLAGS environment variable."
default: ""
- name: go-version
description: "Version of Go, must match a tag from https://hub.docker.com/_/golang?tab=tags"
default: "1.16"
- name: ko-version
description: "Version of ko, must be either 'latest', or a release name from https://github.com/google/ko/releases"
default: latest
- name: package-directory
description: "The directory inside the context directory containing the main package."
default: "."
- name: target-platform
description: "Target platform to be built. For example: 'linux/arm64'. Multiple platforms can be provided separated by comma, for example: 'linux/arm64,linux/amd64'. The value 'all' will build all platforms supported by the base image. The value 'current' will build the platform on which the build runs."
default: current
buildSteps:
- name: prepare
image: golang:$(params.go-version)
imagePullPolicy: Always
securityContext:
runAsUser: 0
capabilities:
add:
- CHOWN
command:
- chown
args:
- -R
- "1000:1000"
- /tekton/home
resources:
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 100m
memory: 128Mi
- name: build-and-push
image: golang:$(params.go-version)
imagePullPolicy: Always
workingDir: $(params.shp-source-root)
securityContext:
runAsUser: 1000
runAsGroup: 1000
env:
- name: DOCKER_CONFIG
value: /tekton/home/.docker
- name: HOME
value: /tekton/home
- name: GOFLAGS
value: $(params.go-flags)
command:
- /bin/bash
args:
- -c
- |
set -euo pipefail
# Parse image URL to extract repository and tag, must work with
# - a URL without a tag and a port: registry/image
# - a URL without a tag but a port: registry:port/image
# - a URL with a tag but without a port: registry/image:tag
# - a URL with both a tag and a port: registry:port/image:tag
IMAGE=$(params.shp-output-image)
REPO=
TAG=
IFS=':' read -ra PARTS <<< "${IMAGE}"
for PART in "${PARTS[@]}"; do
if [ "${REPO}" == "" ]; then
REPO="${PART}"
elif [[ "${PART}" == *"/"* ]]; then
REPO="${REPO}:${PART}"
elif [ "${TAG}" == "" ]; then
TAG="${PART}"
else
REPO="${REPO}:${TAG}"
TAG="${PART}"
fi
done
# Determine the ko version
KO_VERSION='$(params.ko-version)'
if [ "${KO_VERSION}" == "latest" ]; then
KO_VERSION=$(curl --silent "https://api.github.com/repos/google/ko/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
fi
# Create one variable with v-suffix and one without as we need both for the download URL
if [[ ${KO_VERSION} = v* ]]; then
KO_VERSION_WITH_V=${KO_VERSION}
KO_VERSION_WITHOUT_V=${KO_VERSION:1}
else
KO_VERSION_WITH_V=v${KO_VERSION}
KO_VERSION_WITHOUT_V=${KO_VERSION}
fi
# Download ko to the temp directory
curl -f -s -L "https://github.com/google/ko/releases/download/${KO_VERSION_WITH_V}/ko_${KO_VERSION_WITHOUT_V}_$(uname)_$(uname -m | sed 's/aarch64/arm64/').tar.gz" | tar xzf - -C /tmp ko
# Determine the platform
PLATFORM='$(params.target-platform)'
if [ "${PLATFORM}" == "current" ]; then
PLATFORM="$(uname | tr '[:upper:]' '[:lower:]')/$(uname -m | sed -e 's/x86_64/amd64/' -e 's/aarch64/arm64/')"
fi
# Print version information
go version
echo "ko version $(/tmp/ko version)"
# Run ko
export GOROOT=$(go env GOROOT)
export KO_DOCKER_REPO="${REPO}"
pushd '$(params.shp-source-context)' > /dev/null
if [ "${TAG}" == "" ]; then
/tmp/ko publish '$(params.package-directory)' --bare --oci-layout-path=/tmp/layout --platform="${PLATFORM}"
else
/tmp/ko publish '$(params.package-directory)' --bare --oci-layout-path=/tmp/layout --platform="${PLATFORM}" --tags="${TAG}"
fi
popd > /dev/null
# Store the image digest
grep digest /tmp/layout/index.json | sed -E 's/.*sha256([^"]*).*/sha256\1/' | tr -d '\n' > '$(results.shp-image-digest.path)'
# Store the image size
du -b -c /tmp/layout/blobs/sha256/* | tail -1 | sed 's/\s*total//' | tr -d '\n' > '$(results.shp-image-size.path)'
resources:
limits:
cpu: 500m
memory: 1Gi
requests:
cpu: 250m
memory: 65Mi