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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How can I set Pipelines environmental variables from my Python code inside my Pipelines script?

I have set my an environmental variable in my setup.py script and I would like to access this in my Pipelines script.

os.environ["MY_PKG_VERSION"] = str(version)

The thing is every time I echo this in my Pipelines script

- echo $MY_PKG_VERSION

nothing shows up.  I tried creating an unsecured environmental variable in Pipelines (Settings->Pipelines->Environmental Variables) and accessed it via Python to update it, but this doesn't update it despite it working locally.

import os
import re

# Update the current version to the new version
current_version = os.environ['MY_PKG_VERSION']
release_version = int(re.sub(".\d+$", "", current_version))
patch_version = int(re.sub("^\d+.", "", current_version))
new_patch_version = patch_version + 1
new_version = "%d.%d" % (release_version, new_patch_version)

# Set version number as environmental variable
os.environ['MY_PKG_VERSION'] = new_version

print(current_version)
print(new_version)

If $MY_PKG_VERSION is set to 0.1, then current_verision==0.1 and new_version==0.2.  This script is run via Pipelines without failing and the print() outputs are displaying the correct values.

Ideally, I would be able to use the environmental variable set in my Python code within the Pipelines script by concatenating it to another string.

Any ideas on how to go about achieving this?

Cheers,

Grant

2 answers

Hello,

I am looking for changing the "APPLICATION_ENVIRONMENT" directly in bitbucket-pipelines.yml. Any idea how to do that?

 

We have specific branches that need to de deployed on specific environment on AWS Beanstalk?

 

Thank you for your help.

0 votes

Hi Grant,

Bitbucket Pipelines environment variables are currently immutable. So you are unable to modify them during your build. 

In regards to having the version numbers, you should follow this issue for updates: https://bitbucket.org/site/master/issues/12838/build-number-that-increments-on-every

If you need a unique identifier for you version and are okay with it not being an ordered number, you can use the environment variable $BITBUCKET_COMMIT.

Are there any other use-cases for you where you would want to modify an environment variable, or just for version numbers?

Thanks,

Phil

My use case is purely for modifying the version number, which is pretty critical. I see the ticket was opened six months ago. Is Atlassian intended to add the feature of either setting environmental variables or having an auto incriminating version number attainable also as an environmental variable? 

I know this is an old post but I have a similar requirement.

I use pipelines to create and update aws cloudformation stacks. After the pipeline has run and the stack created, I manually set a STACK_ID deployment variable. Future pipeline runs pass the id to the aws cli create-changeset call to create a changeset for the current stack, rather than create a new stack.

Perhaps I need to revisit this for an alternative solution but I think it would be a good option to have a pipeline command that can alter a repo/deployment variable.

Hi Craig,

In order to update a variable, for use in other Pipelines, you'll need to use the Bitbucket REST API.

I think having a specific pipeline command to do that is a great idea (it could also be extended to allow API calls to Bitbucket in general via a build). Could you open a feature request with details of your use-case.

Thanks,

Phil

For our use case, we need to set environmental variables from an artifact created in a previous step. This is used to bypass the limitation of not being able to reuse deployment environments between steps. 

I am not able to set env variables via os.environ. Is this still a limitation of bitbucket or am I missing something?

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Bitbucket

Contest: Share your custom Bitbucket Pipe and win

Announced in this blog, this holiday season we’re celebrating all things CI/CD and between now and the end of 2019 we’ll be showcasing content, use cases, feature announcements and more. One featur...

2,132 views 11 6
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you