I am trying to wrap my head around how to make use of variables defined at different positions inside a Bitbucket pipeline.
I am using a Docker image based on python:3.8-slim-bookworm
, because reasons. Except for installing some packages, e.g. awscli
, the image is unmodified.
I separate the definition of steps from the actual pipelines, where the order of steps for different branches is outlined.
Inside a script
block,
script:
- FRUIT="banana"
- echo $FRUIT
- echo "$FRUIT"
- echo "${FRUIT}"
- echo "the ${FRUIT} is not an apple"
- ALMOST_PI=3
- echo $(($PI * 5))
I can define and use variables, even arithmetic expansion works.
I can export
and use variables as well.
script:
- export FUNCTION_NAME="do-stuff"
- export EXTENSION"="zip"
- export ZIP_FILE="${FUNCTION_NAME}.${EXTENSION}"
- echo $ZIP_FILE
This gives do-stuff.zip
However, the following does not work!
I can export
and use variables as well.
step:
environment:
VEGETABLE: "potato"
script:
- echo $VEGETABLE
This gives just nothing.
Moreover, variables can be declared in a variables
block on the same indentation level than image
, definitions
, steps
and pipelines
.
variables:
ARTIFACTS_DIR: &artifacts-dir "${BITBUCKET_CLONE_DIR}/artifacts"
However, so far I could not make use of this declaration inside a script block in a step. Instead, I declare the ARTIFACTS_DIR
time and time again in each step. This works, but it is cumbersome.