---
title: "Bitbucket Pipelines"
canonical: "https://kb.uconn.edu/space/IKB/26496827477/Bitbucket%20Pipelines"
format: markdown
---
Bitbucket pipelines is Atlassian’s CI/CD tool that is integrated with the cloud version of Bitbucket. It can aid users in creating powerful, automated workflows and deployments. 

## Pipelines:

1. If you are deploying to an external server, make sure you contact (Zach Robert) to allow access to the university’s firewall for port 22 to your server from [Bitbucket Cloud](https://bitbucket.org/uconnbitbucket).

| **Description** | **Screenshot** |
| --- | --- |
| Setting up a repository to use with Pipelines.  We’ll use State Licensure as an example.  Click on **Repository settings**. | ![image](media://2586541a-2928-4f5c-a6a4-5e7a6d0b0fe8) |
| Click on **Settings**, then slide the toggle to Enable Pipelines | ![image](media://a2de3a04-c3d1-43ca-b65b-051897ac7d13) |
| Click **SSH Keys** to enable communication with the server(s) you’ll be deploying your files to.  Enter server name or i/p address then click **Fetch**. Once you see the returned Fingerprint, click **Add Host**. | ![image](media://41e179ae-ae9c-4522-9bca-207745542272) |
| Click **Deployments **to setup the environments you’ll be using.  For this example, we’ll use **Test **and **Production**.  We’ll see how it’s used in the file, **bitbucket-pipelines.yml**.  You can add additional environments, if needed. | ![image](media://e42ca294-4d99-416d-9443-88ce3a361426) |
| Click the down mark to expand the Test section. We’ll add two variables, **SFTP_USER** and **SFTP_SERVER **which will be referenced in the **bitbucket-pipelines.yml** file.  You have the ability to secure the value, if needed. | ![image](media://bffc5c9d-8a99-4594-afee-96906f162486) |
| Click the down mark to expand **Production **section.  You can add the same variable names as in Test, if you want, to be consistent with the **bitbucket-pipelines.yml** file. | ![image](media://2cbe0384-7033-4783-ae28-5b89faae807b) |
| Example **bitbucket-pipelines.yml** file.  This file should be put in the root directory of your files.  
  
Reference Site: [https://support.atlassian.com/bitbucket-cloud/docs/step-options/](https://support.atlassian.com/bitbucket-cloud/docs/step-options/)   
  
Layout of file:<br>```
options:
```<br>**size:**  the minimum required is 4x.  4x and 8x pipelines size options are only available for builds running under a paid Bitbucket Cloud plan (Standard or Premium).<br>**runtime:**  the runtime configuration to be applied to the step.  Use cloud option.  Set atlassian-ip-ranges to true.  
**** Only use the above option if you are deploying your code to a different server.**<br>```
image:
```<br>**image**: to use a different Docker image.<br>```
definitions:
```<br>**definitions**: starts the YAML file by declaring that we will define steps below<br>```
steps:
```<br>**steps**: any number of steps or deployment options that will be defined below<br>```
step:
```<br>**step**: a deployment option, like **deploy-test** or **deploy-prod**.<br>1. **name**: a name for the step or stage.
2. **deployment**: sets the environment for a deployment step or stage.  It uses the variable setup in the Deployment step above.
3. **script**: the script run by the deployment options, containing “pipes” that allow for integration with other services. In the example to the right, we are using two pipes: an SSH and an SCP pipe. Atlassian provides other pipes as needed. Documentation is online.<br>```
pipe: name-of-pipe:version
```<br>**pipe**: list the name and version of the pipe being used, under which contains the variables that will be passed to the pipe. Please see Bitbucket's list of pipeline integrations for all pipe options<br>- The **ssh-run pipe** includes the required arguments, many of which should have been defined as variables described earlier. The **COMMAND **argument accepts the actual command that will be run by the pipe. In this case, we are issuing an `rm -rf`command for the `/Apps/StateLicensure/*` directory  
[https://bitbucket.org/atlassian/ssh-run/src/master/](https://bitbucket.org/atlassian/ssh-run/src/master/)
- The **scp-deploy pipe** includes similar arguments as the ssh-run pipe, but also requires a **REMOTE_PATH **and a **LOCAL_PATH **to be defined. Pipelines has a number of variables that need no definition, including **$BITBUCKET_CLONE_DIR**, which returns the absolute path of the repository directory.   
[https://bitbucket.org/atlassian/scp-deploy/src/master/](https://bitbucket.org/atlassian/scp-deploy/src/master/)  
[https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/](https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/)
  - This pipe copies the contents of the entire repository into the `/Apps/StateLicensure/` directory. We also pass an optional **DEBUG **variable of ‘true’ to enable better debugging in case an error is encountered
- Taken together, these two commands delete the content of the remote `/Apps/StateLicensure/` directory on the test server and replace it with the repository contents. | ```xml
# Template PHP Build

options:
  size: 4x
  runtime:
    cloud:
      atlassian-ip-ranges: true

image: php:7.4.33

definitions:
  steps:
    - step: &build
        name: build the app
        script:
          - mkdir build
          - tar --exclude=build --exclude='bitbucket-pipelines.yml' -czvf build/slic.tar.gz * 
        artifacts:
          - build/**
    - step: &deploy-test
        name: deploy to test
        deployment: test
        script:
          - pipe: atlassian/scp-deploy:1.4.1
            variables:
              USER: $SFTP_USER
              SERVER: $SFTP_SERVER
              SSH_KEY: $DEPLOY64_KEY
              REMOTE_PATH: '/Apps/'
              LOCAL_PATH: '${BITBUCKET_CLONE_DIR}/build/slic.tar.gz'
              DEBUG: 'true'
          - 'echo ''tar -xzf /Apps/slic.tar.gz -C /Apps/StateLicensure'' > build-app.sh'
          - 'echo ''chown -R deploy:deploy /Apps/StateLicensure/'' >> build-app.sh'
          - 'echo ''rm -rf /Apps/slic.tar.gz'' >> build-app.sh'
          - pipe: atlassian/ssh-run:0.2.2
            variables:
              SSH_USER: $SFTP_USER
              SERVER: $SFTP_SERVER
              SSH_KEY: $DEPLOY64_KEY
              PORT: '22'
              MODE: 'script'
              COMMAND: 'build-app.sh'
    - step: &deploy-production
        name: deploy to production
        deployment: production
        script:
          - pipe: atlassian/scp-deploy:1.4.1
            variables:
              USER: $SFTP_USER
              SERVER: $SFTP_SERVER
              SSH_KEY: $DEPLOY64_KEY
              REMOTE_PATH: '/Apps/'
              LOCAL_PATH: '${BITBUCKET_CLONE_DIR}/build/slic.tar.gz'
              DEBUG: 'true'
          - 'echo ''tar -xzf /Apps/slic.tar.gz -C /Apps/StateLicensure'' > build-app.sh'
          - 'echo ''chown -R deploy:deploy /Apps/StateLicensure/'' >> build-app.sh'
          - 'echo ''rm -rf /Apps/slic.tar.gz'' >> build-app.sh'
          - pipe: atlassian/ssh-run:0.2.2
            variables:
              SSH_USER: $SFTP_USER
              SERVER: $SFTP_SERVER
              SSH_KEY: $DEPLOY64_KEY
              PORT: '22'
              MODE: 'script'
              COMMAND: 'build-app.sh'

pipelines:
  default:
    - step: *build
    - step:
        <<: *deploy-test
        deployment: Test
  custom:
    production:
    - step: *build
    - step:
        <<: *deploy-production
        deployment: Production
``` |
| ```
pipelines: 
```<br>**pipelines**: the start of the process<br>```
default: 
```<br>**default**: this step will run on any commit to any branch.  If you only have one branch, master, it will run immediately after the commit.  Using the example from above, it will run **&deploy-test** step.<br>```
custom: 
```<br>**custom**: will run manually.  This will run **&deploy-production** step from above. | ```
pipelines:
  default:
    - step: *build
    - step:
        <<: *deploy-test
        deployment: Test
  custom:
    production:
    - step: *build
    - step:
        <<: *deploy-production
        deployment: Production
``` |
| Manually run the custom step.  Click **Run pipeline**, select the **Branch**, then select default and click **Run**. | ![image](media://aae276bb-c6ce-4423-9583-84c1d998bc66)<br>![image](media://27ff2a1b-4845-4234-aef1-311b87e6c4dc) |
| There are two variables set at the workspace for the SSH Key, if required to deploy to an external server as we did with Bamboo.<br>- $DEPLOY64_KEY is encoded.
- $DEPLOY_KEY is decoded. |  |
| **Linting/error checking.**<br>Linting/error checking is enabled for the YAML file if you are editing it on Bitbucket, but you might also want to copy and paste the complete contents of the YAML file into their pipelines validator. This will catch any errors and ensure that the YAML file contains valid structure.  
[https://bitbucket-pipelines.atlassian.io/validator](https://bitbucket-pipelines.atlassian.io/validator) |  |