Bitbucket pipeline for build Gradle

Viewed 442

I am working on a Bitbucket pipeline where a zip file gets created using environment variables that are set through a .env file. Before this zip file gets generated usually a .env file is configured with variables set used to create a zip file. The zip file gets generated utilizing a Gradle wrapper (gradlew). I'm a little unsure how to set these variables in the .env file. I know Bitbucket has the option to use repository variables in order to set these values, but I am unsure if this is best practice for automating this process. Any advice on this would be appreciated. Below is how the the environment variables currently get set and how the zip is created with gradlew.

.ENV

NETWORK_NAME=network

GROVE_ZIP_PATH=./build
GROVE_ZIP_FILENAME=zipName.zip

# Configuration
HOST=host.docker.internal
PORT_MAIN=8070
PORT_SEARCH=8074

Create Zip

.\gradlew zipName

The ENV file needs to be configured before the zip file can be created. Any advice on how to do this in a pipeline would help.

1 Answers

You could encode the .ENV into a base64 string and set it as an repository variable on the repo.

1. Convert .ENV into base64

The following command(s) will convert a file into base64, strip all newline characters and also copy it to your clipboard.

Linux openssl base64 < .ENV | tr -d '\n' | xclip -selection clipboard

Mac openssl base64 < .ENV | tr -d '\n' | pbcopy

Note: For the linux command, you will need to install the xclip package.

Unfortunately I don't develop on a windows machine so I cannot provide the windows version of the script.

2. Add the base64'd ENV to your repository variables

Repository settings -> Repository Variables

Make sure the secured box is ticked as well.

3. Decode the base64 value into a .ENV in your pipeline

This can be done with something like this:

script:
  - echo $ENV_FILE_B64 | base64 -di > .ENV
  - ./gradlew zipName

The base64 -d command will decode the contents of $ENV_FILE_B64 from base64 and dump it into a file called .ENV. The i flag ignores garbage characters when decoding.

Related