Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ default:
- .gitlab/cgroup-info.sh
- gitlab_section_end "cgroup-info"

.gitlab_base_ref_params: &gitlab_base_ref_params
- |
if [[ ! $CI_COMMIT_BRANCH =~ ^(master|release/.*)$ ]]; then
export GIT_BASE_REF=$(.gitlab/find-gh-base-ref.sh)
export GRADLE_PARAMS="$GRADLE_PARAMS -PgitBaseRef=origin/$GIT_BASE_REF"
fi

.gradle_build: &gradle_build
image: ghcr.io/datadog/dd-trace-java-docker-build:${BUILDER_IMAGE_VERSION_PREFIX}base
stage: build
Expand Down Expand Up @@ -223,7 +230,8 @@ build_tests:
MAVEN_OPTS: "-Xms64M -Xmx512M -Dorg.slf4j.simpleLogger.defaultLogLevel=debug" # FIXME: Build :smokeTest build fails unless mvn debug logging is on

script:
- ./gradlew clean $GRADLE_TARGET -PskipTests $GRADLE_ARGS
- *gitlab_base_ref_params
- ./gradlew clean $GRADLE_TARGET $GRADLE_PARAMS -PskipTests $GRADLE_ARGS

populate_dep_cache:
extends: build_tests
Expand Down Expand Up @@ -327,7 +335,8 @@ test_published_artifacts:
variables:
CACHE_TYPE: lib
script:
- ./gradlew $GRADLE_TARGET -PskipTests -PrunBuildSrcTests -PskipSpotless -PtaskPartitionCount=$NORMALIZED_NODE_TOTAL -PtaskPartition=$NORMALIZED_NODE_INDEX $GRADLE_ARGS
- *gitlab_base_ref_params
- ./gradlew $GRADLE_TARGET $GRADLE_PARAMS -PskipTests -PrunBuildSrcTests -PskipSpotless -PtaskPartitionCount=$NORMALIZED_NODE_TOTAL -PtaskPartition=$NORMALIZED_NODE_INDEX $GRADLE_ARGS
after_script:
- *cgroup_info
- source .gitlab/gitlab-utils.sh
Expand Down Expand Up @@ -460,6 +469,7 @@ muzzle-dep-report:
- if: $CI_COMMIT_BRANCH == "master"
when: on_success
script:
- *gitlab_base_ref_params
- >
if [ "$PROFILE_TESTS" == "true" ] && [ "$testJvm" != "ibm8" ] && [ "$testJvm" != "oracle8" ];
then
Expand Down
76 changes: 76 additions & 0 deletions .gitlab/find-gh-base-ref.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Did you thought about making this part of our gradle itself. Not that I'm asking for it specifically. As I would like to avoid adding yet another groovy scripting at this time.

Also, I wonder does our gitlab CI have this resolved ?

cc @randomanderson

Copy link
Member Author

@smola smola Jun 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you thought about making this part of our gradle itself.

Can be done at gradle level. A bash script for CI seemed pretty self-contained and easily debuggable though.

Also, I wonder does our gitlab CI have this resolved ?

Not supported by GitLab, because GitHub PRs are not native to GitLab CI, and we do not use GitLab MR. We have an internal solution at DD that requires migrating to a new CI system for GitLab. That's apparently a lot of work for the repo since it was not used since the beginning.

In our current setup, this is the recommended way. I took it from dd-trace-py.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thanks for the info !

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env bash
# Determines the base branch for the current PR (if we are running in a PR).
set -euo pipefail

# Happy path: if we're just one commit away from master, base ref is master.
if [[ $(git log --pretty=oneline origin/master..HEAD | wc -l) -eq 1 ]]; then
echo "We are just one commit away from master, base ref is master" >&2
echo "master"
exit 0
fi

# In GitLab: we have no reference to the base branch or even the PR number.
# We have to find it from the current branch name, which is defined in
# CI_COMMIT_REF_NAME.
if [[ -z "${CI_COMMIT_REF_NAME}" ]]; then
echo "CI_COMMIT_REF_NAME is not set, not running in GitLab CI?" >&2
exit 1
fi

if [[ -z "${GITHUB_TOKEN:-}" ]]; then
echo "GITHUB_TOKEN is not set, fetching from AWS SSM" >&2
if ! command -v aws >/dev/null 2>&1; then
echo "aws is not installed, please install it" >&2
exit 1
fi
GITHUB_TOKEN=$(aws ssm get-parameter --name "ci.$CI_PROJECT_NAME.gh_release_token" --with-decryption --query "Parameter.Value" --output text)
if [[ -z "${GITHUB_TOKEN}" ]]; then
echo "Failed to fetch GITHUB_TOKEN from AWS SSM" >&2
exit 1
fi
export GITHUB_TOKEN
fi

if ! command -v curl >/dev/null 2>&1; then
echo "curl is not installed, please install it" >&2
exit 1
fi

if ! command -v jq >/dev/null 2>&1; then
echo "jq is not installed, please install it" >&2
exit 1
fi

while true; do
set +e
PR_DATA=$(curl \
-XGET \
--silent \
--include \
--fail-with-body \
-H 'Accept: application/vnd.github+json' \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/datadog/dd-trace-java/pulls?head=DataDog:${CI_COMMIT_REF_NAME}&sort=updated&direction=desc")
exit_code=$?
set -e
if [[ ${exit_code} -eq 0 ]]; then
PR_NUMBER=$(echo "$PR_DATA" | sed '1,/^[[:space:]]*$/d' | jq -r '.[].number')
PR_BASE_REF=$(echo "$PR_DATA" | sed '1,/^[[:space:]]*$/d' | jq -r '.[].base.ref')
echo "PR is https://github.com/datadog/dd-trace-java/pull/${PR_NUMBER} and base ref is ${PR_BASE_REF}">&2
echo "${PR_BASE_REF}"
exit 0
fi
if echo "$PR_DATA" | grep -q "^x-ratelimit-reset:"; then
reset_timestamp=$(echo -n "$PR_DATA" | grep "^x-ratelimit-reset:" | sed -e 's/^x-ratelimit-reset: //' -e 's/\r//')
now=$(date +%s)
sleep_time=$((reset_timestamp - now + 1))
echo "GitHub rate limit exceeded, sleeping for ${sleep_time} seconds" >&2
sleep "${sleep_time}"
continue
fi
echo -e "GitHub request failed for an unknown reason:\n$(echo "$PR_DATA" | sed '/^$/q')" >&2
echo "Assuming base ref is master" >&2
echo "master"
exit 0
done