-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_CICD_Variables.sh
74 lines (61 loc) · 2.49 KB
/
change_CICD_Variables.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
#!/bin/bash
#ADD your credentials and URLs
USERNAME="<Your_Username>"
PASSWORD="<Your_Password>"
LOGIN_URL= "<Your_login_Access_URL>"
GITLAB_API_URL= "<Your_Project_API>"
PROJECT_ID= "<Your_Project_ID>"
ACCESS_TOKEN= "<Your_Project_Access_Token>"
Gitlab_url="<Your_Project_URL>"
gitlab_pass="<Your_Gitlab_Password>"
# Iterate through the passed key-value pairs and update CI/CD variables (from the first script)
for update in "${@}"; do
IFS='=' read -r key value <<< "${update}"
echo key= ${key} value = ${value}
# Update CI/CD variable
VARIABLE_KEY="${key}"
NEW_VARIABLE_VALUE="${value}"
# Update CI/CD variable
UPDATE_VARIABLE=$(curl --request PUT \
--url "${GITLAB_API_URL}/projects/${PROJECT_ID}/variables/${VARIABLE_KEY}" \
--header "Authorization: Bearer ${ACCESS_TOKEN}" \
--cookie cookies.txt \
--data-urlencode "value=${NEW_VARIABLE_VALUE}")
# Check for errors in the response
if [[ $(echo "${UPDATE_VARIABLE}" | jq '.error') == "null" ]]; then
echo "CI/CD Variable 'BUDGET' updated successfully."
else
echo "Error updating CI/CD Variable 'BUDGET':"
echo "${UPDATE_VARIABLE}" | jq .
exit 1
fi
done
# Get the login page to extract any necessary tokens or cookies
login_page=$(curl -s "${LOGIN_URL}")
# Extract any necessary tokens or cookies (replace with actual extraction)
# For example, extracting CSRF token from HTML:
#csrf_token=$(echo "${login_page}" | grep -oP 'csrf_token=\K[^&]+')
# Perform login
response=$(curl -s -c cookies.txt -b cookies.txt -d "username=${USERNAME}&password=${PASSWORD}&csrf_token=${csrf_token}" -X POST "${LOGIN_URL}")
# Do something after logging in (replace with your own logic)
echo "Logged in successfully!"
# Fetch CI/CD variables after login
CI_CD_VARIABLES=$(curl --request GET \
--url "${GITLAB_API_URL}/projects/${PROJECT_ID}/variables" \
--header "Authorization: Bearer ${ACCESS_TOKEN}" \
--cookie cookies.txt)
# Check for errors in the response
if [[ $(echo "${CI_CD_VARIABLES}" | jq '. | if type == "array" then length else 1 end') -eq 0 ]]; then
echo "Error: Unable to fetch CI/CD variables. Check your credentials or project ID."
exit 1
fi
# Process and display CI/CD variables
echo "CI/CD Variables:"
if [[ $(echo "${CI_CD_VARIABLES}" | jq 'type') == "array" ]]; then
echo "${CI_CD_VARIABLES}" | jq -c '.[] | {key: .key, value: .value}' | while IFS= read -r line; do
echo "${line}" | jq -r '"\(.key): \(.value)"'
done
else
#echo "Error in CI/CD Variables response:"
echo "${CI_CD_VARIABLES}" | jq .
fi