Skip to content

Commit 374602a

Browse files
authored
ci: add precomputed pipeline variables (#15582)
## Description This PRs adds an initial "setup" stage to the pipeline where we compute and expose the following variables to the rest of the pipeline: - **GH_PR_NUMBER:** The GitHub PR number if there is an open PR for this commit, empty otherwise - **HAS_OPEN_PR:** "true" if there is an open PR for this commit, "false" otherwise - **IS_MAIN_BRANCH:** "true" if the current branch is main - **IS_RELEASE_BRANCH:** "true" if the current branch is a release branch (e.g., "1.2"), "false" otherwise - **IS_RELEASE:** "true" if the current commit is a release tag (e.g., "v1.2.3"), "false" otherwise - **IS_MERGE_QUEUE:** "true" if the current branch is a merge queue branch (e.g., starts with "gh-readonly-queue/"), "false" otherwise The goal is to provide consistent controls for which jobs to run when, e.g. should a job run only when we have a PR open or only during a release? real example, with #15572 we want to be sure we still restrict benchmarks to only run/pass when we have an open PR to reduce volume, today that is controlled by the fact that the GHA wheel building only happens when a PR is open. ## Testing <!-- Describe your testing strategy or note what tests are included --> ## Risks <!-- Note any risks associated with this change, or "None" if no risks --> ## Additional Notes <!-- Any other information that would be helpful for reviewers -->
1 parent db5d2d3 commit 374602a

File tree

5 files changed

+99
-29
lines changed

5 files changed

+99
-29
lines changed

.gitlab-ci.yml

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
stages:
2+
- setup
23
- package
34
- tests
45
- shared-pipeline
@@ -41,6 +42,50 @@ include:
4142
- local: ".gitlab/benchmarks/serverless.yml"
4243
- local: ".gitlab/native.yml"
4344

45+
46+
# Expose the following variables to the rest of the pipeline
47+
# GH_PR_NUMBER: The GitHub PR number if there is an open PR for this commit, empty otherwise
48+
# HAS_OPEN_PR: "true" if there is an open PR for this commit, "false" otherwise
49+
# IS_MAIN_BRANCH: "true" if the current branch is main
50+
# IS_RELEASE_BRANCH: "true" if the current branch is a release branch (e.g., "1.2"), "false" otherwise
51+
# IS_RELEASE: "true" if the current commit is a release tag (e.g., "v1.2.3"), "false" otherwise
52+
# IS_MERGE_QUEUE: "true" if the current branch is a merge queue branch (e.g., starts with "gh-readonly-queue/"), "false" otherwise
53+
pipeline variables:
54+
image: registry.ddbuild.io/images/dd-octo-sts-ci-base:2025.06-1
55+
tags: [ "arch:amd64" ]
56+
stage: setup
57+
id_tokens:
58+
DDOCTOSTS_ID_TOKEN:
59+
aud: dd-octo-sts
60+
variables:
61+
GIT_STRATEGY: none
62+
GH_REPO: DataDog/dd-trace-py
63+
script:
64+
- |
65+
if [ -z ${GH_TOKEN} ]
66+
then
67+
# Use dd-octo-sts to get GitHub token
68+
dd-octo-sts token --scope DataDog/dd-trace-py --policy gitlab.github-access.read > token
69+
gh auth login --with-token < token
70+
rm token
71+
fi
72+
- |
73+
# Prevent git operation errors:
74+
# failed to determine base repo: failed to run git: fatal: detected dubious ownership in repository at ...
75+
git config --global --add safe.directory "${CI_PROJECT_DIR}"
76+
- |
77+
# Determine if we have an open GitHub PR for this commit
78+
GH_PR_NUMBER=$(gh pr list --state open --search "${CI_COMMIT_SHA}" --json number --jq '.[0].number' || echo "")
79+
echo "GH_PR_NUMBER=${GH_PR_NUMBER}" | tee -a workflow.env
80+
echo "HAS_OPEN_PR=$(if [ -z "${GH_PR_NUMBER}" ]; then echo "false"; else echo "true"; fi)" | tee -a workflow.env
81+
echo "IS_MAIN_BRANCH=$(if [ "${CI_COMMIT_BRANCH}" == "main" ]; then echo "true"; else echo "false"; fi)" | tee -a workflow.env
82+
echo "IS_RELEASE_BRANCH=$(if [[ "${CI_COMMIT_BRANCH}" =~ ^[0-9]+\.[0-9]+$ ]]; then echo "true"; else echo "false"; fi)" | tee -a workflow.env
83+
echo "IS_RELEASE=$(if [[ "${CI_COMMIT_TAG}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(rc[0-9]+)?$ ]]; then echo "true"; else echo "false"; fi)" | tee -a workflow.env
84+
echo "IS_MERGE_QUEUE=$(if [[ "${CI_COMMIT_BRANCH}" =~ ^gh-readonly-queue/.*$ ]]; then echo "true"; else echo "false"; fi)" | tee -a workflow.env
85+
artifacts:
86+
reports:
87+
dotenv: workflow.env
88+
4489
tests-gen:
4590
stage: tests
4691
extends: .testrunner
@@ -55,14 +100,14 @@ tests-gen:
55100
export GH_TOKEN=$(dd-octo-sts token --scope DataDog/dd-trace-py --policy gitlab.github-access.read)
56101
fi
57102
- scripts/gen_gitlab_config.py --verbose
58-
needs: []
103+
needs: [ "pipeline variables" ]
59104
artifacts:
60105
paths:
61106
- .gitlab/tests-gen.yml
62107

63108
run-tests-trigger:
64109
stage: tests
65-
needs: [ tests-gen ]
110+
needs: [ tests-gen, "pipeline variables" ]
66111
# Allow the child job to fail if explicitly asked
67112
rules:
68113
- if: $RELEASE_ALLOW_TEST_FAILURES == "true"
@@ -118,7 +163,7 @@ macrobenchmarks:
118163
rules:
119164
- if: $CI_PIPELINE_SOURCE == "schedule"
120165
when: always
121-
- if: $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+/
166+
- if: $IS_RELEASE == "true"
122167
when: always
123168
- when: manual
124169

@@ -131,10 +176,16 @@ check_new_flaky_tests:
131176
- export DD_API_KEY=$(aws ssm get-parameter --region us-east-1 --name ci.${CI_PROJECT_NAME}.dd-api-key-qualitygate --with-decryption --query "Parameter.Value" --out text)
132177
- export DD_APP_KEY=$(aws ssm get-parameter --region us-east-1 --name ci.${CI_PROJECT_NAME}.dd-app-key-qualitygate --with-decryption --query "Parameter.Value" --out text)
133178
- datadog-ci gate evaluate
134-
except:
135-
- main
136-
- '[0-9].[0-9]*'
137-
- 'mq-working-branch**'
179+
rules:
180+
- if: $IS_MAIN_BRANCH == "true"
181+
when: never
182+
- if: $IS_RELEASE == "true"
183+
when: never
184+
- if: $IS_RELEASE_BRANCH == "true"
185+
when: never
186+
- if: $IS_MERGE_QUEUE == "true"
187+
when: never
188+
- when: on_success
138189

139190
requirements_json_test:
140191
rules:
@@ -148,10 +199,10 @@ package-oci:
148199

149200
promote-oci-to-prod:
150201
stage: release
151-
rules: null
152-
only:
153-
# TODO: Support publishing rc releases
154-
- /^v[0-9]+\.[0-9]+\.[0-9]+$/
202+
rules:
203+
- if: $IS_RELEASE == "true"
204+
when: on_success
205+
- when: never
155206
needs:
156207
- job: release_pypi_prod
157208
- job: package-oci
@@ -177,10 +228,10 @@ promote-oci-to-staging:
177228

178229
publish-lib-init-pinned-tags:
179230
stage: release
180-
rules: null
181-
only:
182-
# TODO: Support publishing rc releases
183-
- /^v[0-9]+\.[0-9]+\.[0-9]+$/
231+
rules:
232+
- if: $IS_RELEASE == "true"
233+
when: on_success
234+
- when: never
184235
needs:
185236
- job: release_pypi_prod
186237
- job: create-multiarch-lib-injection-image

.gitlab/package.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,9 @@ verify_package_version:
116116
tags: [ "arch:amd64" ]
117117
stage: package
118118
needs: [ download_ddtrace_artifacts ]
119-
only:
120-
# v2.10.0
121-
# v2.10.1
122-
# v2.10.0rc0
123-
# v2.10.0rc5
124-
- /^v[0-9]+\.[0-9]+\.[0-9]+(rc[0-9]+)?$/
119+
rules:
120+
- if: $IS_RELEASE == "true"
121+
when: on_success
122+
- when: never
125123
script:
126124
- .gitlab/verify-package-versions.sh

.gitlab/release.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ variables:
33

44
.release_base:
55
stage: release
6-
only:
7-
# v2.10.0
8-
# v2.10.1
9-
# v2.10.0rc0
10-
# v2.10.0rc5
11-
- /^v[0-9]+\.[0-9]+\.[0-9]+(rc[0-9]+)?$/
6+
rules:
7+
- if: $IS_RELEASE == "true"
8+
when: on_success
9+
- when: never
1210

1311
.release_pypi:
1412
extends: .release_base

.gitlab/templates/build-base-venvs.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ build_base_venvs:
1212
DD_USE_SCCACHE: '1'
1313
DD_FAST_BUILD: '1'
1414
rules:
15-
- if: '$CI_COMMIT_REF_NAME == "main"'
15+
- if: $IS_MAIN == "true"
1616
variables:
1717
DD_FAST_BUILD: '0'
18-
- when: always
18+
- if: $IS_RELEASE == "true"
19+
variables:
20+
DD_FAST_BUILD: '0'
21+
- if: $IS_RELEASE_BRANCH == "true"
22+
variables:
23+
DD_FAST_BUILD: '0'
24+
- when: on_success
1925
script: |
2026
set -e -o pipefail
2127
riot -P -v generate --python=$PYTHON_VERSION

scripts/needs_testrun.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,18 @@ def needs_testrun(suite: str, pr_number: int, sha: t.Optional[str] = None) -> bo
159159
"""
160160
if "itr:noskip" in get_latest_commit_message().lower():
161161
return True
162+
163+
# Custom GitLab env variables
164+
# Always run all tests under these conditions
165+
if os.getenv("IS_MERGE_QUEUE", "false") == "true":
166+
return True
167+
if os.getenv("IS_MAIN_BRANCH", "false") == "true":
168+
return True
169+
if os.getenv("IS_RELEASE_BRANCH", "false") == "true":
170+
return True
171+
if os.getenv("IS_RELEASE", "false") == "true":
172+
return True
173+
162174
try:
163175
patterns = get_patterns(suite)
164176
except Exception as exc:
@@ -209,6 +221,11 @@ def _get_pr_number() -> int:
209221
if pr_url is not None:
210222
return int(pr_url.split("/")[-1])
211223

224+
# Custom environment variable
225+
number = os.environ.get("GH_PR_NUMBER")
226+
if number is not None:
227+
return int(number)
228+
212229
# GitLab
213230
ref_name = os.environ.get("CI_COMMIT_REF_NAME")
214231
if ref_name is not None:

0 commit comments

Comments
 (0)