-
Notifications
You must be signed in to change notification settings - Fork 0
/
icce-create.sh
executable file
·136 lines (111 loc) · 4.73 KB
/
icce-create.sh
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
#!/bin/bash
# Copyright © 2022 Dimitri Prosper <dimitri_prosper@us.ibm.com>
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -euo pipefail
set -o errexit
set +o errexit
ibmcloud code-engine project create --name "$CE_PROJECT_NAME" --no-select
[ $? -ne 0 ] && echo "Error during project create" \
&& ibmcloud code-engine project delete --name "$CE_PROJECT_NAME" --force --hard \
&& exit 1
ibmcloud code-engine project select --name "$CE_PROJECT_NAME"
[ $? -ne 0 ] && echo "Error during project select" && exit 1
set -o errexit
p_registry_secret=""
if [ ! -z "${REGISTRY_USER}" ] && [ ! -z "${REGISTRY_PASSWORD}" ] ; then
set +o errexit
ibmcloud code-engine registry create \
--name "$CE_REGISTRY_SECRET" \
--server "$REGISTRY" \
--username "$REGISTRY_USER" \
--password "$REGISTRY_PASSWORD"
[ $? -ne 0 ] && echo "Error during registry secret create" && exit 1
set -o errexit
p_registry_secret="--registry-secret $CE_REGISTRY_SECRET"
fi
p_env_secret=""
if [ ! -z "${APP_SECRET}" ] ; then
set +o errexit
ibmcloud code-engine secret create \
--name app-secret \
--from-literal "APP_SECRET=$APP_SECRET"
[ $? -ne 0 ] && echo "Error during app secret create" && exit 1
set -o errexit
p_env_secret="--env-from-secret app-secret"
fi
# Reads and write configmaps to Code Engine from icce-project-config.json, skips if empty.
p_mount_configmap=""
p_env_configmap=""
if [ -f .github/workflows/icce-project-config.json ]; then
if jq -e . .github/workflows/icce-project-config.json >/dev/null 2>&1; then
configmaps=$(jq -c '.configmapsfromfile[]?' .github/workflows/icce-project-config.json)
for configmap in $configmaps; do
name=$(echo ${configmap} | jq -r '.name | select (.!=null)')
file=$(echo ${configmap} | jq -r '.file | select (.!=null)')
path=$(echo ${configmap} | jq -r '.path | select (.!=null)')
set +o errexit
ibmcloud code-engine configmap create --name "$name" --from-file "$file"
[ $? -ne 0 ] && echo "Error during configmap create" && exit 1
set -o errexit
p_mount_configmap+="--mount-configmap $path=$name "
done
envs=$(jq -c '.envsfromfile[]?' .github/workflows/icce-project-config.json)
for env in $envs; do
name=$(echo ${env} | jq -r '.name | select (.!=null)')
file=$(echo ${env} | jq -r '.file | select (.!=null)')
set +o errexit
ibmcloud code-engine configmap create --name "$name" --from-env-file "$file"
[ $? -ne 0 ] && echo "Error during configmap create" && exit 1
set -o errexit
p_env_configmap+="--env-from-configmap $name "
done
else
echo "Failed to use icce-project-config.json, either the file failed json parsing or something else went wrong." && exit 1
fi
fi
set +o errexit
ibmcloud code-engine application create \
--name "$CE_APP_NAME" \
--image "$IMAGE" \
--port "$APP_PORT" \
${p_env_secret} \
${p_registry_secret} \
${p_mount_configmap} \
${p_env_configmap} \
--wait \
--wait-timeout 120
[ $? -ne 0 ] && echo "Error encountered during app create, printing events and logs from deployment." \
&& events=$(ibmcloud ce application events -n "$CE_APP_NAME") \
&& echo "::set-output name=events::$events" \
&& logs=$(ibmcloud ce application logs -n "$CE_APP_NAME") \
&& echo "::set-output name=logs::$logs" \
&& ibmcloud code-engine project delete --name "$CE_PROJECT_NAME" --force --hard \
&& exit 1
sleep 30
if [ -f .github/workflows/icce-app-config.json ]; then
if jq -e . .github/workflows/icce-app-config.json >/dev/null 2>&1; then
bindings=$(jq -c '.bindings[]?' .github/workflows/icce-app-config.json)
for binding in $bindings; do
serviceName=$(echo ${binding} | jq -r '.serviceName | select (.!=null)')
set +o errexit
ibmcloud code-engine application bind --name "$CE_APP_NAME" --service-instance "$serviceName"
[ $? -ne 0 ] && echo "Error during bind to service $serviceName" && exit 1
set -o errexit
done
else
echo "Failed to use icce-app-config.json, either the file failed json parsing or something else went wrong." && exit 1
fi
fi
app_url=$(ibmcloud code-engine application get --name "$CE_APP_NAME" --output json | jq -r '.status.url')
[ $? -ne 0 ] && echo "Error obtaining app url" && exit 1
set -o errexit
echo "::set-output name=url::$app_url"
exit 0