-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtrigger-pipeline.sh
More file actions
246 lines (214 loc) · 7.97 KB
/
trigger-pipeline.sh
File metadata and controls
246 lines (214 loc) · 7.97 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/bash
# Script to manually trigger GitLab pipeline for WebCMS development deployments
#
# Usage after pushing to GitHub:
# 1. Go to GitLab: https://gitlab.epa.gov/drupalcloud/drupalclouddeployment/-/settings/repository
# 2. Find "Mirroring repositories" section
# 3. Click "Update now" button next to the GitHub mirror
# 4. Run this script: ./trigger-pipeline.sh
#
# One-time Setup:
# 1. Create a Personal Access Token in GitLab:
# - Go to: https://gitlab.epa.gov/-/user_settings/personal_access_tokens
# - Token name: "WebCMS Pipeline Trigger"
# - Scopes: Select "api" (full API access)
# - Click "Create personal access token"
# - Copy the token (you won't see it again!)
#
# 2. Set your token as an environment variable (add to ~/.bashrc for persistence):
# export GITLAB_TOKEN="your-token-here"
# Default to development branch (hardcoded for safety)
BRANCH="${1:-development}"
GITLAB_TOKEN="${2:-$GITLAB_TOKEN}"
# Only allow development branch
if [ "$BRANCH" != "development" ]; then
echo "❌ Error: This script only triggers pipelines for the development branch"
echo "Current branch: $BRANCH"
exit 1
fi
# EPA GitLab Configuration
GITLAB_URL="https://gitlab.epa.gov"
PROJECT_PATH="drupalcloud/drupalclouddeployment"
if [ -z "$GITLAB_TOKEN" ]; then
echo "❌ Error: GitLab token not provided"
echo ""
echo "Usage: $0 [branch] [gitlab-token]"
echo " branch defaults to 'development' (only development is allowed)"
echo ""
echo "Setup Instructions:"
echo "1. Create a Personal Access Token:"
echo " https://gitlab.epa.gov/-/user_settings/personal_access_tokens"
echo ""
echo "2. Set as environment variable:"
echo " export GITLAB_TOKEN='your-token-here'"
echo ""
echo "3. Sync the GitLab mirror manually:"
echo " https://gitlab.epa.gov/drupalcloud/drupalclouddeployment/-/settings/repository"
echo " Click 'Update now' next to the GitHub mirror"
echo ""
echo "4. Run this script:"
echo " ./trigger-pipeline.sh"
exit 1
fi
echo "🚀 Triggering GitLab Pipeline for Development"
echo "============================================="
echo "Project: $PROJECT_PATH"
echo "Branch: $BRANCH (development only)"
echo "GitLab: $GITLAB_URL"
echo ""
# Step 1: Get project ID from project path
echo "📡 Looking up project ID..."
PROJECT_INFO=$(curl -s --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
"${GITLAB_URL}/api/v4/projects/$(echo $PROJECT_PATH | sed 's/\//%2F/g')")
PROJECT_ID=$(echo "$PROJECT_INFO" | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
if [ -z "$PROJECT_ID" ]; then
echo "❌ Failed to find project: $PROJECT_PATH"
echo ""
echo "Possible issues:"
echo " - Invalid token or insufficient permissions"
echo " - Project path is incorrect"
echo " - You don't have access to this project"
echo ""
echo "Response from GitLab:"
echo "$PROJECT_INFO" | head -n 5
exit 1
fi
echo "✓ Found project ID: $PROJECT_ID"
echo ""
# Step 2: Trigger mirror sync
echo "🔄 Syncing GitHub mirror to GitLab..."
SYNC_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST \
"${GITLAB_URL}/api/v4/projects/${PROJECT_ID}/mirror/pull" \
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}")
SYNC_HTTP_CODE=$(echo "$SYNC_RESPONSE" | tail -n 1)
if [ "$SYNC_HTTP_CODE" = "200" ] || [ "$SYNC_HTTP_CODE" = "204" ]; then
echo "✓ Mirror sync initiated"
echo "⏳ Waiting for mirror to sync latest commit..."
# Get the current commit SHA from local git
LOCAL_COMMIT=$(git rev-parse HEAD)
echo " Local commit: ${LOCAL_COMMIT:0:7}"
# Poll GitLab to check if the commit is available
# Will wait up to 5 minutes (150 attempts x 2 seconds)
MAX_ATTEMPTS=150
ATTEMPT=1
while true; do
# Check if the commit exists in GitLab
COMMIT_CHECK=$(curl -s --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
"${GITLAB_URL}/api/v4/projects/${PROJECT_ID}/repository/commits/${LOCAL_COMMIT}" 2>/dev/null)
if echo "$COMMIT_CHECK" | grep -q '"id"'; then
echo "✓ Commit ${LOCAL_COMMIT:0:7} found in GitLab (took ${ATTEMPT} attempts, ~$((ATTEMPT * 2)) seconds)"
break
fi
# Show progress every 5 attempts (every 10 seconds)
if [ $((ATTEMPT % 5)) -eq 0 ]; then
echo " Still waiting... (~$((ATTEMPT * 2))s elapsed)"
fi
# Safety timeout after 5 minutes
if [ $ATTEMPT -ge $MAX_ATTEMPTS ]; then
echo ""
echo "⚠️ Mirror sync taking longer than expected (5 minutes)"
echo " This may indicate an issue with the GitHub → GitLab mirror."
echo ""
echo " Continuing to wait... (Press Ctrl+C to cancel)"
echo " You can check mirror status at:"
echo " https://gitlab.epa.gov/drupalcloud/drupalclouddeployment/-/settings/repository"
echo ""
fi
sleep 2
ATTEMPT=$((ATTEMPT + 1))
done
else
echo "⚠️ Mirror sync returned HTTP $SYNC_HTTP_CODE"
echo " Continuing anyway and will poll for commit..."
echo ""
# Even if sync request failed, still try to poll for the commit
# Get the current commit SHA from local git
LOCAL_COMMIT=$(git rev-parse HEAD)
echo " Local commit: ${LOCAL_COMMIT:0:7}"
echo " Waiting for commit to appear in GitLab..."
MAX_ATTEMPTS=150
ATTEMPT=1
while true; do
COMMIT_CHECK=$(curl -s --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
"${GITLAB_URL}/api/v4/projects/${PROJECT_ID}/repository/commits/${LOCAL_COMMIT}" 2>/dev/null)
if echo "$COMMIT_CHECK" | grep -q '"id"'; then
echo "✓ Commit ${LOCAL_COMMIT:0:7} found in GitLab (took ${ATTEMPT} attempts, ~$((ATTEMPT * 2)) seconds)"
break
fi
if [ $((ATTEMPT % 5)) -eq 0 ]; then
echo " Still waiting... (~$((ATTEMPT * 2))s elapsed)"
fi
if [ $ATTEMPT -ge $MAX_ATTEMPTS ]; then
echo ""
echo "⚠️ Mirror sync taking longer than expected (5 minutes)"
echo " Continuing to wait... (Press Ctrl+C to cancel)"
fi
sleep 2
ATTEMPT=$((ATTEMPT + 1))
done
fi
echo ""
# Step 3: Trigger the pipeline
echo "🔨 Triggering pipeline..."
# Build pipeline variables JSON body
VARIABLES_JSON=""
if [ "$SKIP_BUILD" == "true" ]; then
VARIABLES_JSON='{"variables": [{"key": "SKIP_BUILD", "value": "true"}]}'
echo " Mode: DEPLOY-ONLY (SKIP_BUILD=true)"
else
echo " Mode: FULL BUILD + DEPLOY"
fi
echo ""
# Trigger pipeline with proper JSON body for variables
if [ -n "$VARIABLES_JSON" ]; then
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST \
"${GITLAB_URL}/api/v4/projects/${PROJECT_ID}/pipeline?ref=${BRANCH}" \
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
--header "Content-Type: application/json" \
--data "$VARIABLES_JSON")
else
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST \
"${GITLAB_URL}/api/v4/projects/${PROJECT_ID}/pipeline?ref=${BRANCH}" \
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}")
fi
HTTP_CODE=$(echo "$RESPONSE" | tail -n 1)
BODY=$(echo "$RESPONSE" | sed '$d')
if [ "$HTTP_CODE" = "201" ]; then
echo "✅ Pipeline triggered successfully!"
echo ""
PIPELINE_ID=$(echo "$BODY" | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
PIPELINE_URL=$(echo "$BODY" | grep -o '"web_url":"[^"]*"' | cut -d'"' -f4)
echo "Pipeline ID: $PIPELINE_ID"
echo "Pipeline URL: $PIPELINE_URL"
echo ""
echo "🔍 View pipeline at:"
echo " $PIPELINE_URL"
elif [ "$HTTP_CODE" = "400" ]; then
echo "❌ Failed to trigger pipeline (HTTP $HTTP_CODE)"
echo ""
echo "Possible issues:"
echo " - Branch '$BRANCH' doesn't exist"
echo " - Pipeline is already running for this branch"
echo " - CI/CD is disabled for this project"
echo ""
echo "Response:"
echo "$BODY"
exit 1
elif [ "$HTTP_CODE" = "401" ] || [ "$HTTP_CODE" = "403" ]; then
echo "❌ Authentication failed (HTTP $HTTP_CODE)"
echo ""
echo "Your token may be:"
echo " - Invalid or expired"
echo " - Missing 'api' scope"
echo " - Not authorized for this project"
echo ""
echo "Create a new token at:"
echo " https://gitlab.epa.gov/-/user_settings/personal_access_tokens"
exit 1
else
echo "❌ Failed to trigger pipeline (HTTP $HTTP_CODE)"
echo ""
echo "Response:"
echo "$BODY"
exit 1
fi