Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dotherightthing/35f64b24636caa8278d62b6752f54a81 to your computer and use it in GitHub Desktop.
Save dotherightthing/35f64b24636caa8278d62b6752f54a81 to your computer and use it in GitHub Desktop.
[Automate deployments from a Bitbucket repository to Sitehost using Rsync] Configure the script and SSH key for a Bitbucket pipeline which deploys via Rsync. #bitbucket #pipelines #deployment #ssh #devops #wordpress #rsync

Automate deployments from a Bitbucket repository to Sitehost using Rsync

Created: 2019.08.08

1. Generate a Bitbucket SSH key

This will be used to authorise interactions with the Sitehost server.

  1. Open repository in Bitbucket
  2. Scroll down to Pipelines, then click SSH keys
  3. Click Generate keys
  4. Click Copy Public key

2. Register the SSH key with the target server

  1. Log into Sitehost
  2. Choose the correct account from the dropdown
  3. Click SSH Keys
  4. Click Add SSH Key
  5. Enter a Label: Bitbucket deployment to Production
  6. Paste in the Public Key
  7. Click Add SSH Key
  8. Click Containers, then SSH & SFTP
  9. Take a note of the User name
  10. Take a note of the Server IP
  11. Click the 3 dots to Manage the existing user
  12. Tick the Bitbucket deployment to Production checkbox under SSH Keys
  13. Click Save changes
  14. Wait until the spinner at the end of the table row is replaced with the 3 dots menu icon (The SSH user is now in the process of being updated message does not disappear).

3. Verify that the SSH key permits access to the target server

  1. Open repository in Bitbucket
  2. Scroll down to Pipelines, then click SSH keys
  3. Under Known Hosts, enter the Server IP noted in step 2
  4. Click Fetch
  5. Click Add (or similar)

4. Create and populate deployment variables for Bitbucket

These will be used in the Pipeline, trigged by commits to the repository.

  1. Scroll down to Pipelines, then click Deployments
  2. Click the desired environment heading (e.g. Production) to reveal additional UI
  3. Scroll down to Variables, then click Add
  4. Create SFTP_USERNAME and populate it with the User name from step 2
  5. Create SFTP_SERVER and populate it with the Server IP from step 2
  6. Create SFTP_PATH and format it as follows (for a WordPress site running in a Sitehost 'container'): container/application/public/wp-content/themes/theme-name

5. Create the deployment script for Bitbucket's pipeline engine

Create or update the bitbucket-pipelines.yml file in the repo root:

# Theme deployment.
# Deployment $VARIABLES are managed in Bitbucket
#
# @package TwentynineteenDotherightthing
# @since 0.1.0
# @uses https://bitbucket.org/atlassian/sftp-deploy/src/master/
#
# pipelines: marks the beginning of all your pipeline definitions.
# default: contains the steps that will run on every push.
# step: each step starts a new Docker container that includes a clone of your repository, and then runs the contents of your script section inside it.
# script: a list of commands that are executed in sequence.

image: frozentech/bitbucket-pipelines-php-mysql

pipelines:

    # string required here, need to increment for 4.* etc
    3.*:
    - step:
       name: Build theme
       script:
       # ...
       
       # Make artifacts available in Pipeline UI
       artifacts:
         # The Gulpfile pipes the contents of the distDir into a zip
         # then renames this to release-n.n.n.zip
         # then outputs it to the build root.
         # Gulp handles this, for compatibility with Travis.
         # Note: * is used, as ${BITBUCKET_TAG} causes the artifact to disappear
         - wpdtrt-dbth/**
         - release-*.zip

    - step:
       name: Deploy theme to server
       deployment: production
       script:
         # SFTP deployment to server
         # Rsync only transfers updated files
         - pipe: atlassian/rsync-deploy:0.3.1
           variables:
             USER: $SFTP_USERNAME
             SERVER: $SFTP_SERVER
             REMOTE_PATH: $SFTP_PATH
             LOCAL_PATH: wpdtrt-dbth/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment