2

I'm trying to figure out if there is a way to trigger a webhook inside a bitbucket-pipelines.yml file? I've looked around and the closest I've found to an answer is the answer located here Bitbucket webhook trigger after pipeline completes successfully. But the answer just says to "add the necessary commands", but no mention of what those commands are.

Just wondering if someone can elaborate as to what those commands would be to accomplish this?

The problem in case you're wondering that I'm trying to solve is we have a webhook that currently runs whenever a PR is created, which is fine. However, we have build steps that are run in our pipeline that can fail (tests, linting, etc...), and we don't want this webhook to run if the build fails. So my idea was to initiate the webhook at the end of the pipeline, so that if the build fails it won't initiate the webhook.

DNielsen
  • 43
  • 6

1 Answers1

4

You can also use an after-script in your step configuration. There is a BITBUCKET_EXIT_CODE variable available that you can use to determine the build result status.

pipelines:
  default:
    - step:
        name: Build and test
        script:
          - npm install
          - npm test
        after-script:
          - if [[ $BITBUCKET_EXIT_CODE -eq 0 ]]; then curl https://webhook-url ; else echo "Step failed"; fi
Alexander Zhukov
  • 4,357
  • 1
  • 20
  • 31
  • Will this send the same data to the webhook url that a conventional Bitbucket Webhook will, using the available event triggers, or do I need to send the data manually? – DNielsen Oct 23 '19 at 19:49
  • You will need to send the data manually if you want to do this from a pipeline – Alexander Zhukov Oct 24 '19 at 06:48
  • Thanks very much, I'd upvote your comment but I can't with my rep at the moment. – DNielsen Oct 24 '19 at 08:00
  • When I attempted this, I got `curl: not found`, so I ran my webhook in my docker container, which has curl installed. I also stored the webhook URL in a repository variable. – Chris Tyler Aug 27 '20 at 15:18
  • I can't find any Bitbucket documentation which indicates support for the if [[...]] conditional. Is it that this is documented and I can't find it (very possible), or if this is just a standard, generally understood, behaviour? – Bigears Nov 01 '22 at 15:25