Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How can i set environment variables for all steps in one branch

Medin December 25, 2019

I want to achieve this goal, 
When i push to dev branch i want all steps to execute with Test environment variables using Pipeline Deployments

 

image: node:11.13.0-alpine

definitions: 
steps:
- step: &build
name: Build
script:
- echo $AWS_STAGE
- step: &deploy
name: Deploy
script:
- echo $AWS_STAGE

pipelines:
branches:
release/dev:
- step: 
<<: *build
deployment: test
- step: 
<<: *deploy
deployment: test

I have Deployments setup correctly and its printing correct variables if i have deployments in only one step 

Above YAML is valid by https://bitbucket-pipelines.prod.public.atl-paas.net/validator
But throws an error when pipeline is runing
image.png

1 answer

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.
December 28, 2019

Looks like the problem is to have two times the deployment environment "test" in the same pipeline.

Bitbucket Pipelines refuses that and gives the error message.

Normally the deployment should be given on the step that deploys the (previous) build artifacts.

From what I can read in your question that would be the second step about deploying.

You can transfer artifacts from the first build step to the second deploy step by making use of the artifacts entry (see Using artifacts in steps) which should prevent the error.

So in short: It's possible to have multiple steps with deployments in the same pipeline but each deployment must have a different deployment environment (also sometimes named as deployment target).

E.g. first deploy to test, then deploy to production.

Artifacts are stored for 14 days, so if the deployment to production is a manual step, you have 14 days time to press the deploy button for production if the production deployment is a manual step.

If you could share a bit more how come to have the same deployment environment two times in the same pipeline, it's perhaps easier to figure out a more concrete suggestion.

Medin January 2, 2020

Hey, @ktomk thanks for the reply. 
I am indeed using artefacts, but i extracted simple example just for the usecase. 


Actually what i need is an environment variables for build and deploy. For example if i want to build and deploy test env, then i want to be able to run the build step with Test env variables, because the build needs to contain urls to BE and so on...
Then in deploy step, i want to deploy the build to test environment by using deployment variables. 

In short Deployment, variables should not be only for deploying but also for other steps.

For example in Wercker, they have global env variables, then per each step you can define env variables. 
Can i do this with Bitbucket pipelines?

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.
January 3, 2020

I don't know Wercker personally, so I can not comment on it.

You can define repository variables in Bitbucket Pipelines and they are for all pipelines and steps. This might map onto what you call Wercker global env variables.

In Bitbucket Pipelines from what I know it is *not* possible to define variables per step.

However there are deployment variables which is per deployment. But this comes with a caveat, see the following larger discussion which circles around the limitations: The deployment environment 'test' in your bitbucket-pipelines.yml file is invalid (the title is a bit misleading).

AFAIK deployment variables are limited to a single step. So this is perhaps not that well working for you. The discussion above (and below) still might shed more light and maybe you find a way 

See as well:

Medin January 6, 2020

Thanks for your answer, but yes with Bitbucket pipelines i need to repeat some scripts in some steps to achieve the goal. 

I hope you will work on this limitation and enable deployment variables on each step + manual overriding the variables in each step. That would open more ways of engineering the Pipelines. 

Thanks

Like # people like this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events