Deploy using SCP

This guide will show you how to deploy your files to a remote host using the SCP-deploy pipe in Bitbucket Pipelines.

For this example we created simple ReactApp and deployed it to production with the SCP-deploy pipe.

A full end-to-end example is available in this repository if you prefer a more hands-on experimentation with deploying to your host using Pipelines and Pipes.

Prerequisites:

  • Remote host with ssh access

  • Bitbucket repository

Step 1: Configure your Bitbucket repository for the project

Add your credentials and configuration settings in Bitbucket as variables (see: how to configure Pipelines variables).

Name

Value

USER

The user that is configured on the remote host.

SERVER

The remote host to transfer the files to.

You can define these variables at the deployment environment, repository, or team level.

Step 2: Configure your SSH keys

Set up an SSH key in Bitbucket Pipelines or provide it as a secured variable: SSH_KEY (see using multiple ssh keys). By default, the SCP-deploy pipe will use your configured SSH key and known_hosts file.

Step 3: Configure your server

On your remote host (sometimes called a production node):

  • Allow users to connect on HTTP (port 80).

  • Configure ssh keys to allow access.

Here's a configuration example using NGINX on the server. Note: this configuration is intended for demo purposes only.

1 2 3 4 5 6 7 8 9 10 11 12 13 server { listen 80; listen [::]:80; root /var/www/scp-deploy/html; index index.html index.htm; server_name <your_host_public_DNS_name>, <example.com>; location / { try_files $uri $uri/ =404; } }

Step 4: Setup your CI/CD configuration

Edit your bitbucket-pipelines.yml file. To find out more about the SCP-deploy pipe, select it in the right of the editor.

Add these steps to bitbucket-pipelines.yml :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 image: node:10.15.3 pipelines: default: - step: name: Build and test caches: - node script: - npm install - npm test - npm run build artifacts: - build/** - step: name: Deploy artifacts using SCP to PROD deployment: production script: - pipe: atlassian/scp-deploy:0.3.3 variables: USER: $USER SERVER: $SERVER REMOTE_PATH: '/var/www/scp-deploy/html' LOCAL_PATH: 'build/*'

This configuration allows you to run tests, build, and deploy your files to the remote host using the SCP-deploy pipe after each push to the Bitbucket repository. Learn more about how to edit and configure your Bitbucket pipelines configuration.

Note: You can use file globbing in the LOCAL_PATH to specify the files you want to deploy.

For example, you can use $BITBUCKET_CLONE_DIR default variable - the absolute path of the directory that the repository is cloned into within the Docker container:

1 2 LOCAL_PATH: '${BITBUCKET_CLONE_DIR}'     # copy the current directory and all included files. Keeps the parent directory structure. LOCAL_PATH: '${BITBUCKET_CLONE_DIR}/*'    # copy the contents from the current directory.

Or you can specify your "'custom/local/path':

1 2 LOCAL_PATH: 'custom/local/path'    # copy your custom directory and all included files. Keeps the parent directory structure. LOCAL_PATH: 'custom/local/path/*'   # copy the contents from your custom directory.

If you specify the '/' (slash) as the LOCAL_PATH variable, it will copy all files from the root build container filesystem. We recommend using a relative directory custom/local/path or ${BITBUCKET_CLONE_DIR}/* if you want to copy all files from the build directory.

Step 5: Deploy application to production

Push your application’s code to your Bitbucket repository which will trigger the pipeline. You can then select Pipelines  to check pipeline progress. 

Now you can focus on building your great application, everything else is handled with Bitbucket Pipelines.

Additional Help