|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# The repo is cloned to $KOKORO_ARTIFACTS_DIR/git/functions-framework-java |
| 5 | +REPO_DIR="${KOKORO_ARTIFACTS_DIR}/git/functions-framework-java" |
| 6 | +cd "${REPO_DIR}" |
| 7 | + |
| 8 | +# ============================================================================== |
| 9 | +# 1. Configure Airlock and AR Credentials |
| 10 | +# ============================================================================== |
| 11 | +# Get OAuth token from GCE metadata server inside Kokoro VM |
| 12 | +MAVEN_TOKEN=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token" -H "Metadata-Flavor: Google" | grep -oP '"access_token":"\K[^"]+') |
| 13 | + |
| 14 | +# Create a temporary settings.xml to configure Airlock mirror and AR auth |
| 15 | +cat > settings.xml <<EOF |
| 16 | +<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" |
| 17 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 18 | + xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> |
| 19 | + <mirrors> |
| 20 | + <!-- Redirect ALL downloads to Airlock Maven Central mirror --> |
| 21 | + <mirror> |
| 22 | + <id>airlock-mirror</id> |
| 23 | + <name>Airlock Maven Central mirror</name> |
| 24 | + <url>https://us-maven.pkg.dev/artifact-foundry-prod/maven-3p-trusted</url> |
| 25 | + <mirrorOf>*</mirrorOf> |
| 26 | + </mirror> |
| 27 | + </mirrors> |
| 28 | + <servers> |
| 29 | + <!-- Credentials for the Airlock mirror --> |
| 30 | + <server> |
| 31 | + <id>airlock-mirror</id> |
| 32 | + <username>oauth2accesstoken</username> |
| 33 | + <password>${MAVEN_TOKEN}</password> |
| 34 | + </server> |
| 35 | + <!-- Credentials for the Exit Gate AR deployment repo --> |
| 36 | + <server> |
| 37 | + <id>exit-gate-ar</id> |
| 38 | + <username>oauth2accesstoken</username> |
| 39 | + <password>${MAVEN_TOKEN}</password> |
| 40 | + </server> |
| 41 | + </servers> |
| 42 | +</settings> |
| 43 | +EOF |
| 44 | + |
| 45 | +# ============================================================================== |
| 46 | +# 2. Retrieve GPG keys from Secret Manager |
| 47 | +# ============================================================================== |
| 48 | +GPG_KEYRING="${KOKORO_ARTIFACTS_DIR}/gpg-keyring" |
| 49 | +GPG_PASSPHRASE_FILE="${KOKORO_ARTIFACTS_DIR}/gpg-passphrase" |
| 50 | + |
| 51 | +# Read names from environment variables injected by Louhi |
| 52 | +PROJECT_ID="${_LOUHI_SECRET_PROJECT_ID}" |
| 53 | +KEYRING_NAME="${_LOUHI_GPG_KEYRING_SECRET_NAME}" |
| 54 | +PASSPHRASE_NAME="${_LOUHI_GPG_PASSPHRASE_SECRET_NAME}" |
| 55 | + |
| 56 | +echo "Fetching secrets from project: ${PROJECT_ID}" |
| 57 | +gcloud secrets versions access latest --secret="${KEYRING_NAME}" --project="${PROJECT_ID}" > "${GPG_KEYRING}" |
| 58 | +gcloud secrets versions access latest --secret="${PASSPHRASE_NAME}" --project="${PROJECT_ID}" > "${GPG_PASSPHRASE_FILE}" |
| 59 | + |
| 60 | +export GPG_TTY=$(tty) |
| 61 | +export GPG_PASSPHRASE=$(cat "${GPG_PASSPHRASE_FILE}") |
| 62 | +export GNUPGHOME=/tmp/gpg |
| 63 | +mkdir -p "${GNUPGHOME}" |
| 64 | +gpg --batch --import "${GPG_KEYRING}" |
| 65 | + |
| 66 | +# ============================================================================== |
| 67 | +# 3. Build, Sign, and Deploy |
| 68 | +# ============================================================================== |
| 69 | +# Detect which package to build based on the Louhi trigger tag |
| 70 | +if [[ -n "${_LOUHI_REF_NAME:-}" ]]; then |
| 71 | + echo "Triggered by Louhi tag: ${_LOUHI_REF_NAME}" |
| 72 | + if [[ "${_LOUHI_REF_NAME}" == *functions-framework-api* ]]; then |
| 73 | + PACKAGE_DIR="functions-framework-api" |
| 74 | + elif [[ "${_LOUHI_REF_NAME}" == *function-maven-plugin* ]]; then |
| 75 | + PACKAGE_DIR="function-maven-plugin" |
| 76 | + elif [[ "${_LOUHI_REF_NAME}" == *java-function-invoker* ]]; then |
| 77 | + PACKAGE_DIR="invoker" |
| 78 | + else |
| 79 | + echo "Unknown tag format: ${_LOUHI_REF_NAME}. Defaulting to invoker." |
| 80 | + PACKAGE_DIR="invoker" |
| 81 | + fi |
| 82 | +else |
| 83 | + # Fallback for manual/non-tag builds (e.g. testing) |
| 84 | + echo "No Louhi tag detected. Falling back to KOKORO_JOB_NAME detection." |
| 85 | + if [[ $KOKORO_JOB_NAME == *"function-maven-plugin"* ]]; then |
| 86 | + PACKAGE_DIR="function-maven-plugin" |
| 87 | + elif [[ $KOKORO_JOB_NAME == *"functions-framework-api"* ]]; then |
| 88 | + PACKAGE_DIR="functions-framework-api" |
| 89 | + else |
| 90 | + PACKAGE_DIR="invoker" |
| 91 | + fi |
| 92 | +fi |
| 93 | + |
| 94 | +echo "Building package in directory: ${PACKAGE_DIR}" |
| 95 | +cd "${PACKAGE_DIR}" |
| 96 | + |
| 97 | +# Run maven deploy using the temporary settings.xml |
| 98 | +# We use altDeploymentRepository to override the deploy target without editing pom.xml |
| 99 | +mvn clean deploy -B \ |
| 100 | + -P sonatype-oss-release \ |
| 101 | + --settings=../settings.xml \ |
| 102 | + -DaltDeploymentRepository=exit-gate-ar::https://us-maven.pkg.dev/oss-exit-gate-prod/ff-releases--mavencentral \ |
| 103 | + -Dgpg.executable=gpg \ |
| 104 | + -Dgpg.passphrase="${GPG_PASSPHRASE}" \ |
| 105 | + -Dgpg.homedir="${GNUPGHOME}" |
| 106 | + |
| 107 | +# ============================================================================== |
| 108 | +# 4. Copy artifacts to 'artifacts/' folder for Kokoro Attestation Generation |
| 109 | +# ============================================================================== |
| 110 | +ARTIFACTS_DIR="${REPO_DIR}/artifacts" |
| 111 | +mkdir -p "${ARTIFACTS_DIR}" |
| 112 | + |
| 113 | +# Copy target jars and poms (excluding test jars) to be captured by build.cfg |
| 114 | +find target/ -maxdepth 1 -name "*.jar" -o -name "*.pom" | grep -v "test" | xargs -I {} cp {} "${ARTIFACTS_DIR}/" |
0 commit comments