HTTPS SSH

Overview

The goal of this repository is to provide a high quality, professional example for setting up a Continuous Integration pipeline using BitBucket Pipelines to build and publishes a maven artifact (Java Library) to a maven repository.

It provides two triggers:

  1. On all commits to master, trigger a mvn deploy
  2. After the first trigger has successfully run, you can manually perform a mvn release:prepare which will add two commits to the repository.
    • These two commits will be picked up by the first trigger and will result in deploying the new snapshot and release version to your maven repository.

This is one of two examples that were used in the article Continuous Integration: A Complete Guide to Using BitBucket Pipelines With Private Maven Repositories

The second example, for reading from a Maven Repository with BitBucket Pipelines can be found here: TODO: Add link.

Universal Example

We hope this examples provides a benefit to all Apache Maven and BitBucket Pipeline users.

While this example uses a CloudRepo private maven repository to deploy artifacts, any other maven compatible repository can be used with minor modifications.

Once this library has been published to a maven repository, the library can be added to any other Maven project.

Getting Started

For an in-depth walk through, please read our Continuous Integration: A Complete Guide to Using BitBucket Pipelines With Maven Repositories article.

If you just want to get started perform the following steps:

  1. Fork this repository to your own BitBucket account.
  2. Enable BitBucket Pipelines in your newly Forked Repository
    • Settings | Pipelines (Settings) | Enable Pipelines
    • Enabling BitBucket Pipelines
    • Note: The first build trigger should not fire until a new commit has been pushed after pipelines are enabled (which we'll do in a later step).
  3. Add Secure Environment Variables for Your Pipeline
    • Settings | Pipelines (Enviroment Variables)
    • Add the Following Enviroment Variables
    • CLOUDREPO_USERNAME
      • This is the username for pushing and pulling artifacts from your maven repository. This will end up in your settings.xml file.
      • Optional: To prevent logging of this username you can set the enviroment variable to secret.
    • CLOUDREPO_PASSPHRASE
      • This is the passphrase for pushing and pulling artifacts from your maven repository. This will end up in your settings.xml file.
      • RECOMMENDED: To prevent logging of this passphrase, ensure that you set the enviroment variable to secret.
    • GIT_USER_EMAIL
      • Used only when the mvn release:prepare trigger is run. This is the user email by Git used when Pipelines commits back to this repository.
      • Note: This does not have to correspond to an actual user's email, it will only show up in your Git commit logs as the author of any commit performed by BitBucket Pipelines.
    • GIT_USER_NAME
      • Used only when the mvn release:prepare trigger is run. This is the user name used by Git when Pipelines commits back to this repository.
      • Note: This does not have to correspond to an actual user's name, it will only show up in your Git commit logs as the author of any commit performed by BitBucket Pipelines.
    • BitBucket Pipelines Environment Variables
  4. (For Maven Releases Only): Create SSH KeyPair
    • We a need key pair to allow BitBucket Pipelines to git push back to this repo when we run are releasing an artifact via a mvn release:perform command.
    • Settings | Pipelines (SSH keys) | Generate Keys
    • BitBucket Pipeline Create SSH KeyPair
  5. (For Maven Releases Only): Add Public Key to Your Account's list of SSH Keys
    • Copy the public key generated in the previous step.
    • In your Account Settings (not the BitBucket Pipelines Settings), add a new SSH Key
    • Settings | Security | SSH Keys | Add Key
    • Paste the public key from the BitBucket Pipeline here
    • BitBucket Add SSH Key Pair
  6. Update the <distributionManagement> section of pom.xml to point to your Maven Repository URLs for both a Snapshot and Release Maven repository.
  7. Commit your changes to pom.xml and push them to your forked repository, this should trigger a new build.
  8. You can view the status of Pipelines by going to the Pipelines menu option in your BitBucket Repository Page.
  9. Pipelines should automatically start building any commits to the master branch.

Validating Deployment

Validate Successful build and deployment in two places:

  1. BitBucket Pipelines Status
  2. Maven Repository Storage

BitBucket Pipelines Status

You can refer to the Pipelines menu option for your BitBucket Repository to see the status of your builds.

After successful execution of this example, you should see the following after your first successful build:

First BitBucket Pipeline Step Execution

When you are ready to create a release version of your artifact, click the 'Run' button in the 'Create Release Version' step. This will kick of a mvn release:perform which will create two new commits on your master branch.

When the 'Create Release Versions' step is done, the build results will look like the following:

Second BitBucket Pipeline Step Execution

When you go back to the Pipeline Summary you will see that two new builds have been completed, one for the new release version and another for the next version of the snapshot.

Final BitBucket Pipeline Summary

If your BitBucket Pipeline history looks like this then you've completed this example successfully.

Maven Repository Storage

Since these pipelines deployed Snapshot artifacts to a Maven Repository, log into your repositories user interface to verify the artifacts have been successfully deployed.

We'll show examples from the CloudRepo Admin Portal below:

Snapshot Release

Navigating to the maven-snapshots repository in CloudRepo, we can validate that the next version of the snapshot has been release:

CloudRepo Snapshot Release

Release Version

Navigating to the maven-releases repository in CloudRepo, we can validate the 1.0.0 release version has been successfully deployed.

CloudRepo Release Version

Thank You

While we do our best to ensure a high quality example, we might have missed something. If you hit a snag, please open an issue so that we can fix it for others who are seeking similar knowledge.

This example was brought to you by the engineering team at CloudRepo. If you want to contact us directly, visit our Contact Us Page

Please feel free to share on Social Media, Medium, or other channeles.