-
Notifications
You must be signed in to change notification settings - Fork 3
/
y-build-like-sync
executable file
·61 lines (51 loc) · 2.47 KB
/
y-build-like-sync
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
#!/usr/bin/env bash
[ -z "$DEBUG" ] || set -x
set -eo pipefail
# Bash isn't a good tool for producing a tar from a build context
# but this script is an experiment on how to compose an image
# from a runtime and some static files using go-containerregistry's crane
# The user shouldn't need to write Dockerfile or .dockerignore,
# but we could probably use a generated Dockerfile + kaniko instead, or Buildpacks
# Settings
DEFAULT_REGISTRY=builds-registry.ystack.svc.cluster.local
[ -z "$BUILDS_REGISTRY" ] && BUILDS_REGISTRY=$DEFAULT_REGISTRY
[ -z "$PUSH_REGISTRY" ] && PUSH_REGISTRY=$DEFAULT_REGISTRY
if [ "$(curl -s --connect-timeout 3 http://$BUILDS_REGISTRY/v2/)" != "{}" ]
then
echo "ERROR Skaffold need local access to the builds registry for digest lookup"
echo "Registry: $BUILDS_REGISTRY"
echo "Look for y-stack's ingress or port-forward utilities"
exit 1
fi
[ -z "$IMAGE" ] && echo "No IMAGE env (from for example Skaffold)" && exit 1
IMAGE=$IMAGE
case "$IMAGE" in
$BUILDS_REGISTRY/* ) ;;
$BUILDS_REGISTRY:80/* ) ;;
$PUSH_REGISTRY/* ) echo "Unlike y-build this script won't push to non-build registries. Got: $IMAGE" && exit 1 ;;
* ) echo "Output is restricted to PUSH_REGISTRY=$PUSH_REGISTRY. Got: $IMAGE" && exit 1 ;;
esac
RUNTIME_IMAGE=$1
[ -z "$RUNTIME_IMAGE" ] && echo "First argument must be a runtime image to append the layer to" \
&& echo "To improve build times use a runtime image in the target repo" && exit 1
# crane hangs for a long time if it doesn't know that the registry is plain http
RUNTIME_IMAGE=$(echo $RUNTIME_IMAGE | sed 's|.local/|.local:80/|')
IMAGE=$(echo $IMAGE | sed 's|.local/|.local:80/|')
# This is a PoC, let's make a lot of assumptions to simplify
context=.
src='**'
# assuming a single manual sync, which is the reasonable use case for a runtime
[ ! -f skaffold.yaml ] && echo "This composition example assumes a sync defined in a skaffold.yaml" && exit 1
dest=$(cat skaffold.yaml | grep 'dest:' | awk '{ print $2 }')
# this avoids "tar: Removing leading `/' from member names" and could come in handy if we can't use --transform
dest=$(echo $dest | sed 's|^/||')
list=$(mktemp)
# Wanted: a way to produce a build context like buildctl would
(cd $context; git ls-files -c -o --exclude-standard -- . || find . -type f) > $list
tar=$(mktemp)
tar --transform "s|^|$dest/|" --show-transformed-names -cvhf $tar -T $list --mode='ug+rw' --group=65534 --owner=65532
rm $list
set -x
y-crane append --insecure -b $RUNTIME_IMAGE -f $tar -t $IMAGE
set +x
rm $tar