Can I execute a pipeline from other pipeline in bitbucket pipelines?
Asked Answered
S

5

11

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

How to execute the pipeline after the other pipeline complete?

Shotgun answered 17/4, 2020 at 19:47 Comment(0)
L
23

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.

Lundell answered 7/5, 2020 at 17:30 Comment(2)
kinda feels limited, what happens pipeline to when i leave my company and my account is deactivated?Austerlitz
@AliMertÇakar you can use a ACCESS TOKEN from the target repo to support your company sustain even when you leaveSavino
O
6

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)

Omaomaha answered 10/2, 2022 at 13:48 Comment(0)
M
3

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

Monophysite answered 19/4, 2020 at 20:14 Comment(1)
I feel that using the above mentioned pipe is more in the spirit of a pipeline. Yes, that pipe will probably do this internally.Huei
W
0

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.

Wilheminawilhide answered 11/5, 2023 at 9:25 Comment(0)
H
0

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
Harbaugh answered 11/4 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.