Skip to content

Instantly share code, notes, and snippets.

@fvoges
Created December 10, 2018 10:44
Show Gist options
  • Save fvoges/c5ce4c4bd7f32b5a5a0c87112cf9bce0 to your computer and use it in GitHub Desktop.
Save fvoges/c5ce4c4bd7f32b5a5a0c87112cf9bce0 to your computer and use it in GitHub Desktop.
How to configure the webhook for Puppet Enterprise's Code Manager in BitBucket server

BitBucket

  1. Install BitBucket
  2. Make a Project called puppet (with a short name of PUP)
  3. Create a repository called control-repo
    • Create a user called r10k with a password of puppet.
    • Make the r10k user an admin of the PUP project.
  4. Either use the admin user to test pushing code, or create a user for yourself and add your SSH key to that user.
    • If making a user for yourself, give your user account read/write or admin privilege to the PUP project.

Using The Post-Recieve Hook Plugin (Requires Java KS change)

This is the preferred method.

NOTE: BitBucket has a new built-in webhook feature. It's similar to the functionality provided by the plugin. But, as of this writing (December 2018), Code Manager doesn't support the built-in webhooks and still requires the functionality provided by the plug-in.

  1. Install the following BitBucket Server plugin by logging into the web GUI of the Stash server and going to Find new add-ons.
  2. Add the Puppet Master's CA cert to the Java keystore on the BitBucket server:
    • Determine the $JAVA_HOME value used for BitBucket by looking in: /opt/atlassian/bitbucket/<version>/bin/setenv.sh.

      • You can also look at the System Information page of the Web GUI. In my case, it's /opt/atlassian/bitbucket/4.3.2/jre.
    • Run the following command and replace $JAVA_HOME with the path from the previous step:

      $JAVA_HOME/bin/keytool -import -alias puppet-ca -file /etc/puppetlabs/puppet/ssl/certs/ca.pem -keystore $JAVA_HOME/lib/security/cacerts
      • When asked for a password, use changeit.
  3. Restart the BitBucket service (check the Atlassian documentation to find the service name in your OS).
  4. Configure the hook on your control repo.
    • Click the Hooks tab under the repo's settings.

    • Click the pencil icon next to Post-Receive WebHooks

    • The URL to drop in should be in the format of:

      https://puppet-master:8170/code-manager/v1/webhook?type=stash&token=<TOKEN>
      • Replace <TOKEN> with the RBAC Token that was generated automatically for you (see /etc/puppetlabs/puppetserver/.puppetlabs/deploy_token)

Using the External Hook Plugin

If, for some reason, you can't install the plugin or modify the Java Key Store, you can also use this method.

Be aware that, with this method, there's no way to inform Code Manager which branch was modified, so you have to hard code the branch name in the configuration. The alternative is to redeploy all branches (Puppet environments)

  1. Install the following BitBucket Server plugin by logging into the web GUI of the Stash server and going to Find new add-ons.

  2. Configure the hook on your control repo.

    • Click the Hooks tab under the repo's settings.
    • Click the pencil icon next to External Post-Receive WebHooks
    • Point to an executable script. For example:
      • Deploy only the production branch/environment:

        #!/usr/bin/env bash
        echo "Invoking a deployment from BitBucket... "
        curl -v -k -X POST -H 'Content-Type: application/json' \
        https://puppetmaster:8170/code-manager/v1/deploys?token=`cat /var/opt/deploy_token.txt` \
        -d '{"environments": ["production"], "wait": true}' | cat
      • Deploy all environments:

        #!/usr/bin/env bash
        echo "Invoking a deployment from BitBucket... "
        curl -v -k -X POST -H 'Content-Type: application/json' \
        https://puppetmaster:8170/code-manager/v1/deploys?token=`cat /var/opt/deploy_token.txt` \
          -d '{"deploy-all": true, "wait": true}' | cat
  3. You will need to make sure that the BitBucket server has a deploy token avaliable at /var/opt/deploy_token.txt.

Using the RampUp profile to configure it automatically

A Rampup BitBucket profile is available here: PuppetLabs-RampUpProgram/rampup_profile_bitbucket_server

A working Vagrant example of using Puppet Enterprise with Code Manager and Stash is avaliable here: petems/pe-bitbucket-vagrant-stack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment