Disclaimer: This is not an encouragement to use BitBucket. This is only here to help the poor people who are stuck with it.
The following JavaScript functions can be used to extract the environment variables for a BitBucket deployment into a shell script which sets the environment variables locally.
This is helpful for when you want to test something locally without manually having to copy and paste 50 environment variables.
/**
* Get the deployment variables for a given BitBucket deployment, and return them in a dictionary.
*
* @param {string} deploymentName The name of the deployment to get the variables for
* @returns {Record<string, string>} A key-value dictionary of the deployment variables
*/
function getBitBucketDeploymentVariables(deploymentName) {
// Find the parent div from the name of the deployment
const h4Element = Array.from(document.querySelectorAll('h4')).find(h4 => h4.textContent.trim() === deploymentName);
const parentDiv = h4Element.parentElement.parentElement;
// get the table containing all the variables
const variablesTable = parentDiv.querySelector('section > table');
// Make a dictionary to hold all the goodies
const variables = {};
// Get the values from the table
variablesTable.querySelectorAll('tbody tr > td').forEach(td => {
// Get the key and value from the td
const key = td.querySelector('div > div:nth-child(1) > form').textContent;
const value = td.querySelector('div > div:nth-child(2) > form').textContent;
// Add this variable to the dictionary
variables[key] = value;
});
return variables;
}
/**
* Create a shell script to set the given environment variables.
*
* @param {Record<string, string>} environmentVariables The environment variables to set
* @returns {string} A shell script which will set the environment variables
*/
function createEnvironmentSettingScript(environmentVariables) {
// Create the beginning of the script
let script = '#!/usr/bin/env sh\n';
// Add each environment variable to the script
for (const [key, value] of Object.entries(environmentVariables)) {
script += `export ${key}="${value}"\n`;
}
// You already know
script += '\n';
return script;
}
/**
* Extract the environment variables for the given deployment name, and return a shell script
* which sets the environment variables.
*
* @param {string} deploymentName The name of the deployment to get environment variables for
* @returns {string} A shell script which will set the environment locally
*/
function getScriptForDeploymentVariables(deploymentName) {
const deploymentVariables = getBitBucketDeploymentVariables(deploymentName);
const shellScript = createEnvironmentSettingScript(deploymentVariables);
return shellScript;
}
First navigate to the deployments page of your BitBucket repository, and paste the code above into the console. Then you can call getScriptForDeploymentVariables
, passing the name of the environment that you want to get the variables for(case sensitive). This will return a shell script you can use to set the environment variables locally.
If you want to get the environment variables as a dictionary, and not a shell script, you can use the getBitBucketDeploymentVariables
function, passing the name of the environment. Then you can copy the dictionary returned from that function and use it however you want.
This script not get the value of secret variables. Any secret variables will have whatever value is shown in the BitBucket table, usually ••••••.