forked from deckhouse/deckhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules_images_werf
executable file
·138 lines (113 loc) · 4.03 KB
/
modules_images_werf
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
#!/bin/bash
set -euo pipefail
# Remove after merge !!!
export BASE_SCRATCH="registry.deckhouse.io/base_images/spotify/scratch@sha256:db4cabf15c8b9eb70dabe1da385b6d9b2cac6d658b813fbb57dc5231ddd52420"
function bp() {
# Set env DECKHOUSE_PULL_MODULES_IMAGES_BEFORE_BUILD=true to pull every image used in modules
if [[ "${DECKHOUSE_PULL_BASE_IMAGES_BEFORE_BUILD_MODULES:-}" == "true" ]]; then
pull
fi
# Always push images to "dev" repository.
REGISTRY_SUFFIX=$(echo ${WERF_ENV} | tr '[:upper:]' '[:lower:]')
werf build --config modules_images_werf.yaml --report-path images_tags_werf.json --secondary-repo ${DECKHOUSE_REGISTRY_HOST}/deckhouse/${REGISTRY_SUFFIX}
# Push images to "release" repository on git tag.
if [[ -n "${GIT_TAG_NAME}" && -n "${DECKHOUSE_REGISTRY_HOST}" ]]; then
werf build --config modules_images_werf.yaml --repo ${DECKHOUSE_REGISTRY_HOST}/deckhouse/${REGISTRY_SUFFIX} --secondary-repo ${WERF_REPO} --report-path images_tags_werf.json
fi
jq '
def to_camel_case:
. | ascii_downcase | split("-") | .[0:1] as $first | .[1:] |
map(
.[1:] as $rest | .[0:1] | ascii_upcase | . + $rest
) |
$first + . | join("")
;
.Images | to_entries | reduce .[] as $image ({};
. * {
($image.key | split("/")[0] | to_camel_case): {
($image.key | split("/")[1] | to_camel_case): $image.value.DockerTag
}
}
)' images_tags_werf.json > modules/images_tags_${WERF_ENV}.json
rm images_tags_werf.json
}
function cleanup() {
werf cleanup --config modules_images_werf.yaml --without-kube --dry-run
}
function pull() {
>&2 echo "Pulling images..."
cat $(find modules -name *Dockerfile -o -name *werf.inc.yaml) \
| grep -Eo '^(from\:|FROM)\s(\S+)$' \
| cut -d ' ' -f2 | tr -d \" \
| sort | uniq \
| xargs --no-run-if-empty -n 1 docker pull -q
}
function usage() {
cat <<"EOF"
Usage: modules_images SUBCOMMAND [SUBCOMMAND OPTIONS]...
Available subcommands:
modules_images bp build and push all modules images
modules_images cleanup run werf cleanup
modules_images pull pull all base images (get FROM images)
EOF
}
function setup_werf() {
ci_type=$1
if [[ -n ${DECKHOUSE_DEV_REGISTRY_PASSWORD:-} ]] ; then
echo ${DECKHOUSE_DEV_REGISTRY_PASSWORD} | docker login --username="${DECKHOUSE_DEV_REGISTRY_USER}" --password-stdin ${DECKHOUSE_DEV_REGISTRY_HOST} 2>/dev/null
fi
if [[ -n ${DECKHOUSE_REGISTRY_PASSWORD:-} ]] ; then
echo ${DECKHOUSE_REGISTRY_PASSWORD} | docker login --username="${DECKHOUSE_REGISTRY_USER}" --password-stdin ${DECKHOUSE_REGISTRY_HOST} 2>/dev/null
fi
if [[ -n ${DECKHOUSE_REGISTRY_READ_PASSWORD:-} ]] ; then
echo ${DECKHOUSE_REGISTRY_READ_PASSWORD} | docker login --username="${DECKHOUSE_REGISTRY_READ_USER}" --password-stdin ${DECKHOUSE_REGISTRY_READ_HOST} 2>/dev/null
fi
# Github workflow uses werf/actions/install.
if [[ $ci_type == "gitlab" ]] ; then
type multiwerf && source $(multiwerf use 1.2 ${WERF_CHANNEL:-ea} --as-file)
fi
type werf && source $(werf ci-env $ci_type --config modules_images_werf.yaml --verbose --as-file)
# There are 3 repositories, overwrite WERF_REPO to push to dev registry.
if [[ -n "${DEV_REGISTRY_PATH}" ]]; then
export WERF_REPO="${DEV_REGISTRY_PATH}"
fi
}
function main() {
if [[ $# -eq 0 ]] ; then
usage
exit 1
fi
# Check if jq is present.
type jq &>/dev/null || (echo "Please install jq (https://stedolan.github.io/jq/)"; exit 1)
ci_type=
GIT_TAG_NAME=
if [[ -n ${GITLAB_CI:-} ]] ; then
ci_type=gitlab
export GIT_TAG_NAME=${CI_COMMIT_TAG}
fi
if [[ -n $GITHUB_REPOSITORY ]] ; then
ci_type=github
gitTag=${GITHUB_REF#refs/tags/}
if [[ ${GITHUB_REF} == ${gitTag} ]] ; then
gitTag=
fi
export GIT_TAG_NAME=${gitTag}
fi
if [[ -z $ci_type ]] ; then
echo "CI type is not recognized." && exit 1
fi
setup_werf $ci_type
SUBCOMMAND=$1
shift
case "$SUBCOMMAND" in
bp )
bp $@ ;;
cleanup )
cleanup $@ ;;
pull )
pull $@ ;;
* )
usage; exit 1 ;;
esac
}
main $@