Afonso Rodrigues
Afonso Rodrigues

Reputation: 143

Can I execute a pipeline from other pipeline in bitbucket pipelines?

I has the two repositories in bitbucket pipelines, both with pipelines enable.

How to execute the pipeline after the other pipeline complete?

Upvotes: 12

Views: 16121

Answers (5)

IAmHello
IAmHello

Reputation: 21

Edit: sorry I misread the question - this approach only works with pipelines in the same repo. I have not tested this, but it should be possible to make this work if you are able to set up your build environment as follows:

  • have a "main" repo, which includes the (two) other repos as git submodules, and
  • move the pipeline definitions from the (two) submodules into a single pipelines file in the main repo - these can then refer to each other with YAML anchors

Original answer

Alternatively, you could to use YAML anchors to copy the steps from the other pipeline.

The anchor '&' defines a chunk of configuration

The alias '*' refers to that chunk elsewhere

You can use overrides with the characters '<<:' to add more values, or override existing ones.

Example with build-test and deploy anchors (note that the separate definitions key is optional, for readability only - anchors may also be defined directly on a step under the pipelines key):

definitions: 
  steps:
    - step: &build-test
        name: Build and test
        script:
          - mvn package
        artifacts:
          - target/**
    - step: &deploy
        name: Deploy
        deployment: test
        script:
          - ./deploy.sh target/my-app.jar

pipelines:
  branches:
    develop:
      - step: *build-test
      - step: *deploy
    main:
      - step: *build-test
      - step:
          <<: *deploy
          deployment: production
          trigger: manual

Upvotes: 0

marisa79
marisa79

Reputation: 23

I don't have enough reputation to reply to @Ali Mert Çakar so needs to go from a reply.

If you go to the link at least of today you can also use a BITBUCKET_ACCESS_TOKEN - The access token for the repository which is required unless BITBUCKET_USERNAME and BITBUCKET_APP_PASSWORD are used. This would solve your question.

Upvotes: 0

jvleminc
jvleminc

Reputation: 81

Just confirming that the above answers work, but we found out (after a lot of trial and error) that the user executing the pipeline must have WRITE permissions on the repo where the pipeline is invoked (even though his app password permissions were set to "WRITE").

Also, this works for executing pipelines in Bitbucket's cloud or on-premise, through local runners.

(Answering as I am lacking reputation for commenting)

Upvotes: 6

Oleksandr Kyrdan
Oleksandr Kyrdan

Reputation: 376

Use "Bitbucket trigger pipeline" pipe in your pipeline's final step. You can easily setup this pipe:

script:
  - pipe: atlassian/trigger-pipeline:4.1.5
    variables:
      BITBUCKET_USERNAME: $BITBUCKET_USERNAME
      BITBUCKET_APP_PASSWORD: $BITBUCKET_APP_PASSWORD
      REPOSITORY: 'your-awesome-repo'

Where variables:

$BITBUCKET_USERNAME - Bitbucket user that will trigger the pipeline. Note, that this should be an account name, not the email.

$BITBUCKET_APP_PASSWORD - Bitbucket app password of the user that will trigger the pipeline. Remember to check the Pipelines Write and Repositories Read permissions when generating the app password.

This pipe will trigger the branch pipeline for master in your-awesome-repo. This pipeline will continue, without waiting for the triggered pipeline to complete.

Upvotes: 25

Recoba20
Recoba20

Reputation: 324

As a final step of your almost completed pipeline, you could just trigger the other one by using BitBucket REST API.

Trigger a Pipeline for a branch

Upvotes: 3

Related Questions