-
Notifications
You must be signed in to change notification settings - Fork 0
/
createJenkinsCredentials.sh
executable file
·134 lines (126 loc) · 5.99 KB
/
createJenkinsCredentials.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
#!/bin/bash
# Ela
RESOURCE_PREFIX=$(cat creds.json | jq -r '.resourcePrefix')
AZURE_LOCATION=$(cat creds.json | jq -r '.azureLocation')
DEPLOYMENT=$(cat creds.json | jq -r '.deployment')
# Ela - Removed all reference to "HTTP:// and placed all JENKINS URL in double quotes"
# Ela
JENKINS_USER=$(cat creds.json | jq -r '.jenkinsUser')
JENKINS_PASSWORD=$(cat creds.json | jq -r '.jenkinsPassword')
GITHUB_USER_NAME=$(cat creds.json | jq -r '.githubUserName')
GITHUB_PERSONAL_ACCESS_TOKEN=$(cat creds.json | jq -r '.githubPersonalAccessToken')
DT_API_TOKEN=
JENKINS_URL_PORT=$(kubectl get service jenkins -n cicd -o=json | jq -r '.spec.ports[] | select(.name=="http") | .port')
# Ela
case $DEPLOYMENT in
aks)
JENKINS_URL="jenkins-$RESOURCE_PREFIX-dt-kube-demo.$AZURE_LOCATION.cloudapp.azure.com"
;;
eks)
JENKINS_URL=$(kubectl get service jenkins -n cicd -o=json | jq -r '.status.loadBalancer.ingress[].hostname | select (.!=null)')
;;
gke)
JENKINS_URL=$(kubectl get service jenkins -n cicd -o=json | jq -r '.status.loadBalancer.ingress[].ip | select (.!=null)')
;;
esac
# Ela
#if [ -n "JENKINS_URL" ]
#then
# JENKINS_URL=$(kubectl get service jenkins -n cicd -o=json | jq -r '.status.loadBalancer.ingress[].ip')
#fi
CRED_NAME=registry-creds
echo "-----------------------------------------------------------------------------------"
echo "Creating Credential '$CRED_NAME' within Jenkins ..."
echo "-----------------------------------------------------------------------------------"
echo "----------------------------------------------------"
echo "Checking if $CRED_NAME exists ..."
echo "----------------------------------------------------"
CRED_URL="$JENKINS_URL:$JENKINS_URL_PORT/credentials/store/system/domain/_/credential/$CRED_NAME/config.xml"
if [ "$(curl -sL -w '%{http_code}' $CRED_URL -o /dev/null)" == "200" ]
then
echo "----------------------------------------------------"
echo "Deleting $CRED_NAME since exists ..."
echo "----------------------------------------------------"
curl -X POST "$JENKINS_URL:$JENKINS_URL_PORT/credentials/store/system/domain/_/credential/$CRED_NAME/doDelete" \
--user $JENKINS_USER:$JENKINS_PASSWORD
fi
echo "----------------------------------------------------"
echo "Adding $CRED_NAME ..."
echo "----------------------------------------------------"
curl -X POST "$JENKINS_URL:$JENKINS_URL_PORT/credentials/store/system/domain/_/createCredentials" \
--user $JENKINS_USER:$JENKINS_PASSWORD \
--data-urlencode 'json={
"": "0",
"credentials": {
"scope": "GLOBAL",
"id": "'$CRED_NAME'",
"username": "'$REGISTRY_USER'",
"password": "'$REGISTRY_TOKEN'",
"description": "Token used by Jenkins to push to the container registry",
"$class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
}
}'
CRED_NAME=git-credentials-acm
echo "-----------------------------------------------------------------------------------"
echo "Creating Credential '$CRED_NAME' within Jenkins ..."
echo "-----------------------------------------------------------------------------------"
echo "----------------------------------------------------"
echo "Checking if $CRED_NAME exists ..."
echo "----------------------------------------------------"
CRED_URL="$JENKINS_URL:$JENKINS_URL_PORT/credentials/store/system/domain/_/credential/$CRED_NAME/config.xml"
if [ "$(curl -sL -w '%{http_code}' $CRED_URL -o /dev/null)" == "200" ]
then
echo "----------------------------------------------------"
echo "Deleting $CRED_NAME since exists ..."
echo "----------------------------------------------------"
curl -X POST "$JENKINS_URL:$JENKINS_URL_PORT/credentials/store/system/domain/_/credential/$CRED_NAME/doDelete" \
--user $JENKINS_USER:$JENKINS_PASSWORD
fi
echo "----------------------------------------------------"
echo "Adding $CRED_NAME ..."
echo "----------------------------------------------------"
echo "Creating Credential 'git-credentials-acm' within Jenkins ..."
curl -X POST "$JENKINS_URL:$JENKINS_URL_PORT/credentials/store/system/domain/_/createCredentials" --user $JENKINS_USER:$JENKINS_PASSWORD \
--data-urlencode 'json={
"": "0",
"credentials": {
"scope": "GLOBAL",
"id": "'$CRED_NAME'",
"username": "'$GITHUB_USER_NAME'",
"password": "'$GITHUB_PERSONAL_ACCESS_TOKEN'",
"description": "Token used by Jenkins to access the GitHub repositories",
"$class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
}
}'
CRED_NAME=perfsig-api-token
echo "-----------------------------------------------------------------------------------"
echo "Creating Credential '$CRED_NAME' within Jenkins ..."
echo "-----------------------------------------------------------------------------------"
echo "----------------------------------------------------"
echo "Checking if $CRED_NAME exists ..."
echo "----------------------------------------------------"
CRED_URL="$JENKINS_URL:$JENKINS_URL_PORT/credentials/store/system/domain/_/credential/$CRED_NAME/config.xml"
if [ "$(curl -sL -w '%{http_code}' $CRED_URL -o /dev/null)" == "200" ]
then
echo "----------------------------------------------------"
echo "Deleting $CRED_NAME since exists ..."
echo "----------------------------------------------------"
curl -X POST "$JENKINS_URL:$JENKINS_URL_PORT/credentials/store/system/domain/_/credential/$CRED_NAME/doDelete" \
--user $JENKINS_USER:$JENKINS_PASSWORD
fi
echo "----------------------------------------------------"
echo "Adding $CRED_NAME ..."
echo "----------------------------------------------------"
echo "Creating Credential 'perfsig-api-token' within Jenkins ..."
curl -X POST "$JENKINS_URL:$JENKINS_URL_PORT/credentials/store/system/domain/_/createCredentials" --user $JENKINS_USER:$JENKINS_PASSWORD \
--data-urlencode 'json={
"": "0",
"credentials": {
"scope": "GLOBAL",
"id": "'$CRED_NAME'",
"apiToken": "'$DT_API_TOKEN'",
"description": "Dynatrace API Token used by the Performance Signature plugin",
"$class": "de.tsystems.mms.apm.performancesignature.dynatracesaas.model.DynatraceApiTokenImpl"
}
}'
echo ""