Skip to content

Commit 322b092

Browse files
committed
Merge branch 'pipelines' into 'main'
.gitlab-ci.yml; Cancel redundant pipelines in the CI. See merge request app-frameworks/esp-matter!1127
2 parents c11c7b1 + f98e0ce commit 322b092

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

.gitlab-ci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
stages:
2+
- cleanup
23
- docker_build
34
- build
45
- target_test
@@ -138,6 +139,17 @@ variables:
138139
- cd $ESP_MATTER_PATH/examples/controller
139140
- idf.py -D SDKCONFIG_DEFAULTS="sdkconfig.defaults.otbr" set-target esp32s3 build
140141

142+
cancel_redundant_pipelines:
143+
stage: cleanup
144+
image: python:3.12-slim
145+
tags:
146+
- cancel-pipeline
147+
rules:
148+
- if: '$CI_PIPELINE_SOURCE == "push" || $CI_PIPELINE_SOURCE == "merge_request_event"'
149+
script:
150+
- pip install --no-cache-dir requests
151+
- python tools/ci/cancel_pipelines.py
152+
141153
build_image:
142154
stage: docker_build
143155
image: espressif/dind:1

tools/ci/cancel_pipelines.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
2+
3+
# SPDX-License-Identifier: CC0-1.0
4+
5+
import re
6+
import os
7+
import requests
8+
from gitlab_api import GitLabAPI
9+
10+
def cancel_branch_pipelines(gitlab_api):
11+
url = f"{gitlab_api.gitlab_api_url}/projects/{gitlab_api.ci_project_id}/pipelines?ref={gitlab_api.ci_commit_ref_name}&status=running"
12+
headers = {"PRIVATE-TOKEN": gitlab_api.gitlab_token}
13+
response = requests.get(url, headers=headers)
14+
15+
if response.status_code == 200:
16+
running_pipelines = response.json()
17+
18+
for pipeline in running_pipelines:
19+
pipeline_id = pipeline.get('id')
20+
source = pipeline.get('source')
21+
ref = pipeline.get('ref')
22+
if pipeline_id != int(gitlab_api.ci_pipeline_id) and source != 'merge_request_event':
23+
print(f"Cancelling branch pipeline with pipeline ID: {pipeline_id}, Source:{source}, Ref: {ref}")
24+
gitlab_api.cancel_pipeline(pipeline_id)
25+
else:
26+
print(f"Skipping current branch pipeline with pipeline ID: {gitlab_api.ci_pipeline_id}")
27+
else:
28+
print(f"Failed to fetch branch pipelines, Status Code: {response.status_code}")
29+
30+
def cancel_merge_request_pipelines(gitlab_api):
31+
url = f"{gitlab_api.gitlab_api_url}/projects/{gitlab_api.ci_project_id}/pipelines?status=running"
32+
headers = {"PRIVATE-TOKEN": gitlab_api.gitlab_token}
33+
response = requests.get(url, headers=headers)
34+
35+
if response.status_code == 200:
36+
running_pipelines = response.json()
37+
38+
for pipeline in running_pipelines:
39+
pipeline_id = pipeline.get('id')
40+
source = pipeline.get('source')
41+
ref = pipeline.get('ref')
42+
merge_request_id_pattern = re.match(r"refs/merge-requests/(\d+)/head", ref)
43+
if merge_request_id_pattern:
44+
print(f"MR pipeline id: {merge_request_id_pattern.group(1)}")
45+
if source == "merge_request_event" and gitlab_api.ci_merge_request_iid == merge_request_id_pattern.group(1):
46+
if pipeline_id == int(gitlab_api.ci_pipeline_id):
47+
print(f"Skipping current MR pipeline with pipeline id: {pipeline_id} ref: {ref} source: {source}")
48+
else:
49+
print(f"Cancelling MR pipeline with pipeline id: {pipeline_id} ref:{ref} source: {source}")
50+
gitlab_api.cancel_pipeline(pipeline_id)
51+
else:
52+
print(f"Skipping pipeline with pipeline ID: {pipeline_id} (ref: {ref}, source: {source})")
53+
54+
else:
55+
print(f"Failed to fetch pipelines, Status: {response.status_code}")
56+
57+
58+
def main():
59+
gitlab_api = GitLabAPI()
60+
cancel_branch_pipelines(gitlab_api)
61+
cancel_merge_request_pipelines(gitlab_api)
62+
63+
if __name__ == "__main__":
64+
main()
65+

tools/ci/gitlab_api.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ def __init__(self):
1212
self.gitlab_token = os.getenv("GITLAB_MR_COMMENT_TOKEN")
1313
self.ci_project_id = os.getenv("CI_PROJECT_ID")
1414
self.ci_merge_request_iid = os.getenv("CI_MERGE_REQUEST_IID")
15+
self.ci_pipeline_id = os.getenv("CI_PIPELINE_ID")
16+
self.ci_commit_ref_name = os.getenv("CI_COMMIT_REF_NAME")
1517

16-
if not all([self.gitlab_api_url, self.gitlab_token, self.ci_project_id, self.ci_merge_request_iid]):
18+
if not all([self.gitlab_api_url, self.gitlab_token, self.ci_project_id, self.ci_pipeline_id]):
1719
raise ValueError("Required GitLab environment variables are not set")
1820

1921
def fetch_merge_request_description(self):
@@ -64,3 +66,13 @@ def download_artifact(self, job_id, artifact_path, output_file):
6466
with open(output_file, 'wb') as f:
6567
for chunk in response.iter_content(chunk_size=8192):
6668
f.write(chunk)
69+
70+
def cancel_pipeline(self, pipeline_id):
71+
url = f"{self.gitlab_api_url}/projects/{self.ci_project_id}/pipelines/{pipeline_id}/cancel"
72+
headers = {"PRIVATE-TOKEN": self.gitlab_token}
73+
response = requests.post(url, headers=headers)
74+
75+
if response.status_code == 200:
76+
print(f"Successfully cancelled Pipeline ID: {pipeline_id}")
77+
else:
78+
print(f"Failed to cancel Pipeline ID: {pipeline_id}, Status Code: {response.status_code}")

0 commit comments

Comments
 (0)