forked from redhat-ai-dev/ai-lab-helm-charts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application-gitops-job.yaml
132 lines (113 loc) · 5.29 KB
/
application-gitops-job.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
apiVersion: batch/v1
kind: Job
metadata:
annotations:
helm.sh/hook: post-install,post-upgrade
helm.sh/hook-delete-policy: hook-succeeded
labels:
app.kubernetes.io/instance: {{ .Release.Name }}-github-repo-creator
app.kubernetes.io/name: {{ .Release.Name }}-github-repo-creator
app.kubernetes.io/part-of: {{ .Release.Name }}
namespace: {{ .Release.Namespace }}
name: {{ .Release.Name }}-github-repo-creator
spec:
template:
spec:
containers:
- name: repo-creator
command:
- /bin/bash
- -c
- |
#!/usr/bin/env bash
set -o nounset
set -o pipefail
setup_data() {
gh repo clone "$GITHUB_SOURCE_REPO" app-source -- --no-tags --branch="$GITHUB_DEFAULT_BRANCH" --single-branch
gh repo clone "$GITHUB_TETKON_SOURCE_REPO" tekton-source -- --no-tags --single-branch
gh repo clone "$GH_REPO" "$APP_NAME" -- --no-tags --branch="$GITHUB_DEFAULT_BRANCH" --single-branch
mkdir -p "$APP_NAME"/.tekton
cd tekton-source/pac/pipelineRuns/.tekton || exit
sed -i 's@'"$TEKTON_FILE_APP_NAME_REPLACEMENT"'@'"$APP_NAME"'@g' docker-push.yaml
sed -i 's@'"$TEKTON_FILE_APP_NAMESPACE_REPLACEMENT"'@'"$APP_NAMESPACE"'@g' docker-push.yaml
sed -i 's@'"$TEKTON_FILE_QUAY_ACCOUNT_REPLACEMENT"'@'"$QUAY_ACCOUNT_NAME"'@g' docker-push.yaml
cd - || exit
cd app-source/chatbot || exit
cp * ../../"$APP_NAME"
cd ../../tekton-source/pac/pipelineRuns/.tekton || exit
cp docker-push.yaml ../../../../"$APP_NAME"/.tekton
cd ../../../../"$APP_NAME" || exit
}
cd /tmp || exit
wget https://github.com/cli/cli/releases/download/v"$GH_VERSION"/gh_"$GH_VERSION"_linux_amd64.tar.gz || exit
tar xzf gh_"$GH_VERSION"_linux_amd64.tar.gz
GH_PATH=$(pwd)/gh_"$GH_VERSION"_linux_amd64/bin
export PATH="$GH_PATH":"$PATH"
export SOURCE_REPO_APP_CONTENT_PATH="chatbot"
export SOURCE_TEKTON_REPO_CONTENT_PATH="pac/pipelineRuns"
export TEKTON_FILE_APP_NAME_REPLACEMENT="application_name_replace"
export TEKTON_FILE_APP_NAMESPACE_REPLACEMENT="application_namespace_replace"
export TEKTON_FILE_QUAY_ACCOUNT_REPLACEMENT="quay_account_replace"
echo APP_NAME is "$APP_NAME"
echo APP_NAMESPACE is "$APP_NAMESPACE"
echo GITHUB_ORG_NAME is "$GITHUB_ORG_NAME"
echo GITHUB_SOURCE_REPO is "$GITHUB_SOURCE_REPO"
echo GITHUB_TETKON_SOURCE_REPO is "$GITHUB_TETKON_SOURCE_REPO"
echo GITHUB_DEFAULT_BRANCH is "$GITHUB_DEFAULT_BRANCH"
echo QUAY_ACCOUNT_NAME is "$QUAY_ACCOUNT_NAME"
export GH_TOKEN=$GITHUB_TOKEN
export GH_REPO="github.com/$GITHUB_ORG_NAME/$APP_NAME"
export HOME=/tmp
gh auth login -p https
gh auth setup-git --hostname=github.com
gh repo view "$GH_REPO" --json id 2> /dev/null
return_code=$?
if [ $return_code == 0 ]; then
echo Repo "$GH_REPO" already exists, checking if contents are OK
setup_data
cd $HOME || exit
gh repo clone "$GH_REPO" existing -- --no-tags --branch="$GITHUB_DEFAULT_BRANCH" --single-branch || exit
FILES=("Containerfile"
"chatbot_ui.py"
"requirements.txt"
".tekton/docker-push.yaml")
for FILE in "${FILES[@]}"
do
echo Looking at "${FILE}"
same=$(diff existing/"${FILE}" "$APP_NAME"/"${FILE}")
return_code=$?
if [ $return_code != 0 ]; then
echo "$same"
exit 1
fi
done
echo Repo "$GH_REPO" contents OK, exiting.
exit 0
else
gh repo create "$GH_REPO" --public --gitignore Python --license Apache-2.0 || exit
gh repo view "$GH_REPO" --json id 2> /dev/null
return_code=$?
if [ $return_code != 0 ]; then
echo Repo create of "$GH_REPO" could not be confirmed
exit $return_code
fi
fi
setup_data
author=$(git log -1 --pretty=format:"%aN")
email=$(git log -1 --pretty=format:"%ae")
git config user.email "$email"
git config user.name "$author"
git status -s | awk '{ print $2 }' | xargs -l -r git add
git commit -a -m "Copy repo content"
git push origin "$GITHUB_DEFAULT_BRANCH"
image: registry.redhat.io/openshift4/ose-docker-builder-rhel9:v4.16
env:
- name: GITHUB_TOKEN
valueFrom:
secretKeyRef:
name: {{ .Values.gitops.gitSecretName }}
key: {{ .Values.gitops.gitSecretKeyToken }}
envFrom:
- configMapRef:
name: {{ .Release.Name }}-app-config
restartPolicy: Never