Skip to content

Commit

Permalink
release: use tag version for docker build/push (#3600)
Browse files Browse the repository at this point in the history
  • Loading branch information
v1v authored Apr 29, 2024
1 parent 30601a1 commit 21a8cd0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 50 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ jobs:
runs-on: ubuntu-latest
needs:
- await-maven-central-artifact
- create-github-release
env:
SONATYPE_FALLBACK: 1
steps:
Expand All @@ -143,11 +144,11 @@ jobs:
secretId: ${{ secrets.VAULT_SECRET_ID }}
- name: "Build docker image"
shell: bash
run: ./scripts/docker-release/build_docker.sh
run: ./scripts/docker-release/build_docker.sh "${{ env.RELEASE_VERSION }}"
- name: "Push docker image"
if: ${{ ! inputs.dry_run }}
shell: bash
run: ./scripts/docker-release/push_docker.sh
run: ./scripts/docker-release/push_docker.sh "${{ env.RELEASE_VERSION }}"

publish-aws-lambda:
name: "Publish AWS Lambda"
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ If you wish to use a locally built artifact in the built image, execute [`./mvnw
and ensure that artifacts are present in `elastic-apm-agent/target/*.jar`.
To create a Docker image from artifacts generated by [`./mvnw package`](mvnw),
run [`scripts/docker-release/build_docker.sh`](scripts/docker-release/build_docker.sh).
run [`scripts/docker-release/build_docker.sh`](scripts/docker-release/build_docker.sh) with the release version.
Alternatively, it is also possible to use the most recent artifact from the [Sonatype
repository](https://oss.sonatype.org/#nexus-search;gav~co.elastic.apm~apm-agent-java~~~).
Expand Down Expand Up @@ -383,4 +383,4 @@ Prior to pushing images, you must login to the Elastic Docker repo using the cor
credentials using the [`docker login`](https://docs.docker.com/engine/reference/commandline/login/) command.
To push an image, run the [`scripts/docker-release/push_docker.sh`](scripts/docker-release/push_docker.sh)
script. An image will be pushed.
script with the release version. An image will be pushed.
21 changes: 5 additions & 16 deletions scripts/docker-release/build_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,7 @@ elif ! docker version
then
echo "ERROR: Building Docker image requires Docker daemon to be running" && exit 1
fi

echo "INFO: Determining latest tag"
if [ ! -z ${TAG_NAME+x} ]
then
echo "INFO: Detected TAG_NAME variable. Probably a Jenkins instance."
readonly GIT_TAG_DEFAULT=$(echo $TAG_NAME|sed s/^v//)
else
echo "INFO: Did not detect TAG_NAME. Examining git log for latest tag"
readonly GIT_TAG_DEFAULT=$(git describe --abbrev=0|sed s/^v//)
fi

readonly GIT_TAG=${GIT_TAG:-$GIT_TAG_DEFAULT}
readonly RELEASE_VERSION=${1}

readonly SCRIPT_PATH="$( cd "$(dirname "$0")" ; pwd -P )"
readonly PROJECT_ROOT=$SCRIPT_PATH/../../
Expand All @@ -36,21 +25,21 @@ then
find -E ${PROJECT_ROOT}elastic-apm-agent/target -regex '.*/elastic-apm-agent-[0-9]+.[0-9]+.[0-9]+(-SNAPSHOT)?.jar' -exec cp {} ${PROJECT_ROOT}apm-agent-java.jar \; || echo "INFO: No locally built image found"
elif [ ! -z ${SONATYPE_FALLBACK+x} ]
then
echo "INFO: No local build artifact and SONATYPE_FALLBACK. Falling back to downloading artifact from Sonatype Nexus repository for version $GIT_TAG"
echo "INFO: No local build artifact and SONATYPE_FALLBACK. Falling back to downloading artifact from Sonatype Nexus repository for version $RELEASE_VERSION"
if ! command -v curl
then
echo "ERROR: Pulling images from Sonatype Nexus repo requires cURL to be installed" && exit 1
fi
curl -L -s -o apm-agent-java.jar \
"https://oss.sonatype.org/service/local/artifact/maven/redirect?r=releases&g=co.elastic.apm&a=elastic-apm-agent&v=$GIT_TAG"
"https://oss.sonatype.org/service/local/artifact/maven/redirect?r=releases&g=co.elastic.apm&a=elastic-apm-agent&v=$RELEASE_VERSION"
else
echo "ERROR: No suitable build artifact was found. Re-running this script with the SONATYPE_FALLBACK variable set to true will try to use the Sonatype artifact for the latest tag"
exit 1
fi

echo "INFO: Starting Docker build for version $GIT_TAG"
echo "INFO: Starting Docker build for version $RELEASE_VERSION"

docker build -t docker.elastic.co/$NAMESPACE/apm-agent-java:$GIT_TAG \
docker build -t docker.elastic.co/$NAMESPACE/apm-agent-java:$RELEASE_VERSION \
--build-arg JAR_FILE=apm-agent-java.jar \
--build-arg HANDLER_FILE=apm-agent-lambda-layer/src/main/assembly/elastic-apm-handler .

Expand Down
36 changes: 6 additions & 30 deletions scripts/docker-release/push_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,24 @@ readonly RETRIES=3
# creating a Docker image to push. If this script does not detect an image
# to be uploaded, it will fail.

# This script is intended to be run from a CI job and will not work if run in
# standalone manner unless certain envrionment variables are set.

# Grab the tag we are working with

echo "INFO: Determining latest tag"
if [ ! -z ${TAG_NAME+x} ]
then
echo "INFO: Detected TAG_NAME variable. Probably a Jenkins instance."
readonly GIT_TAG_DEFAULT=$(echo $TAG_NAME|sed s/^v//)
else
echo "INFO: Did not detect TAG_NAME. Examining git log for latest tag"
readonly GIT_TAG_DEFAULT=$(git describe --abbrev=0|sed s/^v//)
fi

readonly CUR_TAG=${CUR_TAG:-$GIT_TAG_DEFAULT}
readonly RELEASE_VERSION=${1}
readonly DOCKER_REGISTRY_URL="docker.elastic.co"
readonly DOCKER_IMAGE_NAME="observability/apm-agent-java"
readonly DOCKER_PUSH_IMAGE="$DOCKER_REGISTRY_URL/$DOCKER_IMAGE_NAME:$CUR_TAG"
readonly DOCKER_PUSH_IMAGE="$DOCKER_REGISTRY_URL/$DOCKER_IMAGE_NAME:$RELEASE_VERSION"
readonly DOCKER_PUSH_IMAGE_LATEST="$DOCKER_REGISTRY_URL/$DOCKER_IMAGE_NAME:latest"

# Proceed with pushing to the registry
echo "INFO: Pushing image $DOCKER_PUSH_IMAGE to $DOCKER_REGISTRY_URL"

if [ ${WORKERS+x} ] # We are on a CI worker
then
retry $RETRIES docker push $DOCKER_PUSH_IMAGE || echo "Push failed after $RETRIES retries"
else # We are in a local (non-CI) environment
docker push $DOCKER_PUSH_IMAGE || { echo "You may need to run 'docker login' first and then re-run this script"; exit 1; }
fi
docker push $DOCKER_PUSH_IMAGE || { echo "You may need to run 'docker login' first and then re-run this script"; exit 1; }

readonly LATEST_TAG=$(git tag --list --sort=version:refname "v*" | grep -v RC | sed s/^v// | tail -n 1)

if [ "$CUR_TAG" = "$LATEST_TAG" ]
if [ "$RELEASE_VERSION" = "$LATEST_TAG" ]
then
echo "INFO: Current version ($CUR_TAG) is the latest version. Tagging and pushing $DOCKER_PUSH_IMAGE_LATEST ..."
echo "INFO: Current version ($RELEASE_VERSION) is the latest version. Tagging and pushing $DOCKER_PUSH_IMAGE_LATEST ..."
docker tag $DOCKER_PUSH_IMAGE $DOCKER_PUSH_IMAGE_LATEST

if [ ${WORKERS+x} ] # We are on a CI worker
then
retry $RETRIES docker push $DOCKER_PUSH_IMAGE_LATEST || echo "Push failed after $RETRIES retries"
else # We are in a local (non-CI) environment
docker push $DOCKER_PUSH_IMAGE_LATEST || { echo "You may need to run 'docker login' first and then re-run this script"; exit 1; }
fi
docker push $DOCKER_PUSH_IMAGE_LATEST || { echo "You may need to run 'docker login' first and then re-run this script"; exit 1; }
fi

0 comments on commit 21a8cd0

Please sign in to comment.