Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update lib-injection docker image tags #7057

Merged
merged 8 commits into from
May 31, 2024
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
44 changes: 19 additions & 25 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -213,47 +213,41 @@ deploy_artifacts_to_github:
- gh release upload --clobber --repo DataDog/dd-trace-java $CI_COMMIT_TAG workspace/dd-java-agent/build/libs/*.jar
- gh release upload --clobber --repo DataDog/dd-trace-java $CI_COMMIT_TAG workspace/dd-trace-api/build/libs/*.jar
- gh release upload --clobber --repo DataDog/dd-trace-java $CI_COMMIT_TAG workspace/dd-trace-ot/build/libs/*.jar

deploy_to_docker_registries:

generate-lib-init-tag-values:
tags: ["arch:amd64"]
image: registry.ddbuild.io/ci/auto_inject/gitlab:current
stage: deploy
rules:
- if: '$CI_COMMIT_TAG =~ /.*-[Rr][Cc].*/'
when: manual
allow_failure: true
- if: '$POPULATE_CACHE'
when: never
- if: '$CI_COMMIT_TAG =~ /^v.*/'
# We don't tag prerelease versions
- if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/'
Comment on lines +224 to +225
Copy link
Contributor

Choose a reason for hiding this comment

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

About RC, I don't know who are using them, and where they are getting them from?
@am312 Can you provide more info about them?

when: on_success
- when: manual
allow_failure: true
trigger:
project: DataDog/public-images
branch: main
strategy: depend
variables:
IMG_SOURCES: ghcr.io/datadog/dd-trace-java/dd-lib-java-init:$CI_COMMIT_SHA
IMG_DESTINATIONS: dd-lib-java-init:$CI_COMMIT_TAG
IMG_SIGNING: "false"
IMG_DESTINATION_BASE: dd-lib-java-init
script:
- .gitlab/build-lib-init.sh
artifacts:
reports:
dotenv: build.env

deploy_latest_tag_to_docker_registries:
deploy-lib-init-trigger:
stage: deploy
rules:
- if: '$CI_COMMIT_TAG =~ /.*-[Rr][Cc].*/'
when: manual
allow_failure: true
- if: '$POPULATE_CACHE'
when: never
- if: '$CI_COMMIT_TAG =~ /^v1\..*/'
when: on_success
- when: manual
allow_failure: true
# needs the version from the generate-tag-values job
needs:
- job: generate-lib-init-tag-values
Comment on lines +239 to +241
Copy link
Contributor

Choose a reason for hiding this comment

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

Why split generate-lib-init-tag-values and deploy-lib-init-trigger into two separate jobs?

Copy link
Contributor

Choose a reason for hiding this comment

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

Trigger jobs can't have any script blocks

Copy link
Member Author

Choose a reason for hiding this comment

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

Because I think we have to due to the way GitLab works with trigger jobs I think. I could be wrong though, I suck at gitlab 😅 FWIW, other languages are using this script, so it works even if it's not optimal

artifacts: true
trigger:
# project: DataDog/dd-trace-dotnet-gitlab-test # can be used for testing
project: DataDog/public-images
branch: main
strategy: depend
variables:
IMG_SOURCES: ghcr.io/datadog/dd-trace-java/dd-lib-java-init:$CI_COMMIT_SHA
IMG_DESTINATIONS: dd-lib-java-init:latest
IMG_DESTINATIONS: $IMG_DESTINATIONS
IMG_SIGNING: "false"

create_key:
Expand Down
116 changes: 116 additions & 0 deletions .gitlab/build-lib-init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/bin/bash

set -e

# Safety checks to make sure we have required values
if [ -z "$CI_COMMIT_TAG" ]; then
echo "Error: CI_COMMIT_TAG was not provided"
exit 1
fi

if [ -z "$CI_COMMIT_SHA" ]; then
echo "Error: CI_COMMIT_SHA was not provided"
exit 1
fi

if [ -z "$IMG_DESTINATION_BASE" ]; then
echo "Error: IMG_DESTINATION_BASE. This should be set to the destination docker image, excluding the tag name, e.g. dd-lib-dotnet-init"
exit 1
fi

# If this is a pre-release release, we don't publish
if echo "$CI_COMMIT_TAG" | grep -q "-" > /dev/null; then
echo "Error: This is a pre-release version, should not publish images: $CI_COMMIT_TAG"
exit 1
fi

# Calculate the tags we use for floating major and minor versions
MAJOR_MINOR_VERSION="$(sed -nE 's/^(v[0-9]+\.[0-9]+)\.[0-9]+$/\1/p' <<< ${CI_COMMIT_TAG})"
MAJOR_VERSION="$(sed -nE 's/^(v[0-9]+)\.[0-9]+\.[0-9]+$/\1/p' <<< ${CI_COMMIT_TAG})"

# Make sure we have all the tags
git fetch --tags

# We need to determine whether this is is the latest tag and whether it's the latest major or not
# So we fetch all tags and sort them to find both the latest, and the latest in this major.
# 'sort' technically gets prerelease versions in the wrong order here, but we explicitly
# exclude them anyway, as they're ignored for the purposes of determining the 'latest' tags.
LATEST_TAG="$(git tag | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -V -r | head -n 1)"
LATEST_MAJOR_TAG="$(git tag -l "$MAJOR_VERSION.*" | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -V -r | head -n 1)"
echo "This tag: $CI_COMMIT_TAG"
echo "Latest repository tag: $LATEST_TAG"
echo "Latest repository tag for this major: $LATEST_MAJOR_TAG"
echo "---------"

# GNU sort -C (silent) reports via exit code whether the data is already in sorted order
# We use this to check whether the current tag is greater than (or equal to) the latest tag
if printf '%s\n' "$LATEST_TAG" "$CI_COMMIT_TAG" | sort -C -V; then
# The current tag is the latest in the repository
IS_LATEST_TAG=1
else
IS_LATEST_TAG=0
fi

if printf '%s\n' "$LATEST_MAJOR_TAG" "$CI_COMMIT_TAG" | sort -C -V; then
# The current tag is the latest for this major version in the repository
IS_LATEST_MAJOR_TAG=1
else
IS_LATEST_MAJOR_TAG=0
fi

# print everything for debugging purposes
echo "Calculated values:"
echo "MAJOR_MINOR_VERSION=${MAJOR_MINOR_VERSION}"
echo "MAJOR_VERSION=${MAJOR_VERSION}"
echo "IS_LATEST_TAG=${IS_LATEST_TAG}"
echo "IS_LATEST_MAJOR_TAG=${IS_LATEST_MAJOR_TAG}"
echo "---------"

# Final check that everything is ok
# We should have a major_minor version
if [ -z "$MAJOR_MINOR_VERSION" ]; then
echo "Error: Could not determine major_minor version for stable release, this should not happen"
exit 1
fi

# if this is a latest major tag, we should have a major version
if [ "$IS_LATEST_MAJOR_TAG" -eq 1 ] && [ -z "$MAJOR_VERSION" ]; then
echo "Error: Could not determine major version for latest major release, this should not happen"
exit 1
fi

# Generate the final variables, and save them into build.env so they can be read by the trigger job
set_image_tags() {
SUFFIX="$1"
VARIABLE_SUFFIX="${SUFFIX:+_$SUFFIX}" # add a '_' prefix
TAG_SUFFIX="${SUFFIX:+-$SUFFIX}" # add a '-' prefix

# We always add this tag, regardless of the version
DESTINATIONS="${IMG_DESTINATION_BASE}:${CI_COMMIT_TAG}${TAG_SUFFIX}"

# We always add the major_minor tag (we never release 2.5.2 _after_ 2.5.3, for example)
DESTINATIONS="${DESTINATIONS},${IMG_DESTINATION_BASE}:${MAJOR_MINOR_VERSION}${TAG_SUFFIX}"

# Only latest-major releases get the major tag
if [ "$IS_LATEST_MAJOR_TAG" -eq 1 ]; then
DESTINATIONS="${DESTINATIONS},${IMG_DESTINATION_BASE}:${MAJOR_VERSION}${TAG_SUFFIX}"
fi

# Only latest releases get the latest tag
if [ "$IS_LATEST_TAG" -eq 1 ]; then
DESTINATIONS="${DESTINATIONS},${IMG_DESTINATION_BASE}:latest${TAG_SUFFIX}"
fi

# Save the value to the build.env file
echo "IMG_DESTINATIONS${VARIABLE_SUFFIX}=${DESTINATIONS}"
echo "IMG_DESTINATIONS${VARIABLE_SUFFIX}=${DESTINATIONS}" >> build.env
}

# Calculate the non-suffixed tags
set_image_tags

# For each suffix, calculate the tags
for ADDITIONAL_TAG_SUFFIX in ${ADDITIONAL_TAG_SUFFIXES//,/ }
do
set_image_tags "$ADDITIONAL_TAG_SUFFIX"
done
Loading