Bitbucket “git-ftp” pipelines

You can read more about the awesome git-ftp here.

This pipeline is similar to the rsync pipeline but uses the logic of “push only the changed files” and this is possible due to git-ftp.

If you use Git and you need to upload your files to an FTP server, Git-ftp can save you some time and bandwidth by uploading only those files that changed since the last upload. It keeps track of the uploaded files by storing the commit id in a log file on the server. It uses Git to determine which local files have changed.

The setup

Add a bitbucket-pipelines.xml to your repository with the following code:

image: samueldebruyn/debian-git

pipelines:
  custom:
    production_manual:
      - step:
          script:
            - apt-get update
            - apt-get -qq install git-ftp
            - git ftp push --auto-init -v -u "$PRODUCTION_USER" -p "$PRODUCTION_PASS" --syncroot $BITBUCKET_CLONE_DIR --remote-root $PRODUCTION_FOLDER sftp://$PRODUCTION_HOST:22
    staging_manual:
      - step:
          script:
            - apt-get update
            - apt-get -qq install git-ftp
            - git ftp push --auto-init -v -u "$STAGING_USER" -p "$STAGING_PASS" --syncroot $BITBUCKET_CLONE_DIR --remote-root $STAGING_FOLDER sftp://$STAGING_HOST:22
  branches:
      production:
      - step:
          script:
            - apt-get update
            - apt-get -qq install git-ftp
            - git ftp push --auto-init -v -u "$PRODUCTION_USER" -p "$PRODUCTION_PASS" --syncroot $BITBUCKET_CLONE_DIR --remote-root $PRODUCTION_FOLDER sftp://$PRODUCTION_HOST:22
      staging:
      - step:
          script:
            - apt-get update
            - apt-get -qq install git-ftp
            - git ftp push --auto-init -v -u "$STAGING_USER" -p "$STAGING_PASS" --syncroot $BITBUCKET_CLONE_DIR --remote-root $STAGING_FOLDER sftp://$STAGING_HOST:22

You can also add a .git-ftp-ignore file in your repository to exclude any files from being synced.

bitbucket-pipelines.yml

You will have to add the following Repository Variables in your repository settings:

  • PRODUCTION_USER
    the SSH username for the production environment (i.e. deployproduction).
  • PRODUCTION_PASS
    the password of the production user.
  • PRODUCTION_HOST
    the host for the production environment (i.e. xkon.gr).
  • PRODUCTION_FOLDER
    the wp-content directory path for your production environment without a trailing slash (i.e. site/public_html/wp-content).
  • STAGING_USER
    the SSH username for the staging environment (i.e. deploystaging).
  • STAGING_PASS
  • the password of the staging user.
  • STAGING_HOST
    the host for the production environment (i.e. staging.xkon.gr).
  • STAGING_FOLDER
    the wp-content directory path for your staging environment without a trailing slash (i.e. staging/public_html/wp-content).