Trigger step in Bitbucket pipelines
Asked Answered
K

5

5

I have a CI pipeline in Bitbucket which is building, testing and deploying an application. The thing is that after the deploy I want to run selenium tests. Selenium tests are in an another repository in Bitbucket and they have their own pipeline.

Is there a trigger step in the Bitbucket pipeline to trigger a pipeline when a previous one has finished?

I do not want to do a fake push to the test repository to trigger those tests.

Kauffmann answered 14/4, 2018 at 15:19 Comment(0)
H
11

The most "correct" way I can think of doing this is to use the Bitbucket REST API to manually trigger a pipeline on the other repository, after your deployment completes.

There are several examples of how to create a pipeline here: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pipelines/#post

Copy + pasting the first example. How to trigger a pipeline for the latest commit on master:

$ curl -X POST -is -u username:password \
  -H 'Content-Type: application/json' \
 https://api.bitbucket.org/2.0/repositories/jeroendr/meat-demo2/pipelines/ \
  -d '
  {
    "target": {
      "ref_type": "branch", 
      "type": "pipeline_ref_target", 
      "ref_name": "master"
    }
  }'
Hendecasyllable answered 10/8, 2018 at 14:8 Comment(0)
N
4

Try out official component Bitbucket pipeline trigger: https://bitbucket.org/product/features/pipelines/integrations?p=atlassian/trigger-pipeline

You can run in after deploy step

script:
  - pipe: atlassian/trigger-pipeline:4.1.7
    variables:
      BITBUCKET_USERNAME: $BITBUCKET_USERNAME
      BITBUCKET_APP_PASSWORD: $BITBUCKET_APP_PASSWORD
      REPOSITORY: 'your-awesome-repo'
      ACCOUNT: 'teams-in-space'
Nitride answered 10/8, 2020 at 6:11 Comment(0)
A
3

according to their official documentation there is no "easy way" to do that, cause the job are isolated in scope of one repository, yet you can achieve your task in following way:

  1. create docker image with minimum required setup for execution of your tests inside
  2. upload to docker hub (or some other repo if you have such)
  3. use docker image in last step of you pipeline after deploy to execute tests
Aniela answered 17/4, 2018 at 7:32 Comment(0)
K
0

@BigGinDaHouse I did something more or less like you say.

My step is built on top of docker image with headless chrome, npm and git.

I did follow the steps below:

  1. I have set private key for the remote repo in the original repo. Encoded base 64. documentation. The public key is being set into the remote repo in SSH Access option in bitbucket menu.
  2. In the pipeline step I am decoding it and setting it to a file. I am also changing its permission to be 400.
  3. I am adding this Key inside the docker image. ssh-add

  4. Then I am able to do a git clone followed by npm install and npm test

NOTE: The entry.sh is because I am starting the headless browser.

  - step:
            image: kimy82/headless-selenium-npm-git
            script:
              - echo $key_in_env_variable_in_bitbucket | base64 --decode > priv_key
              - chmod 400 ./priv_key
              - eval `ssh-agent -s`
              - ssh-agent $(ssh-add priv_key; git clone [email protected]:project.git)
              - cd project
              - nohup bash /usr/bin/entry.sh >> out.log &
              - npm install
              - npm test
Kauffmann answered 17/4, 2018 at 22:36 Comment(0)
G
0

Top answers (this and this) are correct, they work.

Just adding that 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" for repos and pipelines...)

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

(Answering as I am lacking reputation for commenting)

Gurney answered 10/2, 2022 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.