You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DevOps-Bash-tools/circleci_project_set_env_va...

87 lines
2.4 KiB
Bash

#!/usr/bin/env bash
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2021-12-03 17:41:23 +0000 (Fri, 03 Dec 2021)
#
# https://github.com/HariSekhon/bash-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
# https://circleci.com/docs/api/v2/#operation/createEnvVar
set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
srcdir="$(dirname "${BASH_SOURCE[0]}")"
# shellcheck disable=SC1090
. "$srcdir/lib/utils.sh"
# shellcheck disable=SC2034,SC2154
usage_description="
Adds / updates CircleCI project-level environment variable(s) from args or stdin
If no second argument is given, reads environment variables from standard input, one per line in 'key=value' format or 'export key=value' shell format
Examples:
${0##*/} github/HariSekhon/DevOps-Bash-tools AWS_ACCESS_KEY_ID=AKIA...
echo AWS_ACCESS_KEY_ID=AKIA... | ${0##*/} github/HariSekhon/DevOps-Bash-tools
Loads both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY via stdin:
aws_csv_creds.sh credentials_exported.csv | ${0##*/} github/HariSekhon/DevOps-Bash-tools
"
# used by usage() in lib/utils.sh
# shellcheck disable=SC2034
usage_args="<project_slug> [<key>=<value> <key2>=<value2> ...]"
help_usage "$@"
min_args 1 "$@"
project_slug="$1"
shift || :
project_slug="${project_slug##/}"
project_slug="${project_slug%%/}"
if ! [[ "$project_slug" =~ ^[[:alnum:]]+/[[:alnum:]-]+/[[:alnum:]-]+$ ]]; then
usage "project-slug given '$project_slug' does not conform to <vcs>/<user_or_org>/<repo> format"
fi
add_env_var(){
local env_var="$1"
env_var="${env_var%%#*}"
env_var="${env_var##[[:space:]]}"
env_var="${env_var##export}"
env_var="${env_var##[[:space:]]}"
if ! [[ "$env_var" =~ ^[[:alpha:]][[:alnum:]_]+=.+$ ]]; then
usage "invalid environment key=value argument given: $env_var"
fi
local name="${env_var%%=*}"
local value="${env_var#*=}"
timestamp "adding/updating CircleCI environment variable '$name' to project '$project_slug'"
"$srcdir/circleci_api.sh" "/project/$project_slug/envvar" -X POST -d "{\"name\": \"$name\", \"value\": \"$value\"}" | jq .
}
if [ $# -gt 0 ]; then
for arg in "$@"; do
add_env_var "$arg"
done
else
while read -r line; do
add_env_var "$line"
done
fi