How do I modify variables in a build

Jacob Craig May 29, 2020

I'm trying use the variable BITBUCKET_BRANCH to define a filename, but the slash is causing us issues. Here's a sample YAML

pipelines:
branches:
feature/*:
- step:
name: Build javascript
image: node:12.16.3
caches:
- node
script:
- apt-get update && apt-get install --yes zip
- npm install
- npx ng build
- zip -r ${BITBUCKET_BRANCH}.zip dist
- pipe: atlassian/bitbucket-upload-file:0.1.4
variables:
BITBUCKET_USERNAME: $NAME
BITBUCKET_APP_PASSWORD: $PASSWORD
FILENAME: "${BITBUCKET_BRANCH}.zip"

 

How can I modify the BITBUCKET_BRANCH variable to remove non-alphanumeric characters? For example changing from: feature/bitbucket-ci --> featurebitbucketci.

2 answers

0 votes
ktomk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 5, 2020

I did not test this on Bitbucket but I can imagine it works this way:

  • Define a new variable that contains the filename in the form you prefer
  • Use it in your step-script (e.g. for creating the zip)
  • Export it so that mapping to the variable table of the Pipe works (?)

Example (tested to work with the node:12.16.3 image):

script:
- PACKAGE_FILE="${BITBUCKET_BRANCH//\//-}.zip" # replace all slashes with dashes
  - zip -r ${PACKAGE_FILE} dist
- export PACKAGE_FILE
- pipe: atlassian/bitbucket-upload-file:0.1.4
variables:
BITBUCKET_USERNAME: $NAME
BITBUCKET_APP_PASSWORD: $PASSWORD
FILENAME: $PACKAGE_FILE

 Discussion:

It might already work like this. Fine. Here is some more context of why I suggest this:

It is my understanding that the variables property of a pipe needs to contain strings. These are passed to the docker run command that runs the pipe image as --env NAME=value command line options and arguments.

This command runs in the same shell script as the other script lines. Therefore technically the export should not be necessary. Additionally it should not be necessary to use the plain string $PACKAGE_FILE for the FILENAME: property. However from your report I'm not entirely sure if this is stable and as written I didn't test this on Bitbucket (and the local Bitbucket Pipeline runner I'm using does not have full pipe support [yet], so local testing is limited to some degree).

Therefore the extra script line to export the $PACKAGE_FILE variable and overall to create an extra variable so that the pipe.variables table has it in a simple manner (just $PACKAGE_FILE, just the variable name prefixed with the dollar sign "$").

0 votes
Oleksandr Kyrdan
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 3, 2020

Hi @Jacob Craig ,

Thank you for your question!

You could create a new variable:

- ARTIFACT_FILENAME=$(echo $BITBUCKET_BRANCH | sed "s/[^a-zA-Z0-9]//g")
- zip -r ${ARTIFACT_FILENAME}.zip dist
Jacob Craig June 3, 2020

That part was easy enough to do, the question was how to send that into the pipe.

FILENAME: "${BITBUCKET_BRANCH}.zip" 

I thought those were unfurled at the beginning of the run.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events