Tools2023-04-24

Triggers in Bitbucket Pipeline Simplified

Automate workflows and streamline software development processes with Bitbucket pipelines. Also, read how to trigger your Bitbucket pipeline.
Triggers in Bitbucket Pipeline Simplified

Bitbucket pipelines is a feature provided by Bitbucket to automate the building, testing, and deployment of code. If you’re fairly new, Bitbucket is a cloud-based Git tool for teams and developers who use Jira. It is an integrated continuous integration and continuous deployment (CI/CD) tool that helps software development teams to streamline their workflows, resulting in faster and more efficient software delivery.

Bitbucket pipelines can be triggered manually or automatically whenever there is a change in the code repository. In this article, we will discuss in detail the Bitbucket pipeline triggers and how to use them to automate your workflows.

Overview of Bitbucket Pipeline Triggers

Various event triggers, such as push, pull requests, tags, schedules, and custom even events can trigger bitbucket pipelines. The most common event that triggers a pipeline is a push event, which occurs when code is pushed to the repository. This event is detected by the Bitbucket webhook, which sends a notification to the Bitbucket pipelines service to start the pipeline. A pipeline can also be triggered by a pull request event, which occurs when a pull request is created, updated, or merged.

In addition, Bitbucket pipelines can also be triggered by tags. A tag is a label assigned to a specific commit in the repository. When a new tag is pushed to the repository, it can trigger a pipeline to build and deploy the tagged version of the code.

Schedules can also trigger bitbucket pipelines. Schedules allow you to define a time interval when a certain pipeline should be triggered automatically. For example, you can schedule a pipeline to run every night at midnight to perform a nightly build and test of your code.

Finally, Bitbucket pipelines can be triggered by custom events. Custom events are events that are triggered by external systems or scripts. For example, you can use a custom script to trigger a pipeline whenever a new version of a library is released.

Benefits of Bitbucket Pipelines

  1. Developer flexibility: Developers can choose to increase additional build capacity at any time during the development process.
  2. Workload efficiency: With available features ready to get you going and up to speed with your development workflow, reducing build times and giving your more access to the product.
  3. Concurrency is available for all your Pipeline processes: Run builds concurrently across member teams.

Configuring Bitbucket Pipeline Triggers

To configure Bitbucket pipeline triggers, you need to create a bitbucket-pipeline.yml file in the root directory of your repository. This file contains the configuration for your pipelines, including the triggers that will be used to start them. Here’s an example of a bitbucket-pipeline.yml file:

Configuring Bitbucket Pipeline Triggers

In the file example above, we have defined two pipelines. The first pipeline is the default pipeline, which will be triggered by the push event. This pipeline contains a single step that prints the message "Hello, World!" to the console.

The second pipeline is triggered by the push event, but it will only run for branches that match the pattern feature. This pipeline also contains a single step that prints a message to the console.

As you can see, the bitbucket-pipeline.yml file is very flexible and allows you to define complex workflows that can be triggered by a variety of events.

Using Bitbucket Pipeline Triggers

Now that we have covered the basics of Bitbucket pipeline triggers and how to configure them, let's look at some more advanced use cases.

1. Triggering Pipelines for Pull Requests

One common use case for Bitbucket pipeline triggers is to trigger pipelines for pull requests. Pull requests are an important part of the code review process, and it's important to ensure that code changes are tested before they are merged into the main branch.

To trigger a pipeline for pull requests, you can use the pull-requests keyword in your bitbucket-pipeline.yml file.

Here's an example:

Triggering Pipelines for Pull Requests

In this example, we have defined a pipeline that will run for all pull requests. The ** pattern matches all branches and pull requests. The pipeline contains a single step that prints a message to the console.

2. Triggering Pipelines for Tags

Another important use case for Bitbucket pipeline triggers is to trigger pipelines for tags. Tags are used to mark specific versions of your code, and it's important to ensure that the tagged versions are properly built and tested.

To trigger a pipeline for tags, you can use the tags keyword in your bitbucket-pipeline.yml file.

Here's an example:

Triggering Pipelines for Tags

3. Triggering Pipelines on a Schedule

Another use case for Bitbucket pipeline triggers is to trigger pipelines on a schedule. This is useful for running automated tests or other tasks on a regular basis, such as nightly builds or weekly reports.

To trigger a pipeline on a schedule, you can use the schedules keyword in your bitbucket-pipeline.yml file.

Here's another example:

Triggering Pipelines on a Schedule

In this example, we have defined a pipeline that will run every night at midnight for the master branch. The pipeline contains a single step that prints a message to the console.

4. Caching

Caching is an advanced feature in Bitbucket pipelines that can significantly speed up your pipeline execution time. When you run a pipeline, it often involves downloading dependencies, compiling code, and running tests. Caching allows you to store the results of these tasks between pipeline runs, so you don't have to repeat them every time you run a pipeline.

To enable caching in your pipeline, you need to specify the cache directories, as can be found in the pipeline.yml file below.

Caching

With caching, you have defined a pipeline that includes a step to build and test the code, and also specified that you want to cache the Gradle directory, which is where the Gradle dependencies are stored. Bitbucket pipelines will automatically check if the cache exists before running the step, and if it does, it will use the cached results instead of downloading the dependencies again, making your workflow more efficient.

5. Parallelism

Parallelism is another advanced feature in Bitbucket pipelines that can help you speed up your pipeline execution time. When you run a pipeline, you can split the tasks into multiple parallel steps, which can all run concurrently, thus reducing the overall pipeline execution time.

To enable parallelism in your pipeline, you need to specify the number of parallel steps you want to run in your pipeline.yml file.

Here's an example:

Parallelism

6. Having Custom Docker Images

Custom Docker images allow you to use a specific version of the software or libraries that you need for your pipeline. Docker images are lightweight and portable, making them ideal for use in pipelines. To use a custom Docker image in your pipeline, you need to create a Dockerfile that specifies the software and libraries you need, and then build the image. Once you have built the image, you can specify it in your pipeline.yml file.

Here's an example:

Having Custom Docker Images

In this example, we have specified the my-custom-image Docker image in our pipeline. The pipeline includes a step to build and test the code using the specified image.

Conclusion

Bitbucket pipeline triggers are a powerful feature that can help you automate your workflows and streamline your software development process. With Bitbucket pipelines, you can easily configure your pipelines to be triggered by various events, such as push, pull requests, tags, schedules, and custom events. By using pipeline triggers, you can ensure that your code is properly built, tested, and deployed, resulting in faster and more efficient software delivery.

With Bitbucket pipeline triggers, you can ensure that your code is properly built, tested, and deployed, resulting in faster and more efficient software delivery.

However, simply using Bitbucket, might not be enough to get the best out of your project space, an engineering analytics tool can equip you with the data-driven insights you need to place your team on top. Hatica offers metrics across 13 dashboards, powered by CI/CD tools, JiraGitHub, Azure, CircleCI and GitLab. By collating tool activities at one place, Hatica helps teams streamline their workflow and improve productivity.

Subscribe to the Hatica blog today to read more about unblocking developers, and boosting productivity with engineering analytics. 

Subscribe to Hatica's blog

Get bi-weekly insights straight to your inbox

Share this article:
Table of Contents
  • Overview of Bitbucket Pipeline Triggers
  • Benefits of Bitbucket Pipelines
  • Configuring Bitbucket Pipeline Triggers
  • Using Bitbucket Pipeline Triggers
  • 1. Triggering Pipelines for Pull Requests
  • 2. Triggering Pipelines for Tags
  • 3. Triggering Pipelines on a Schedule
  • 4. Caching
  • 5. Parallelism
  • 6. Having Custom Docker Images
  • Conclusion

Ready to dive in? Start your free trial today

Overview dashboard from Hatica