Repository navigation
Expand file tree
/
Copy pathgitlab-cli
More file actions
146 lines (115 loc) · 4.62 KB
/
Copy pathgitlab-cli
File metadata and controls
146 lines (115 loc) · 4.62 KB
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
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
set -x
project=$1
branch=$2
tokenuser=$3
gitlaburl=https://gitlab.com/api/v4
datefile=`date '+%d%m%y%H%M%S'`
tmpfile="./gitlabtrigger_pipeline_$datefile.tmp"
tmpprojectsfile="./gitlabtrigger_projects_$datefile.tmp"
tmptokenfile="./token_$datefile.tmp"
# check arg help
if [[ $# -eq 0 ]] ; then
echo 'Usage : bin project branch'
echo 'Example : bin transfert master'
exit 0
fi
function get_projects() {
echo "------------------------------------------------"
echo " Get Gitlab Projects"
echo "------------------------------------------------"
echo ""
curl -X GET --header "PRIVATE-TOKEN: $tokenuser" -s $gitlaburl/projects/ | jq . > $tmpprojectsfile
}
function get_project_id() {
echo "------------------------------------------------"
echo " Get Gitlab project pipelines"
echo "------------------------------------------------"
echo ""
project_id=(`curl --header "PRIVATE-TOKEN: $tokenuser" -s $gitlaburl/projects?search=$project | jq -r .[].id`)
echo $project_id
}
function create_project_trigger() {
echo "------------------------------------------------"
echo " create project trigger for $project"
echo "------------------------------------------------"
echo ""
curl -X POST --header "PRIVATE-TOKEN: $tokenuser" --form description="$project token" -s $gitlaburl/projects/$project_id/triggers | jq -r > $tmptokenfile
token_trigger=`jq -r .token $tmptokenfile`
token_trigger_id=`jq -r .id $tmptokenfile`
echo "------------------------------------------------"
echo "Created trigger $token_trigger"
}
function run_pipeline() {
echo "------------------------------------------------"
echo " Gitlab pipeline id=$project_id"
echo "------------------------------------------------"
echo ""
echo "Trigger Gitlab pipeline job for project $projects"
curl -X POST -F token="$tokenuser" -F ref=$branch -s $gitlaburl/api/v4/projects/$projects/trigger/pipeline | jq . > $tmpfile
id=`jq -r '.id' $tmpfile`
url=`jq -r '.web_url' $tmpfile`
echo "Gitlab job id : $id"
echo "Gitlab job url : $url"
echo ""
until [ $(curl --header "PRIVATE-TOKEN: $tokenuser" -s $gitlaburl/api/v4/projects/$projects/pipelines/$id | jq -r .status) = "success" ]; do
echo "pipeline is running, please wait ..."
sleep 2
done
rm -f $tmpfile
echo ""
echo "Deployment complete"
echo "Success ! "
}
function run_prod_pipeline() {
echo "------------------------------------------------"
echo " Gitlab pipeline id=$project_id"
echo " Deploy to $branch"
echo "------------------------------------------------"
echo ""
curl -X POST --header "PRIVATE-TOKEN: $tokenuser" -s "$gitlaburl/projects/$project_id/trigger/pipeline?ref=$branch&token=$token_trigger" | jq . > $tmpfile
echo "Pipeline started on branch $branch"
cat $tmpfile
id=`jq -r '.id' $tmpfile`
# running_pipeline = `(curl -F token="$tokenuser" -s $gitlaburl/projects/$project_id/pipelines/pipelines?status=running | jq -r '.[].id')`
until [[ $(curl --header "PRIVATE-TOKEN: $tokenuser" -s $gitlaburl/projects/$project_id/pipelines/$id | jq -r .status) = "success" ]]; do
echo "pipeline is running, please wait ..."
sleep 10
done
echo "------------------------------------------------"
echo "Build complete"
echo "Now need to run manual deploy"
manual_job_id=(`curl --header "PRIVATE-TOKEN: $tokenuser" -s $gitlaburl/projects/$project_id/pipelines/$id/jobs?scope=manual | jq -r .[].id`)
#run manual job
curl -X POST --header "PRIVATE-TOKEN: $tokenuser" "$gitlaburl/projects/$project_id/jobs/$manual_job_id/play"
echo "------------------------------------------------"
echo "Deploy started"
echo ""
until [[ $(curl --header "PRIVATE-TOKEN: $tokenuser" -s $gitlaburl/projects/$project_id/jobs/$manual_job_id | jq -r .status) = "success" ]]; do
echo "deploy is running, please wait ..."
echo "------------------------------------------------"
sleep 10
done
rm -f $tmpfile
echo ""
echo "Deployment complete"
echo "Success ! "
echo "------------------------------------------------"
}
function delete_trigger() {
echo "------------------------------------------------"
echo "Build complete"
curl --request DELETE --header "PRIVATE-TOKEN: $tokenuser" "$gitlaburl/projects/$project_id/triggers/$token_trigger_id"
}
if [[ $branch == prod ]] ; then
get_project_id
create_project_trigger
run_prod_pipeline
delete_trigger
fi
if [[ $branch == master ]] ; then
get_project_id
create_project_trigger
run_prod_pipeline
delete_trigger
fi