|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Unless explicitly stated otherwise all files in this repository are licensed |
| 4 | +# under the Apache License Version 2.0. |
| 5 | +# This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 6 | +# Copyright 2024 Datadog, Inc. |
| 7 | + |
| 8 | +set -e |
| 9 | + |
| 10 | +LAYER_NAME="dd-trace-java" |
| 11 | + |
| 12 | +publish_layer() { |
| 13 | + region=$1 |
| 14 | + |
| 15 | + version_nbr=$(aws lambda publish-layer-version --layer-name $LAYER_NAME \ |
| 16 | + --description "Datadog Tracer Lambda Layer for Java" \ |
| 17 | + --compatible-runtimes "java8" "java11" "java17" "java21" \ |
| 18 | + --compatible-architectures "x86_64" "arm64" \ |
| 19 | + --zip-file "fileb://.layers/tracer.zip" \ |
| 20 | + --region $region \ |
| 21 | + | jq -r '.Version') |
| 22 | + |
| 23 | + # Add permissions only for prod |
| 24 | + if [ "$STAGE" == "prod" ]; then |
| 25 | + permission=$(aws lambda add-layer-version-permission --layer-name $LAYER_NAME \ |
| 26 | + --version-number $version_nbr \ |
| 27 | + --statement-id "release-$version_nbr" \ |
| 28 | + --action lambda:GetLayerVersion \ |
| 29 | + --principal "*" \ |
| 30 | + --region $region |
| 31 | + ) |
| 32 | + fi |
| 33 | + |
| 34 | + echo $version_nbr |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +if [ ! -f ".layers/tracer.zip" ]; then |
| 39 | + printf "[ERROR]: Could not find .layers/tracer.zip." |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | +AVAILABLE_REGIONS=$(aws ec2 describe-regions | jq -r '.[] | .[] | .RegionName') |
| 44 | + |
| 45 | +if [ -z "$REGION" ]; then |
| 46 | + printf "[ERROR]: REGION not specified." |
| 47 | + exit 1 |
| 48 | +else |
| 49 | + printf "Region specified: $REGION\n" |
| 50 | + if [[ ! "$AVAILABLE_REGIONS" == *"$REGION"* ]]; then |
| 51 | + printf "Could not find $REGION in available regions: $AVAILABLE_REGIONS" |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | +fi |
| 55 | + |
| 56 | +if [ -z "$STAGE" ]; then |
| 57 | + printf "[ERROR]: STAGE not specified.\n" |
| 58 | + exit 1 |
| 59 | +fi |
| 60 | + |
| 61 | +printf "[$REGION] Starting publishing layers...\n" |
| 62 | + |
| 63 | +if [[ "$STAGE" =~ ^(staging|sandbox)$ ]]; then |
| 64 | + # Deploy latest version |
| 65 | + latest_version=$(aws lambda list-layer-versions --region $REGION --layer-name $LAYER_NAME --query 'LayerVersions[0].Version || `0`') |
| 66 | + VERSION=$(($latest_version + 1)) |
| 67 | +else |
| 68 | + # Running on prod |
| 69 | + if [ -z "$CI_COMMIT_TAG" ]; then |
| 70 | + printf "[ERROR]: No CI_COMMIT_TAG found.\n" |
| 71 | + printf "Exiting script...\n" |
| 72 | + exit 1 |
| 73 | + else |
| 74 | + printf "Tag found in environment: $CI_COMMIT_TAG\n" |
| 75 | + fi |
| 76 | + |
| 77 | + VERSION="${CI_COMMIT_TAG//[!0-9]/}" |
| 78 | + printf "Version: ${VERSION}\n" |
| 79 | +fi |
| 80 | + |
| 81 | +if [ -z "$VERSION" ]; then |
| 82 | + printf "[ERROR]: Layer VERSION not specified" |
| 83 | + exit 1 |
| 84 | +else |
| 85 | + printf "Layer version parsed: $VERSION\n" |
| 86 | +fi |
| 87 | + |
| 88 | +latest_version=$(aws lambda list-layer-versions --region $REGION --layer-name $LAYER_NAME --query 'LayerVersions[0].Version || `0`') |
| 89 | +if [ $latest_version -ge $VERSION ]; then |
| 90 | + printf "[$REGION] Layer $LAYER_NAME version $VERSION already exists in region $REGION, skipping...\n" |
| 91 | + exit 1 |
| 92 | +elif [ $latest_version -lt $((VERSION-1)) ]; then |
| 93 | + printf "[$REGION][WARNING] The latest version of layer $LAYER_NAME in region $REGION is $latest_version, this will publish all the missing versions including $VERSION\n" |
| 94 | +fi |
| 95 | + |
| 96 | +while [ $latest_version -lt $VERSION ]; do |
| 97 | + latest_version=$(publish_layer $REGION) |
| 98 | + printf "[$REGION] Published version $latest_version of layer $LAYER_NAME in region $REGION\n" |
| 99 | + |
| 100 | + # This shouldn't happen unless someone manually deleted the latest version, say 28, and |
| 101 | + # then tries to republish 28 again. The published version would actually be 29, because |
| 102 | + # Lambda layers are immutable and AWS will skip deleted version and use the next number. |
| 103 | + if [ $latest_version -gt $VERSION ]; then |
| 104 | + printf "[$REGION] Published version $latest_version is greater than the desired version $VERSION!" |
| 105 | + exit 1 |
| 106 | + fi |
| 107 | +done |
| 108 | + |
| 109 | +printf "[$REGION] Finished publishing layer...\n\n" |
0 commit comments