Created
April 17, 2019 15:17
-
-
Save chrsmith/911e752ca5112788e6575c95765d19e7 to your computer and use it in GitHub Desktop.
Bash script for managing CircleCI environment variables.
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
#!/bin/bash | |
# | |
# circleci-envvar-manager.sh | |
# Bash script for managing CircleCI project environment variables. | |
# | |
# This script uses the CircleCI APIs to list, create, and delete the environment | |
# variables associated with CircleCI projects. Suitable for scripting the | |
# rotation of access keys. | |
# | |
# CircleCI API documentation: | |
# https://circleci.com/docs/api/v1-reference/ | |
set -o nounset -o errexit -o pipefail | |
if [ -z ${CIRCLECI_API_TOKEN:-} ]; then | |
echo "Error: no CIRCLECI_API_TOKEN environment variable found" | |
echo "You can create a CircleCI personal API token at:" | |
echo "https://circleci.com/account/api" | |
# Once the token has been created, you can verify its validitity by calling: | |
# $ curl "https://circleci.com/api/v1.1/me?circle-token=${CIRCLECI_API_TOKEN}" | |
exit 1 | |
fi | |
function list_envvars() { | |
local project=$1 | |
curl \ | |
--header "Content-Type: application/json" \ | |
"https://circleci.com/api/v1.1/project/${project}/envvar?circle-token=${CIRCLECI_API_TOKEN}" | |
} | |
function new_envvar() { | |
local project=$1 | |
local envvar=$2 | |
local value=$3 | |
curl \ | |
-X POST \ | |
--header "Content-Type: application/json" \ | |
-d '{"name":"'"${envvar}"'", "value":"'"${value}"'"}' \ | |
"https://circleci.com/api/v1.1/project/${project}/envvar?circle-token=${CIRCLECI_API_TOKEN}" | |
} | |
function delete_envvar() { | |
local project=$1 | |
local envvar=$2 | |
curl \ | |
-X DELETE \ | |
--header "Content-Type: application/json" \ | |
"https://circleci.com/api/v1.1/project/${project}/envvar/${envvar}?circle-token=${CIRCLECI_API_TOKEN}" | |
} | |
# Switch on the script's first argument. | |
case ${1} in | |
list-envvars) | |
list_envvars $2 | |
;; | |
new-envvar) | |
echo "deleting current env var..." | |
delete_envvar $2 $3 | |
echo "setting new value..." | |
new_envvar $2 $3 $4 | |
;; | |
delete-envvar) | |
delete_envvar $2 $3 | |
;; | |
*) | |
echo "Usage. <project> is of the form 'github/robot-co/project-name'" | |
echo "" | |
echo " ${0} list-envvars <project>" | |
echo " ${0} delete-envvar <project> <envvar-name>" | |
echo " ${0} new-envvar <project> <envvar-name> <envvar-value>" | |
exit 1 | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment