Skip to content

Commit 04ed6b4

Browse files
committed
Initial Commit
0 parents  commit 04ed6b4

File tree

7 files changed

+270
-0
lines changed

7 files changed

+270
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# cp-utils

cleanup.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Access token needs delete rights which is not included in the base repo rights
2+
export INSTANCE_GIT_REPO_TOKEN=
3+
export INSTANCE_GIT_REPO_OWNER=cgrant
4+
5+
## Delete Trigger
6+
TRIGGER_NAME=${APP_NAME}-webhook-trigger
7+
gcloud alpha builds triggers delete ${TRIGGER_NAME} -q
8+
9+
10+
## Delte Secret
11+
SECRET_NAME=${APP_NAME}-webhook-trigger-secret
12+
gcloud secrets delete ${SECRET_NAME} -q
13+
14+
## Delete Repo
15+
export GIT_TOKEN=${INSTANCE_GIT_REPO_TOKEN}
16+
export GIT_USERNAME=${INSTANCE_GIT_REPO_OWNER}
17+
export BASE_DIR=${PWD}
18+
export GIT_CMD=${BASE_DIR}/cp-templates/cicd-pipeline/util/git/gh.sh
19+
export GIT_ASKPASS=${BASE_DIR}/cp-templates/cicd-pipeline/util/git/git-ask-pass.sh
20+
${GIT_CMD} delete ${APP_NAME}
21+

createApp.sh

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
## Required Vars
3+
4+
# APP_NAME
5+
# REGION
6+
7+
# INSTANCE_GIT_REPO_TOKEN
8+
# INSTANCE_GIT_REPO_OWNER
9+
# INSTANCE_GIT_REPO_NAME
10+
11+
12+
13+
## Constructed Vars
14+
export GIT_USERNAME=${INSTANCE_GIT_REPO_OWNER}
15+
export GIT_TOKEN=${INSTANCE_GIT_REPO_TOKEN}
16+
export GIT_BASE_URL=https://${INSTANCE_GIT_REPO_OWNER}@github.com/${INSTANCE_GIT_REPO_OWNER}
17+
export BASE_DIR=${PWD}
18+
export GIT_CMD=${BASE_DIR}/utils/git/gh.sh
19+
export GIT_ASKPASS=${BASE_DIR}/utils/git/git-ask-pass.sh
20+
21+
22+
## Verify Vars
23+
echo GIT_ASKPASS=${GIT_ASKPASS}
24+
echo REGION=${REGION}
25+
echo APP_NAME=${APP_NAME}
26+
echo INSTANCE_GIT_REPO_OWNER=${INSTANCE_GIT_REPO_OWNER}
27+
echo GIT_USERNAME=${GIT_USERNAME}
28+
echo INSTANCE_GIT_REPO_NAME=${INSTANCE_GIT_REPO_NAME}
29+
echo INSTANCE_GIT_REPO_TOKEN=${INSTANCE_GIT_REPO_TOKEN}
30+
echo GIT_TOKEN=$GIT_TOKEN
31+
32+
apt-get update
33+
apt-get install curl git -y
34+
35+
#git config --global user.email $(gcloud config get-value account)
36+
git config --global user.email ${INSTANCE_GIT_REPO_OWNER}
37+
git config --global user.name ${INSTANCE_GIT_REPO_OWNER}
38+
39+
40+
#git clone https://github.com/gitrey/cp-templates.git util
41+
42+
43+
44+
#source $GIT_ASKPASS
45+
46+
printf 'Creating application: %s \n' $APP_NAME
47+
48+
# # Create an instance of the template.
49+
# Clone the template repo
50+
#git clone https://github.com/GoogleCloudPlatform/software-delivery-workshop.git plat
51+
#mv plat/delivery-platform/resources/repos/app-templates ./
52+
#rm -rf plat
53+
rm -rf ./cp-templates/.git
54+
cd ./cp-templates/go-app-cicd/
55+
56+
# Swap Variables
57+
for template in $(find . -name '*.tmpl'); do envsubst < ${template} > ${template%.*}; done
58+
59+
# Create and push to new repo
60+
git init
61+
git checkout -b main
62+
#git symbolic-ref HEAD refs/heads/main
63+
echo create repo
64+
${GIT_CMD} create ${APP_NAME}
65+
git remote add origin $GIT_BASE_URL/${APP_NAME}
66+
git add . && git commit -m "initial commit"
67+
git push origin main
68+
# Auth fails intermittetly on the very first client call for some reason
69+
# Adding a retry to ensure the source is pushed.
70+
git push origin main
71+
72+

createWebhook.sh

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+

git/gh.sh

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2021 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
## Input Validation
18+
if [[ ${GIT_TOKEN} == "" ]]; then
19+
echo "GIT_TOKEN variable not set. Please rerun the env script"
20+
exit -1
21+
fi
22+
if [[ ${GIT_USERNAME} == "" ]]; then
23+
echo "GIT_USERNAME variable not set. Please rerun the env script"
24+
exit -1
25+
fi
26+
27+
if [[ $1 == "create_webhook" ]]; then
28+
if [[ $2 == "" || $3 == "" ]]; then
29+
echo "Missing parameters"
30+
echo "Usage: gh create_webhook <repo> <webhook_url>"
31+
exit -1
32+
fi
33+
fi
34+
35+
if [[ $2 == "" || $1 == "" ]]; then
36+
echo "Usage: gh <create|delete|create_webhook> <repo> [webhook_url]"
37+
exit -1
38+
fi
39+
40+
## Local variables
41+
action=$1
42+
repo=$2
43+
WEBHOOK_URL=$3
44+
GIT_API_BASE="https://api.github.com"
45+
46+
export GIT_ASKPASS=$BASE_DIR/common/ghp.sh
47+
48+
## Execution
49+
create_webhook () {
50+
curl -H "Authorization: token ${GIT_TOKEN}" \
51+
-d '{"config": {"url": "'${WEBHOOK_URL}'", "content_type": "json"}}' \
52+
-X POST ${GIT_API_BASE}/repos/${GIT_USERNAME}/${repo}/hooks
53+
}
54+
55+
56+
if [[ $action == 'create' ]]; then
57+
# Create
58+
curl -H "Authorization: token ${GIT_TOKEN}" ${GIT_API_BASE}/user/repos -d '{"name": "'"${repo}"'"}' > /dev/null
59+
echo "Created ${repo}"
60+
61+
#TODO: Check if repo exists first
62+
fi
63+
64+
if [[ $action == 'delete' ]]; then
65+
# Delete
66+
echo
67+
echo "Deleting ${GIT_API_BASE}/repos/${GIT_USERNAME}/${repo}"
68+
curl -H "Authorization: token ${GIT_TOKEN}" -X "DELETE" ${GIT_API_BASE}/repos/${GIT_USERNAME}/${repo}
69+
echo "Deleted ${repo}"
70+
fi
71+
72+
if [[ $action == 'create_webhook' ]]; then
73+
# Webhook
74+
create_webhook
75+
fi

git/git-ask-pass.sh

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2021 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
exec echo "${GIT_TOKEN}"

testScript.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
## Required Vars
3+
export PROJECT_ID=crg-workbench
4+
5+
export APP_NAME=foobar2
6+
export REGION=us-central1
7+
8+
export API_KEY=
9+
10+
export INSTANCE_GIT_REPO_TOKEN=
11+
export INSTANCE_GIT_REPO_OWNER=cgrant
12+
export INSTANCE_GIT_REPO_NAME=${APP_NAME}
13+
14+
15+
16+
./createApp.sh
17+
18+
./createWebhook.sh
19+
20+
21+
#./cleanup.sh

0 commit comments

Comments
 (0)