568a2dd9da454131f89ab25f8ff0bc69e2be744e
[osm/devops.git] /
1 #######################################################################################
2 # Copyright ETSI Contributors and Others.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #    http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #######################################################################################
17
18 apiVersion: argoproj.io/v1alpha1
19 kind: WorkflowTemplate
20 metadata:
21   name: git-wft
22   namespace: osm-workflows
23 spec:
24   templates:
25
26   - name: git-clone
27     inputs:
28       parameters:
29       - name: repo_url
30       - name: destination_folder
31       - name: git_cred_secret
32       - name: git_volume_name
33       - name: mount_path
34     volumes:
35       - name: repos-volume
36         persistentVolumeClaim:
37           claimName: '{{inputs.parameters.git_volume_name}}'
38     script:
39       image: alpine/git:2.45.1
40       env:
41       - name: GIT_USER
42         valueFrom:
43           secretKeyRef:
44             name: "{{inputs.parameters.git_cred_secret}}"
45             key: username
46       - name: GIT_PASS
47         valueFrom:
48           secretKeyRef:
49             name: "{{inputs.parameters.git_cred_secret}}"
50             key: password
51       volumeMounts:
52       - name: repos-volume
53         mountPath: '{{inputs.parameters.mount_path}}'
54       command: ["sh"]
55       source: |
56         FULL_URL="{{inputs.parameters.repo_url}}"
57         DESTINATION="{{inputs.parameters.destination_folder}}"
58         CLONE_URL=""
59
60         echo "Cloning: ${FULL_URL} . . ."
61
62         [[ -n "${DESTINATION}" ]] && mkdir -p "${DESTINATION}"
63
64         # Determine final clone URL
65         if [[ -z "${GIT_USER}" ]]; then
66           CLONE_URL="${FULL_URL}"
67         elif [[ -n "${GIT_PASS}" ]]; then
68           PROTOCOL=$(echo "${FULL_URL}" | awk -F '://' '{print $1}')
69           BASE_URL=$(echo "${FULL_URL}" | awk -F '://' '{print $2}')
70           CLONE_URL="${PROTOCOL}://${GIT_USER}@${BASE_URL}"
71         else
72           echo "ERROR: Malformed invocation."
73           echo "  FULL_URL=${FULL_URL}"
74           echo "  GIT_USER=${GIT_USER}"
75           echo "  DESTINATION=${DESTINATION}"
76           exit 1
77         fi
78
79         # Clone
80         mkdir -p /repos
81         cd /repos
82         if [[ -z "${DESTINATION}" ]]; then
83           echo -e "${GIT_PASS}\n" | git clone "${CLONE_URL}"
84         else
85           echo -e "${GIT_PASS}\n" | git clone "${CLONE_URL}" "${DESTINATION}"
86         fi
87
88   - name: git-commit-merge-push
89     inputs:
90       parameters:
91       - name: repo_folder
92       - name: git_cred_secret
93       - name: git_volume_name
94       - name: mount_path
95       - name: commit_message
96       - name: main_branch
97         value: main
98       - name: contrib_branch
99         value: osm_contrib
100       - name: dry_run
101         value: false
102     volumes:
103       - name: repos-volume
104         persistentVolumeClaim:
105           claimName: '{{inputs.parameters.git_volume_name}}'
106     script:
107       image: alpine/git:2.45.1
108       env:
109       - name: GIT_USER
110         valueFrom:
111           secretKeyRef:
112             name: "{{inputs.parameters.git_cred_secret}}"
113             key: username
114       - name: GIT_PASS
115         valueFrom:
116           secretKeyRef:
117             name: "{{inputs.parameters.git_cred_secret}}"
118             key: password
119       volumeMounts:
120       - name: repos-volume
121         mountPath: '{{inputs.parameters.mount_path}}'
122       command: ["sh"]
123       source: |
124         DESTINATION="{{inputs.parameters.repo_folder}}"
125         COMMIT_MESSAGE="{{inputs.parameters.commit_message}}"
126         CONTRIB_BRANCH="{{inputs.parameters.contrib_branch}}"
127         MAIN_BRANCH="{{inputs.parameters.main_branch}}"
128         DRY_RUN="{{inputs.parameters.dry_run}}"
129
130         # Go to the repo folder
131         cd "${DESTINATION}"
132
133         # Setup global Git user and email
134         echo "Setting up global Git user and e-mail..."
135         git config --global user.name "${GIT_USER}"
136         git config --global user.email "${GIT_USER}@${GIT_USER}.local"
137
138         # Create contrib branch
139         echo "Creating ${CONTRIB_BRANCH} branch into ${DESTINATION}..."
140         git checkout -b ${CONTRIB_BRANCH}
141
142         # Creating commit
143         git status
144         git add -A
145         git commit -m "Operation ${CONTRIB_BRANCH}: ${COMMIT_MESSAGE}"
146
147         # Pull and merge branch
148         git checkout ${MAIN_BRANCH}
149         echo "Pulling latest commits from ${MAIN_BRANCH} branch (if any)..."
150         echo -e "${GIT_PASS}\n" | git pull
151
152         echo "Merging branch ${CONTRIB_BRANCH} onto ${MAIN_BRANCH}..."
153         git merge --no-ff "${CONTRIB_BRANCH}"
154
155         if [[ "${DRY_RUN}" != "true" ]]
156         then
157           echo "Pushing..."
158           cat << "EOF" > "${HOME}/git-creds.sh"
159         #!/bin/sh
160         if echo "$1" | grep -q '^Password'; then
161           echo "${GIT_PASS}"
162         else
163           echo "${GIT_USER}"
164         fi
165         exit 0
166         EOF
167
168           chmod +x "${HOME}/git-creds.sh"
169           TTY=$(tty) GIT_USERNAME="${GIT_USER}" GIT_ASKPASS=~/git-creds.sh git push origin "${MAIN_BRANCH}"
170
171         else
172           echo "DRY RUN - NO PUSH"
173         fi