|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Post-PR-merge operations for a patch version bump. |
| 5 | +# Runs after the bump PR is merged and DRA artifacts for NEW_VERSION are confirmed. |
| 6 | +# |
| 7 | +# Required env vars from PSI trigger params: |
| 8 | +# BRANCH — release branch (e.g. 9.3) |
| 9 | +# NEW_VERSION — the version that was bumped to (e.g. 9.3.4) |
| 10 | +# REPO — repository name (e.g. elastic/cloudbeat) |
| 11 | +# WORKFLOW — must be "patch" |
| 12 | +# |
| 13 | +# Secrets (from kv/ci-shared/cloudbeat/* via vault — wired in task-7): |
| 14 | +# AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY — for CFT upload to S3 |
| 15 | +# SNYK_ORG_ID, SNYK_API_KEY, SNYK_INTEGRATION_ID — for Snyk branch monitoring |
| 16 | +: "${BRANCH:?BRANCH is required}" |
| 17 | +: "${NEW_VERSION:?NEW_VERSION is required}" |
| 18 | +: "${REPO:?REPO is required}" |
| 19 | +: "${WORKFLOW:?WORKFLOW is required}" |
| 20 | + |
| 21 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 22 | +# shellcheck source=common.sh |
| 23 | +source "${SCRIPT_DIR}/common.sh" |
| 24 | + |
| 25 | +echo "--- Post-bump ops for ${NEW_VERSION}" |
| 26 | + |
| 27 | +upload_cloud_formation_templates() { |
| 28 | + echo "--- Upload CloudFormation templates for ${NEW_VERSION}" |
| 29 | + aws configure set aws_access_key_id "${AWS_ACCESS_KEY_ID}" |
| 30 | + aws configure set aws_secret_access_key "${AWS_SECRET_ACCESS_KEY}" |
| 31 | + aws configure set region us-east-2 |
| 32 | + scripts/publish_cft.sh |
| 33 | +} |
| 34 | + |
| 35 | +bump_snyk_branch_monitoring() { |
| 36 | + echo "--- Update Snyk branch monitoring" |
| 37 | + local branches latest_major previous_major latest_major_latest_minor previous_major_latest_minor |
| 38 | + |
| 39 | + branches=$(git branch -r | grep -Eo '[0-9]+\.[0-9]+' | sort -V | uniq) |
| 40 | + latest_major=$(echo "${branches}" | cut -d. -f1 | uniq | tail -1) |
| 41 | + previous_major=$(echo "${branches}" | cut -d. -f1 | uniq | tail -2 | head -1) |
| 42 | + latest_major_latest_minor=$(echo "${branches}" | grep -E "^${latest_major}\." | tail -1) |
| 43 | + previous_major_latest_minor=$(echo "${branches}" | grep -E "^${previous_major}\." | tail -1) |
| 44 | + |
| 45 | + echo " latest: ${latest_major_latest_minor}" |
| 46 | + echo " previous: ${previous_major_latest_minor}" |
| 47 | + |
| 48 | + local cloudbeat_id |
| 49 | + cloudbeat_id=$(curl -sf -X GET \ |
| 50 | + "https://api.snyk.io/rest/orgs/${SNYK_ORG_ID}/targets?version=2024-05-23&display_name=cloudbeat" \ |
| 51 | + -H "accept: application/vnd.api+json" \ |
| 52 | + -H "authorization: ${SNYK_API_KEY}" | jq -r '.data[0].id') |
| 53 | + |
| 54 | + curl -sf -X DELETE \ |
| 55 | + "https://api.snyk.io/rest/orgs/${SNYK_ORG_ID}/targets/${cloudbeat_id}?version=2024-05-23" \ |
| 56 | + -H "accept: application/vnd.api+json" \ |
| 57 | + -H "authorization: ${SNYK_API_KEY}" |
| 58 | + |
| 59 | + for branch in main "${latest_major_latest_minor}" "${previous_major_latest_minor}"; do |
| 60 | + curl -sf -X POST \ |
| 61 | + "https://api.snyk.io/v1/org/${SNYK_ORG_ID}/integrations/${SNYK_INTEGRATION_ID}/import" \ |
| 62 | + -H "Content-Type: application/json; charset=utf-8" \ |
| 63 | + -H "Authorization: token ${SNYK_API_KEY}" \ |
| 64 | + -d "{\"target\":{\"owner\":\"elastic\",\"name\":\"cloudbeat\",\"branch\":\"${branch}\"},\"exclusionGlobs\":\"deploy, scripts, tests, security-policies\"}" |
| 65 | + done |
| 66 | +} |
| 67 | + |
| 68 | +upload_cloud_formation_templates |
| 69 | +bump_snyk_branch_monitoring |
0 commit comments