Skip to content

Commit d262f72

Browse files
authored
ci: publish docker image for system tests (#4013)
Adds a CI job at dd-trace-php/.gitlab/generate-package.php which pushes the build to github's container registry (similarly to what already exists for prod builds). This is necessary to enable LIBRARY_TARGET_BRANCH to work for the PHP target in utils/scripts/load-binary.sh. * add new job to publish CI builds to ghcr.io * fix base image name * publish in two steps * fix(ci): fix token lifecycle and mask github token in system tests publish jobs * restrict CI uploads to Datadog/dd-trace-php * cleanup github token handling * add policy file * add a workflow to delete pr images when the pr is closed * delete image with same tag before pushing * prevent two uploading jobs from running concurrently, skip uploading images them in the default branch * upload image with builds for all archs * Gate upload to having a PR open
1 parent 9fa0094 commit d262f72

3 files changed

Lines changed: 151 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
issuer: https://gitlab.ddbuild.io
2+
3+
subject_pattern: "project_path:DataDog/apm-reliability/dd-trace-php:ref_type:(branch|tag):ref:.*"
4+
5+
permissions:
6+
packages: write
7+
pull_requests: read
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: "Delete Docker image on PR close"
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
7+
jobs:
8+
delete-pr-image:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
packages: write # required to delete GHCR container versions
12+
contents: read
13+
14+
steps:
15+
- name: Sanitize branch name to match image tag (CI_COMMIT_REF_SLUG from gitlab)
16+
id: sanitize
17+
env:
18+
PR_REF: ${{ github.event.pull_request.head.ref }}
19+
run: | # Match GitLab's CI_COMMIT_REF_SLUG: lowercase, non-alphanumeric → '-', collapse and trim
20+
TAG=$(echo -n "$PR_REF" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9]+/-/g' | cut -c1-63 | sed -E 's/^-+//; s/-+$//')
21+
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
22+
23+
- name: Delete image by tag from GHCR
24+
env:
25+
CI_COMMIT_REF_SLUG: ${{ steps.sanitize.outputs.tag }}
26+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27+
run: |
28+
gh api "orgs/DataDog/packages/container/dd-trace-php%2Fdd-library-php/versions" \
29+
--jq ".[] | select(.metadata.container.tags[]? == \"${CI_COMMIT_REF_SLUG}\") | .id" \
30+
| xargs -r -I {} gh api "orgs/DataDog/packages/container/dd-trace-php%2Fdd-library-php/versions/{}" -X DELETE
31+
echo "Deleted version with tag: ${CI_COMMIT_REF_SLUG}"

.gitlab/generate-package.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,119 @@
15361536
paths:
15371537
- packages/datadog-setup.php
15381538

1539+
# Runs on every non-default branch so system tests can be run against any (non-default) in-progress branch.
1540+
"publish docker image for system tests (token)":
1541+
stage: release
1542+
image: registry.ddbuild.io/images/dd-octo-sts-ci-base:2025.06-1
1543+
tags: [ "arch:amd64" ]
1544+
id_tokens:
1545+
DDOCTOSTS_ID_TOKEN:
1546+
aud: dd-octo-sts
1547+
rules:
1548+
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
1549+
when: never
1550+
- when: on_success
1551+
variables:
1552+
GIT_STRATEGY: none
1553+
script:
1554+
- dd-octo-sts token --scope DataDog/dd-trace-php --policy gitlab-ci-publish-packages > github_token_system_tests.txt
1555+
artifacts:
1556+
paths:
1557+
- github_token_system_tests.txt
1558+
expire_in: 1 hour
1559+
when: on_success
1560+
1561+
"publish docker image for system tests":
1562+
stage: release
1563+
image: 486234852809.dkr.ecr.us-east-1.amazonaws.com/docker:29.4.0-noble
1564+
tags: [ "docker-in-docker:amd64" ]
1565+
resource_group: "publish-system-tests-image-${CI_COMMIT_REF_SLUG}"
1566+
interruptible: true
1567+
rules:
1568+
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
1569+
when: never
1570+
- when: on_success
1571+
needs:
1572+
- job: "publish docker image for system tests (token)"
1573+
artifacts: true
1574+
- job: "datadog-setup.php"
1575+
artifacts: true
1576+
- job: "package extension: [amd64, x86_64-unknown-linux-gnu]"
1577+
artifacts: true
1578+
- job: "package extension: [arm64, aarch64-unknown-linux-gnu]"
1579+
artifacts: true
1580+
variables:
1581+
GIT_STRATEGY: none
1582+
allow_failure:
1583+
exit_codes: 3
1584+
script: |
1585+
set -e
1586+
IMAGE="ghcr.io/datadog/dd-trace-php/dd-library-php:${CI_COMMIT_REF_SLUG}"
1587+
VERSIONS_URL="https://api.github.com/orgs/DataDog/packages/container/dd-trace-php%2Fdd-library-php/versions"
1588+
GITHUB_TOKEN=$(<github_token_system_tests.txt)
1589+
1590+
apt-get update -qq && apt-get install -y -qq curl jq > /dev/null
1591+
1592+
STATUS=$(curl -s -o /tmp/pulls.json -w "%{http_code}" -H "Authorization: Bearer ${GITHUB_TOKEN}" -H "Accept: application/vnd.github+json" \
1593+
"https://api.github.com/repos/DataDog/dd-trace-php/pulls?head=DataDog:${CI_COMMIT_BRANCH}&state=open")
1594+
if [ "${STATUS}" != "200" ]; then
1595+
echo "ERROR: failed to check for an open PR on branch ${CI_COMMIT_BRANCH} (HTTP ${STATUS}):"; cat /tmp/pulls.json
1596+
exit 1
1597+
fi
1598+
PR_COUNT=$(jq 'length' /tmp/pulls.json)
1599+
if [ "${PR_COUNT}" -eq 0 ]; then
1600+
echo "No open PR for branch ${CI_COMMIT_BRANCH}; skipping system-tests image publish."
1601+
exit 3
1602+
fi
1603+
echo "Found open PR(s) on branch ${CI_COMMIT_BRANCH}, continuing"
1604+
1605+
docker login ghcr.io -u DataDog --password-stdin < github_token_system_tests.txt
1606+
echo "Docker login to ghcr.io succeeded"
1607+
1608+
gh_api() { curl -s -o /tmp/resp.json -w "%{http_code}" -X "$1" \
1609+
-H "Authorization: Bearer ${GITHUB_TOKEN}" -H "Accept: application/vnd.github+json" "$2"; }
1610+
1611+
# delete any pre-existing image with the same tag so pushing with the same tag doesn't pile up untagged versions.
1612+
echo "== Checking token and existing image tagged ${CI_COMMIT_REF_SLUG} =="
1613+
STATUS=$(gh_api GET "$VERSIONS_URL")
1614+
if [ "$STATUS" != "200" ]; then
1615+
echo "WARNING: token check failed (HTTP ${STATUS}), skipping pre-delete:"; cat /tmp/resp.json
1616+
else
1617+
echo "Token OK (HTTP 200): listed package versions for dd-trace-php/dd-library-php"
1618+
VERSION_ID=$(jq -r --arg TAG "${CI_COMMIT_REF_SLUG}" \
1619+
'.[] | select(.metadata.container.tags[]? == $TAG) | .id' /tmp/resp.json | head -n1)
1620+
if [ -z "$VERSION_ID" ]; then
1621+
echo "No pre-existing image tagged ${CI_COMMIT_REF_SLUG} found, nothing to delete"
1622+
else
1623+
STATUS=$(gh_api DELETE "${VERSIONS_URL}/${VERSION_ID}")
1624+
if [ "$STATUS" = "204" ]; then
1625+
echo "Deleted existing image version ${VERSION_ID} (HTTP 204)"
1626+
else
1627+
echo "WARNING: failed to delete existing image version ${VERSION_ID} (HTTP ${STATUS}), continuing anyway:"
1628+
cat /tmp/resp.json
1629+
fi
1630+
fi
1631+
fi
1632+
1633+
echo "== Building and pushing ${IMAGE} (linux/amd64, linux/arm64) =="
1634+
# stage each arch's tarball under its own dir so the Dockerfile can pick the right one via buildx's TARGETARCH arg.
1635+
mkdir -p packages/amd64 packages/arm64
1636+
cp packages/dd-library-php-*-x86_64-linux-gnu.tar.gz packages/amd64/
1637+
cp packages/dd-library-php-*-aarch64-linux-gnu.tar.gz packages/arm64/
1638+
cp packages/datadog-setup.php packages/amd64/
1639+
cp packages/datadog-setup.php packages/arm64/
1640+
1641+
printf 'FROM scratch\nARG TARGETARCH\nCOPY packages/${TARGETARCH}/dd-library-php-*-linux-gnu.tar.gz /\nCOPY packages/${TARGETARCH}/datadog-setup.php /\n' \
1642+
> Dockerfile.system-tests
1643+
BUILDER="system-tests-builder-${CI_JOB_ID}"
1644+
docker buildx create --name "$BUILDER" --driver docker-container
1645+
docker buildx build --builder "$BUILDER" --platform linux/amd64,linux/arm64 \
1646+
-f Dockerfile.system-tests -t "$IMAGE" --push .
1647+
echo "Pushed $IMAGE"
1648+
after_script:
1649+
- rm -f github_token_system_tests.txt
1650+
- docker buildx rm "system-tests-builder-${CI_JOB_ID}" || true
1651+
15391652
"bundle for reliability env":
15401653
stage: shared-pipeline
15411654
image: registry.ddbuild.io/ci/libdatadog-build/ci_docker_base:67145216

0 commit comments

Comments
 (0)