-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: made shell scripts more robust and reusable across projects
- Loading branch information
Showing
6 changed files
with
97 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
#!/bin/bash | ||
set -e # fail on any error | ||
set -o pipefail # ensure non-zero exit codes are propagated in piped commands | ||
set -e # Fail on any error | ||
set -o pipefail # Ensure piped commands propagate exit codes properly | ||
set -u # Treat unset variables as an error when substituting | ||
|
||
project_id=$(gcloud config get-value project) | ||
region=europe-west1 | ||
function_name=watchdog-notifications | ||
echo "https://console.cloud.google.com/functions/details/${region}/${function_name}?project=${project_id}&tab=logs " | ||
# Load the project variables | ||
source ./set-project-vars.sh | ||
|
||
logs_url="https://console.cloud.google.com/functions/details/${region}/${function_name}?project=${project_id}&tab=logs " | ||
printf '\n\033[1m%s\033[0m\n' "${logs_url}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,50 @@ | ||
#! /bin/bash | ||
set -e # fail on any error | ||
set -o pipefail # ensure non-zero exit codes are propagated in piped commands | ||
#!/bin/bash | ||
set -e # Fail on any error | ||
set -o pipefail # Ensure piped commands propagate exit codes properly | ||
set -u # Treat unset variables as an error when substituting | ||
|
||
# Get the project ID for the governance-watchdog project | ||
printf "Fetching the project ID for the governance-watchdog project:" | ||
project_id=$(gcloud projects list --filter="name:governance-watchdog" --format="value(projectId)") | ||
printf "Looking up project name in variables.tf..." | ||
project_name=$(awk '/variable "project_name"/{f=1} f==1&&/default/{print $3; exit}' ./infra/variables.tf | tr -d '",') | ||
printf ' \033[1m%s\033[0m\n' "${project_name}" | ||
|
||
printf "Fetching the project ID..." | ||
project_id=$(gcloud projects list --filter="name:${project_name}" --format="value(projectId)") | ||
printf ' \033[1m%s\033[0m\n' "${project_id}" | ||
|
||
# Set your local default project to the governance-watchdog project | ||
echo "Setting your default project to '${project_id}'..." | ||
gcloud config set project "${project_id}" | ||
# Set your local default project | ||
printf "Setting your default project to %s..." "${project_id}" | ||
{ | ||
output=$(gcloud config set project "${project_id}" 2>&1 >/dev/null) | ||
status=$? | ||
} | ||
if [[ ${status} -ne 0 ]]; then | ||
echo "Error: ${output}" | ||
exit "${status}" | ||
fi | ||
printf "✅\n" | ||
|
||
# Set the quota project to the governance-watchdog project, some gcloud commands require this to be set | ||
echo "Setting the quota project to '${project_id}'..." | ||
gcloud auth application-default set-quota-project "${project_id}" | ||
printf "Setting the quota project to %s..." "${project_id}" | ||
{ | ||
output=$(gcloud auth application-default set-quota-project "${project_id}" 2>&1 >/dev/null) | ||
status=$? | ||
} | ||
if [[ ${status} -ne 0 ]]; then | ||
echo "Error: ${output}" | ||
exit "${status}" | ||
fi | ||
printf "✅\n" | ||
|
||
# Update the project ID in your .env file so your cloud function points to the correct project when running locally | ||
printf "\n\nUpdating the project ID in your .env file..." | ||
sed -i '' "s/^GCP_PROJECT_ID=.*/GCP_PROJECT_ID=${project_id}/" .env | ||
printf "Updating the project ID in your .env file..." | ||
# Check if .env file exists | ||
if [[ ! -f .env ]]; then | ||
# If .env doesn't exist, create it with the initial value | ||
echo "GCP_PROJECT_ID=${project_id}" >.env | ||
else | ||
# If .env exists, perform the sed replacement | ||
sed -i '' "s/^GCP_PROJECT_ID=.*/GCP_PROJECT_ID=${project_id}/" .env | ||
fi | ||
printf "✅\n\n" | ||
|
||
printf "\n\n✅ All Done!" | ||
exit 0 | ||
echo "✅ All Done!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/bin/bash | ||
set -e # Fail on any error | ||
set -o pipefail # Ensure piped commands propagate exit codes properly | ||
set -u # Treat unset variables as an error when substituting | ||
|
||
printf "Looking up project name in variables.tf..." | ||
project_name=$(awk '/variable "project_name"/{f=1} f==1&&/default/{print $3; exit}' ./infra/variables.tf | tr -d '",') | ||
printf ' \033[1m%s\033[0m\n' "${project_name}" | ||
|
||
printf "Looking up region in variables.tf..." | ||
region=$(awk '/variable "region"/{f=1} f==1&&/default/{print $3; exit}' ./infra/variables.tf | tr -d '",') | ||
printf ' \033[1m%s\033[0m\n' "${region}" | ||
|
||
current_local_project_id=$(gcloud config get project) | ||
|
||
if [[ ! ${current_local_project_id} =~ ${project_name} ]]; then | ||
printf '️\n🚨 Your local gcloud is set to the wrong project: \033[1m%s\033[0m 🚨\n' "${current_local_project_id}" | ||
printf "\nRunning ./set-project-id.sh in an attempt to fix this...\n\n" | ||
source ./set-project-id.sh | ||
printf "\n\n" | ||
else | ||
printf "Looking up the project ID..." | ||
project_id=$(gcloud config get project) | ||
printf ' \033[1m%s\033[0m\n' "${project_id}" | ||
fi | ||
|
||
printf "Looking up function name..." | ||
function_name=$(gcloud functions list --format="value(name)") | ||
printf ' \033[1m%s\033[0m\n' "${function_name}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters