Bitbucket Pipelines: How to pass values between steps for CI/CD
In a Bitbucket pipeline, the step
option represent an execution unit that can carry out tasks such as running compilers or tests.
You can have multiple step
or units, but what if you want to pass files or other data between them?
For example, for the pipeline defined in yaml below, the step named Build 2
cannot see the output of Build 1
. This shows that steps are isolated execution units which rebuild the current working directory from scratch.
pipelines:
default:
- step:
name: Build 1
script:
- echo "I am step 1"
- echo "Hello" > importantfile.txt
- step:
name: Build 2
script:
- echo "I am step 2"
- echo "I cannot see file.txt"
- cat importantfile.txt ##### will result in an error! <------------
So, what if you have files, values or other data you want to pass to steps ahead?
Use artifacts
property as shown in step named Build 1
:
pipelines:
default:
- step:
name: Build 1
script:
- echo "I am step 1"
- touch importantfile.txt
artifacts:
- importantfile.txt
- step:
name: Build 2
script:
- echo "I am step 2"
- echo "I can see importantfile.txt"
- cat importantfile.txt ##### outputs Hello, so it works !!! <------------
- step:
name: Build 3
script:
- echo "I am step 3"
- echo "I also can see importantfile.txt"
- cat file.txt ##### outputs Hello, works here too !!! <------------
With artifacts, you can create .env
files, directories or other temporary pieces of data to pass in to the subsequent execution units.
You can even pass directories such as my tests
:
pipelines:
default:
- step:
name: Build 1
script:
- echo "I am step 1"
- mkdir tests
- echo "Test 1 success" > tests/test1.txt
- echo "Test 2 success" > tests/test2.txt
artifacts:
- tests/**
- step:
name: Build 2
script:
- echo tests/test1.txt # outputs Test 1 success <------------
- echo tests/test2.txt # outputs Test 2 success <------------
Now every step will first download artifacts from previous steps that you can use in the pipeline to complete your work.
To learn more, read the docs here