Skip to content

Add, Update, or Remove Environment Variables using Task

tmpayton edited this page Aug 12, 2024 · 9 revisions

This wiki will show you how to use the remove_env_var and add_update_env_var tasks to modify environment variables.

Login to cloud.gov:

cf login -a api.fr.cloud.gov  --sso

Target any space:

cf target -s dev

Dump old environment variables into a file:

cf env <app> > old_env_var.json

Generate an oauth token and save it in a file:

cf oauth-token > token.txt

NOTE: This token will work for about four minutes before requiring another one. Also make sure the file is in the same directory that you are running the next command.

To add or update an environment variable:

cf run-task api --command "python cli.py add_update_env_var <space> <service instance> <key> <value> '$(< token.txt)'" --name add_update_env_var

To remove an environment variable:

cf run-task api --command "python cli.py remove_env_var <space> <service instance> <key to remove> '$(< token.txt)'" --name remove_env_var

Confirm changes took with

cf env <app>

Rebuild the app in CircleCI

Finally, delete the old environment variables

rm old_env_var.json

Task arguments:

These tasks requires that you pass in the space , service instance, your bearer token.

  • space: [Required] the space that you want to update
  • service_instance: [Required] the user provided service you want to update
  • token: [Required] your cf oath token generated above

For adding or updating environment variables you must pass in the key value pair:

  • key: [Required] The key of the environment variable you would like to add or update
  • value: [Required] The value of the environment variable you would like to add or update

For removing an environment variable you must also pass in the key to remove:

  • key_to_remove: [Required] The key of the environment variable you would like to remove

Add/update example:

cf run-task api --command "python cli.py add_update_env_var dev fec_creds_dev example_key 1234 '$(< token.txt)'" --name add_update_env_var

remove example:

cf run-task api --command "python cli.py remove_env_var dev fec_creds_dev example_key '$(< token.txt)'" --name remove_env_var`
Clone this wiki locally