1+ default :
2+ interruptible : true
3+
4+ .comment_template :
5+ image : badouralix/curl-jq
6+ tags :
7+ - deploy_docs
8+ - shiny
9+
10+ .add_comment_script : &add_comment_script
11+ - |
12+ GITLAB_API="https://${CI_SERVER_HOST}:${CI_SERVER_PORT}/api/v4/projects/${PROJECT_ID}/merge_requests/${MR_ID}/notes"
13+ AUTH_HEADER="PRIVATE-TOKEN: ${GITLAB_BOT_API_TOKEN}"
14+
15+ # Get existing comments
16+ API_RESPONSE=$(curl --silent --header "$AUTH_HEADER" "$GITLAB_API")
17+
18+ # Check if the response contains the expected structure
19+ COMMENTS=$(echo "$API_RESPONSE" | jq -r ".[] | select(.body | contains(\"$COMMENT_IDENTIFIER\")) | .id")
20+
21+ # Delete previous preview comments
22+ if [ -n "$COMMENTS" ]; then
23+ for COMMENT_ID in $COMMENTS; do
24+ curl --silent --request DELETE \
25+ --header "$AUTH_HEADER" \
26+ "${GITLAB_API}/${COMMENT_ID}"
27+ done
28+ fi
29+
30+ # Post a new comment
31+ curl --silent --request POST \
32+ --header "$AUTH_HEADER" \
33+ --header "Content-Type: application/json" \
34+ --data "{\"body\": \"$COMMENT_BODY\"}" \
35+ "$GITLAB_API"
36+
37+ # Define the reusable rule sets
38+ .default-rules :
39+ rules :
40+ - if : ' $CI_PIPELINE_SOURCE == "merge_request_event"'
41+
42+ .label-based-rules :
43+ rules :
44+ - if : ' $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_LABELS =~ /GitHub-Sync-Merge/ && $CI_MERGE_REQUEST_LABELS !~ /GitHub-Edit/'
45+
146stages :
247 - build
348 - deploy
4- - comment
49+ - comment_preview
50+ - sync_merge
51+ - comment_github_pr
552
653build_hugo :
754 stage : build
855 image : " ${CI_TEMPLATE_REGISTRY_HOST}/pages/hugo/hugo_extended:0.135.0"
956 tags :
1057 - build_docs
1158 rules :
12- - if : $CI_PIPELINE_SOURCE == 'merge_request_event'
59+ !reference [.default-rules, rules]
1360 variables :
1461 GIT_SUBMODULE_STRATEGY : recursive
1562 NAME : " ${CI_COMMIT_REF_SLUG}"
@@ -30,7 +77,7 @@ deploy_preview_hugo:
3077 - deploy_docs
3178 - shiny
3279 rules :
33- - if : $CI_PIPELINE_SOURCE == 'merge_request_event'
80+ !reference [.default-rules, rules]
3481 needs : ["build_hugo"]
3582 variables :
3683 SSH_KEY : " $DOCS_PREVIEW_PRIVATEKEY" # SSH_KEY used inside espressif/scp
@@ -50,46 +97,135 @@ deploy_preview_hugo:
5097 - echo "Preview ${SERVER_URL_BASE}/${NAME}"
5198
5299post_preview_link :
53- stage : comment
54- image : badouralix/curl-jq
100+ extends : .comment_template
101+ stage : comment_preview
102+ rules :
103+ !reference [.default-rules, rules]
104+ needs : ["deploy_preview_hugo"]
105+ variables :
106+ SERVER_URL_BASE : " $DOCS_PREVIEW_URL_BASE"
107+ NAME : " ${CI_COMMIT_REF_SLUG}"
108+ MR_ID : " $CI_MERGE_REQUEST_IID"
109+ PROJECT_ID : " $CI_MERGE_REQUEST_PROJECT_ID"
110+ script :
111+ - |
112+ # Create varialbes for adding a comment
113+ export COMMENT_IDENTIFIER="🎉 Preview for this MR"
114+ export PREVIEW_LINK="${SERVER_URL_BASE}/${NAME}"
115+ export COMMENT_BODY="🎉 Preview for this MR: ${PREVIEW_LINK}"
116+
117+ - *add_comment_script
118+
119+ sync_merge_to_github :
120+ stage : sync_merge
121+ image : alpine:latest
55122 tags :
56123 - deploy_docs
57124 - shiny
58125 rules :
59- - if : $CI_PIPELINE_SOURCE == 'merge_request_event'
60- needs : ["deploy_preview_hugo"]
126+ !reference [.label-based-rules, rules]
127+ variables :
128+ GITHUB_REPO : " espressif/developer-portal"
129+ script :
130+ - apk update # Update the package index
131+ - apk add --no-cache git bash github-cli # Install github-cli
132+
133+ - export SOURCE_BRANCH="${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}"
134+
135+ # Configure Git
136+ -
git config --global user.email "[email protected] " 137+ - git config --global user.name "Developer-Portal-BOT"
138+
139+ # Clone the repository
140+ - git clone "$CI_REPOSITORY_URL" repo || (echo "Error cloning repository" && exit 1)
141+ - cd repo
142+ # Rebase the feature branch on main
143+ - git checkout "$SOURCE_BRANCH"
144+ - git fetch origin main
145+ - git rebase main
146+ # Add GitHub remote and push the rebased branch to GitHub
147+ - git remote add github "https://oauth2:${GITHUB_ACCESS_TOKEN}@github.com/${GITHUB_REPO}.git"
148+ # Consider using --force-with-lease here
149+ - git push -u -f github "$SOURCE_BRANCH"
150+
151+ # Wait for GitHub to register the branch
152+ - sleep 5
153+
154+ # Create a PR on GitHub
155+ - export GITHUB_TOKEN="$GITHUB_ACCESS_TOKEN"
156+ - |
157+ PR_URL=$(gh pr list \
158+ --repo "$GITHUB_REPO" \
159+ --head "$SOURCE_BRANCH" \
160+ --json url \
161+ --jq '.[].url')
162+
163+ if [[ -n "$PR_URL" ]]; then
164+ PR_NUMBER=$(gh pr view "$PR_URL" --json number -q '.number')
165+ echo "PR already exists: #$PR_NUMBER"
166+ echo "**GitHub PR:** $PR_URL"
167+ else
168+ echo "No PR found. Creating a new one..."
169+ gh pr create \
170+ --repo "$GITHUB_REPO" \
171+ --head "$SOURCE_BRANCH" \
172+ --base main \
173+ --title "Sync Merge: ${SOURCE_BRANCH}" \
174+ --body $'This PR syncs the GitLab branch `'"${SOURCE_BRANCH}"$'` to GitHub.\n\nThe changes have been reviewed internally.\n\n> [!WARNING]\n>If, for any reason, changes need be committed directly to the GitHub PR (bypassing GitLab), add the label \`GitHub-Edit\` in the GitLab MR. This will disable GitLab CI sync-merge to prevent overwriting changes on GitHub.' \
175+ --label "GitLab-Sync-Merge"
176+
177+ PR_URL=$(gh pr list \
178+ --repo "$GITHUB_REPO" \
179+ --head "$SOURCE_BRANCH" \
180+ --json url \
181+ --jq '.[].url')
182+
183+ if [[ -n "$PR_URL" ]]; then
184+ PR_NUMBER=$(gh pr view "$PR_URL" --json number -q '.number')
185+ echo "PR created successfully: #$PR_NUMBER"
186+ echo "**GitHub PR:** $PR_URL"
187+ else
188+ echo "Failed to create PR."
189+ exit 1
190+ fi
191+ fi
192+
193+ # Store PR_NUMBER and PR_URL as artifacts
194+ - cd $CI_PROJECT_DIR
195+ - echo "PR_NUMBER=$PR_NUMBER" > pr_info.txt
196+ - echo "PR_URL=$PR_URL" >> pr_info.txt
197+
198+ artifacts :
199+ paths :
200+ - pr_info.txt
201+ expire_in : 1 hour
202+
203+ sync_merge_comment :
204+ extends : .comment_template
205+ stage : comment_github_pr
206+ rules :
207+ !reference [.label-based-rules, rules]
208+ needs :
209+ - sync_merge_to_github
61210 variables :
62211 SERVER_URL_BASE : " $DOCS_PREVIEW_URL_BASE"
63212 NAME : " ${CI_COMMIT_REF_SLUG}"
64213 MR_ID : " $CI_MERGE_REQUEST_IID"
65214 PROJECT_ID : " $CI_MERGE_REQUEST_PROJECT_ID"
66215 script :
67216 - |
68- # Print MR_ID and PROJECT_ID for debugging
69- echo "MR_ID: ${MR_ID}"
70- echo "PROJECT_ID: ${PROJECT_ID}"
71- echo "$CI_SERVER_HOST"
72- echo "${CI_SERVER_PORT}"
73-
74- GITLAB_API="https://${CI_SERVER_HOST}:${CI_SERVER_PORT}/api/v4/projects/${PROJECT_ID}/merge_requests/${MR_ID}/notes"
75- AUTH_HEADER="PRIVATE-TOKEN: ${GITLAB_BOT_API_TOKEN}"
76-
77- # Get existing comments
78- API_RESPONSE=$(curl --silent --header "$AUTH_HEADER" "$GITLAB_API")
79- echo "API Response: $API_RESPONSE" # Add this line for debugging
80-
81- # Check if the response contains the expected structure
82- COMMENTS=$(echo "$API_RESPONSE" | jq -r '.[] | select(.body | contains("🎉 Preview for this MR")) | .id')
83-
84- # Delete previous preview comments
85- if [ -n "$COMMENTS" ]; then
86- for COMMENT_ID in $COMMENTS; do
87- curl --silent --request DELETE --header "$AUTH_HEADER" "${GITLAB_API}/${COMMENT_ID}"
88- done
217+ # Load PR_NUMBER and PR_URL from artifact file
218+ if [ -f "pr_info.txt" ]; then
219+ source pr_info.txt
220+ else
221+ echo "pr_info.txt not found!"
222+ exit 1
89223 fi
90224
91- # Post new preview link
92- PREVIEW_LINK="${SERVER_URL_BASE}/${NAME}"
93- COMMENT_BODY="🎉 Preview for this MR: ${PREVIEW_LINK}"
94- curl --silent --request POST --header "$AUTH_HEADER" --header "Content-Type: application/json" \
95- --data "{\"body\": \"${COMMENT_BODY}\"}" "$GITLAB_API"
225+ # Create varialbes for adding a comment
226+ COMMENT_IDENTIFIER="🚀 GitHub PR"
227+ COMMENT_BODY="🚀 GitHub PR for sync-merging: [#$PR_NUMBER]($PR_URL).\\n\\n\
228+ > ⚠️ **Warning**\\n> \\n\
229+ > If, for any reason, changes need be committed directly to the GitHub PR (bypassing GitLab), add the label \`GitHub-Edit\` in the GitLab MR. This will disable GitLab CI sync-merge to prevent overwriting changes on GitHub."
230+
231+ - *add_comment_script
0 commit comments