4

I am trying to set the deployment variable for a step using a variable that is passed to a custom pipeline. The idea is not to have to duplicate the custom pipeline as the only change is the deployment variables that are read from bitbucket settings.

The definition looks as follows, but throws an error

pipelines:
  custom:
    my-pipeline:
       - variables:   
          - name: deployment
       - step: 
           deployment: $deployment 
           script:
             - ...

Am I missing something here, or is the deployment key not allows to accept a variable?

2 Answers 2

4

Unfortunetly you can't use variables in the deployment field. Variables are only available in script fiels. However, your problem is easily solved in another way: anchors

For example:

definitions:
  steps:
    - step: &Test-step
        name: Run tests
        script:
          - npm install
          - npm run test
    - step: &Deploy-step
        name: Deploy to staging
        script:
          - npm install
          - npm run build
          - fab deploy
pipelines:
  default:
    - step: *Test-step
    - step:
        <<: *Deploy-step
        name: Deploy to Staging
        deployment: staging
        trigger: manual
  custom:
    Staging Deployment
      - step: *Test-step
      - step:
          <<: *Deploy-step
          deployment: staging
    Production Deployment
      - step: *Test-step
      - step:
          <<: *Deploy-step
          deployment: production
Sign up to request clarification or add additional context in comments.

2 Comments

I'm sorry, but the question was specifically if variables can be used as input for DEPLOYMENT variable, the answer has also been confirmed as NO by Atlassian. Your example doesn't solve my use case. As it stands right now, the answer is that you cannot use variables for the deployment key.
Defining deployment: staging multiple times gives me error: Configuration error The deployment environment 'staging' in your bitbucket-pipelines.yml file occurs multiple times in the pipeline. Please refer to our documentation for valid environments and their ordering.
0

This post is a bit old, and as per the documentation there is no way to dynamically change the deployment field, but I've figured out a way to get around this with the following:

pipelines:
  custom:
    deploy:
      - variables:
        - name: environment
          description: "Deployment environment"
          default: "stage"
          allowed-values:
            - demo
            - stage
            - live          
      - step:
        name: Deploy to demo          
        deployment: "demo"
        script:
          - echo "Deploying to demo"
        condition:
          state: environment == "demo"
      - step:
        name: Deploy to stage
        deployment: "stage"
        script:
          - echo "Deploying to stage"
        condition:
          state: environment == "stage"
      - step:
        name: Deploy to live
        deployment: "live"
        script:
          - echo "Deploying to live"
        condition:
          state: environment == "live"

This allows you to have one form for your custom pipeline, but still route it in such a way that the appropriate environment will get updated in the 'Deployments' page. Another added benefit is you have the ability to use different runners for the different steps. The only ugly side is that it will show the other environments as "skipped" in the build logs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.