Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitbucket pipeline - assign custom variables

I would like to assign custom variables using bash commands

image: alpine

pipelines:
  default:
      - step:
          name: docker-login
          services:
            - docker
          script:
            - docker login --username $DOCKER_USERNAME --password $DOCKER_PASSWORD
            - echo "logged in to docker hub"
            - echo $VERSION
          variables:
            VERSION: cat ./version.txt
      - step: 
          name: build-image
          services:
            - docker
          script:
            - echo "running docker image test"
      - step:
          name: 'set ssh env'
          script:
            - pipe: atlassian/ssh-run:0.4.0
              variables:
                SSH_USER: $SSH_USER
                SERVER: '$HOST'
                COMMAND: 'pwd'

I am not getting the output of the file.. what is the correct syntax?

like image 363
Ron Portnoy Avatar asked Oct 27 '25 23:10

Ron Portnoy


1 Answers

If your base image that step is running on doesn't support to set environment variables externally. you can not create variables like that. For example, your pipe image atlassian/ssh-run:0.4.0 has these environment variables set, so you can define. But docker-login step will be using image: alpine for its base and this base image doesn't have this environment variables set to be configured. There are three types of variables in Bitbucket (workspace variables, repository variables and deployment variables.) none of them meets your need in this use case. Because all these variable types are static and working as key/value.

We can try something else like below ;

image: alpine

pipelines:
  default:
      - step:
          name: docker-login
          services:
            - docker
          script:
            - docker login --username $DOCKER_USERNAME --password $DOCKER_PASSWORD
            - echo "logged in to docker hub"
            - export VERSION=$(cat ./version.txt)
            - echo $VERSION

Creating variables with linux commands will be helpful here. Also, pls keep in mind that these type of variables (in this case $VERSION variable) will only be identified in its step. If you want to carry it to other steps, you should use artifacts.

like image 158
Oguzhan Aygun Avatar answered Oct 29 '25 16:10

Oguzhan Aygun