|
| 1 | +# TODO: enable secret manager: secretmanager.googleapis.com |
| 2 | + |
| 3 | + |
| 4 | +## REQUIRED VARS |
| 5 | +# PROJECT_ID |
| 6 | +# APP_NAME |
| 7 | + |
| 8 | +# INSTANCE_GIT_REPO_TOKEN |
| 9 | +# INSTANCE_GIT_REPO_OWNER |
| 10 | +# INSTANCE_GIT_REPO_NAME |
| 11 | + |
| 12 | +# API_KEY |
| 13 | + |
| 14 | + |
| 15 | +## CONSTRUCTED VARS |
| 16 | +export GIT_TOKEN=${GIT_TOKEN} |
| 17 | +export GIT_USER=${GIT_USER} |
| 18 | +export GIT_USERNAME=${GIT_USER} |
| 19 | +export API_KEY_VALUE=${API_KEY} |
| 20 | + |
| 21 | + |
| 22 | +export WORK_DIR=${PWD} |
| 23 | +export GIT_CMD=${WORK_DIR}/utils/git/gh.sh |
| 24 | +export GIT_BASE_URL=https://${GIT_USER}@github.com/${GIT_USER} |
| 25 | + |
| 26 | +export APP_INSTANCE_REPO_LOCATION=https://github.com/${GIT_USERNAME}/${APP_NAME} |
| 27 | + |
| 28 | +export IMAGE_REPO=gcr.io/${PROJECT_ID} |
| 29 | +export PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)') |
| 30 | + |
| 31 | +export SECRET_NAME=${APP_NAME}-webhook-trigger-secret |
| 32 | + |
| 33 | +#TODO - Fix value # this didn't work in cloud build run |
| 34 | +#SECRET_VALUE=$(sed "s/[^a-zA-Z0-9]//g" <<< $(openssl rand -base64 15)) |
| 35 | +SECRET_VALUE=foobar |
| 36 | + |
| 37 | +SECRET_PATH=projects/${PROJECT_NUMBER}/secrets/${SECRET_NAME}/versions/1 |
| 38 | +printf ${SECRET_VALUE} | gcloud secrets create ${SECRET_NAME} --data-file=- |
| 39 | + |
| 40 | +gcloud secrets add-iam-policy-binding ${SECRET_NAME} \ |
| 41 | + --member=serviceAccount:service-${PROJECT_NUMBER}@gcp-sa-cloudbuild.iam.gserviceaccount.com \ |
| 42 | + --role='roles/secretmanager.secretAccessor' |
| 43 | + |
| 44 | +## Create CloudBuild Webhook Endpoint |
| 45 | +echo Create CloudBuild Webhook Endpoint |
| 46 | +TRIGGER_NAME=${APP_NAME}-webhook-trigger |
| 47 | +BUILD_YAML_PATH=$WORK_DIR/cp-templates/go-app-cicd/build/cloudbuild.yaml |
| 48 | + |
| 49 | +## Setup Trigger & Webhook |
| 50 | +gcloud alpha builds triggers create webhook \ |
| 51 | + --name=${TRIGGER_NAME} \ |
| 52 | + --inline-config=$BUILD_YAML_PATH \ |
| 53 | + --secret=${SECRET_PATH} --substitutions='_APP_NAME=${APP_NAME},_APP_REPO=$(body.repository.git_url),_REF=$(body.ref)' |
| 54 | + |
| 55 | +## Retrieve the URL |
| 56 | +WEBHOOK_URL="https://cloudbuild.googleapis.com/v1/projects/${PROJECT_ID}/triggers/${TRIGGER_NAME}:webhook?key=${API_KEY_VALUE}&secret=${SECRET_VALUE}" |
| 57 | +echo WEBHOOK_URL=${WEBHOOK_URL} |
| 58 | + |
| 59 | +## Configure Github Repo Webhook |
| 60 | +echo Configure Github Repo Webhook |
| 61 | +${GIT_CMD} create_webhook ${APP_NAME} $WEBHOOK_URL |
| 62 | + |
0 commit comments