GridDragon
GridDragon

Reputation: 3145

How to create a dynamic variable in BitBucket Pipeline

Is there a way to create a variable within a script in a pipeline? It seems like it should be possible, but I haven't been able to find any documentation. I keep getting redirected back to BitBucket's deployment or repository variables.

I have this pipeline which works fine:

image: amazon/aws-cli
definitions:
  steps:
    - step: &buildImage
        name: Build Docker Image
        services:
          - docker
        script:
          - aws configure set aws_access_key_id "${AWS_KEY}"
          - aws configure set aws_secret_access_key "${AWS_SECRET}"
          - aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 12345.dkr.ecr.us-east-1.amazonaws.com
          - docker build -t 12345.dkr.ecr.us-east-1.amazonaws.com/myProject:latest .
          - docker push 12345.dkr.ecr.us-east-1.amazonaws.com/myProject:latest

pipelines:
  default:
    - step: *buildImage

I based it on this example, but I'm struggling with the variables in the example.

When I modify the script section like this:

        script:
          - IMAGE = "12345.dkr.ecr.us-east-1.amazonaws.com/myProject"
          - TAG = "latest"
          - aws configure set aws_access_key_id "${AWS_KEY}"
          - aws configure set aws_secret_access_key "${AWS_SECRET}"
          - aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 12345.dkr.ecr.us-east-1.amazonaws.com
          - docker build -t $IMAGE:$TAG .
          - docker push $IMAGE:$TAG

I get this error

bash: IMAGE: command not found

I've been looking for a way to create variables like this but all I've been able to find is repository or deployment variables. Those don't seem to be what I need though, because I want to define the values at run-time.

Is there a way to achieve this?

Upvotes: 1

Views: 4621

Answers (1)

darwinawardee
darwinawardee

Reputation: 93

They are just bash variables as the error suggests. If you remove the spaces from the variable declaration lines and that should work.

script:
  - IMAGE="12345.dkr.ecr.us-east-1.amazonaws.com/myProject"
  - TAG="latest"
  - aws configure set aws_access_key_id "${AWS_KEY}"
  - aws configure set aws_secret_access_key "${AWS_SECRET}"
  - aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 12345.dkr.ecr.us-east-1.amazonaws.com
  - docker build -t $IMAGE:$TAG .
  - docker push $IMAGE:$TAG

Upvotes: 6

Related Questions