← All posts

Automatic Application Deployment Using Bitbucket Pipelines

If you want to automate your routine working days this article is for you. You can find my experience using Bitbucket pipelines for Angular applications and Node.js servers there. But you can use these instructions for any project and any language. So let's go deeper.

What are Bitbucket pipelines?

Bitbucket Pipelines is a continuous integration and delivery (CI/CD) platform provided by Atlassian. It is tightly integrated with the Bitbucket Cloud repository hosting service and allows developers to automate various tasks, such as building, testing, and deploying their applications.

In simple terms, Bitbucket Pipelines provides a way to define a series of steps or actions that are executed whenever changes are made to a repository. These steps are defined in a configuration file called “bitbucket-pipelines.yml” which resides in the root of the repository.

Bitbucket Pipelines uses Docker images, which means your builds are consistent and reliable. You won’t have to worry about weird environment issues anymore. Just specify the Node.js version you want, add your dependencies, and let it do its magic.

Here is an example of “bitbucket-pipelines.yml”:

Angular CI/CD

First of all, you need to have a repository with your Angular application on Bitbucket. In your repository, you can find the “Pipelines” tab in the sidebar. On the “Pipelines” page you would be able to create the first “bitbucket-pipelines.yml” template.

pipelines page
The pipelines page

Now you need to add the next code to your template:

Here’s a breakdown of each line in the provided Bitbucket Pipelines configuration:

  1. image: node:14.20Specifies the Docker image to be used for the pipeline. In this case, it uses the node:14.20 image, which is based on Node.js version 14.20.
  2. pipelines: (Start of the pipelines block). Begins the definition of pipelines.
  3. branches: (Start of the branches block). Specifies that the following steps should only run for branches.
  4. test: (Start of the test branch block). Specifies that the following steps should run for the branch named “test”.
  5. - step: (Start of the first step block). Defines a new step within the pipeline.
  6. name: InstallSets the name of the step as “Install”. This is just a label for the step.
  7. caches: Specifies the caches to be used in the step.
  8. - node Specifies that the “node” cache should be used. Caching allows reusing dependencies between pipeline runs for faster execution.
  9. script: Indicates the start of the script section where the actual commands for the step are defined.
  10. - apt-get update Updates the package repository on the built environment.
  11. - npm i -f Forces the installation of Node.js dependencies using npm.
  12. - npm run build:test Runs the build:test script defined in the project’s package.json file.
  13. artifacts:Specifies the artifacts that should be saved from this step to be used in subsequent steps.
  14. - dist/**Saves the “dist” folder and its contents as an artifact. The double asterisk notation (**) represents any subdirectories within "dist" as well.
  15. - step: (Start of the second step block). Defines a new step within the pipeline.
  16. name: DeploySets the name of the step as “Deploy”.
  17. script:Indicates the start of the script section for the “Deploy” step.
  18. - cp .htaccess dist/Copies the “.htaccess” file to the “dist” folder. This is an additional step, not directly related to the deployment process.
  19. - scp -r -P 22 dist/ user@111.22.33.10:/home/user/Uses Secure Copy (SCP) to copy the “dist” folder and its contents to the remote server at the specified address and directory.
  20. - ssh user@111.22.33.10 -p 22 'cd /var/www/ && rm -rf dist_old && mv dist dist_old && cp -r /home/user/dist/ dist' Connects to the remote server via SSH and executes a series of commands: it changes the working directory to “/var/www/”, removes the existing “dist_old” folder, renames the current “dist” folder to “dist_old”, and finally copies the “dist” folder from the user’s home directory to the current working directory.

After saving this pipeline to your project you need to give access for connection to your server because Bitbucket could not connect via SSH without keys.

First of all, you have to generate or add your own keys on the “SSH Keys” page in the “Pipeline section” (“Repository settings” tab). Pipelines use an SSH key pair and known host information to securely connect to other services and hosts. Read more about SSH keys in Pipelines.

After that, you could be able to copy your public key to ~/.ssh/authorized_keys on the remote host.

The next step is to add a host address with a port (if it needed), then click Fetch to see the host’s fingerprint. Read about known hosts for Pipelines.

All these steps are required for each repository where you want to use pipelines.

Although your SSH connection is set, here is one more thing you should do — check the owner of your directory at the remote host (your directory owner should be the same as that in the pipeline).

Finally, you can push or merge your test branch to see the magic of automatization on the “Pipelines” page. You also will receive email notifications about failed actions in your pipelines.

Node.js CI/CD

If you use an Angular application for frontend in 99% of cases your backend is Node.js (express, nest.js, etc.). It would be interesting to know if I am wrong (write a comment on this article).

The steps for setting Bitbucket SHH keys and adding hosts are the same so I don’t want to waste your time. I just provide you with an example of the “bitbucket-pipelines.yml” template:

In this case, it is much easier, because we do not need to build an Angular application using Bitbucket cloud. We just connect to the remote host, change the branch, do any action (git pull, npm install, etc.), and restart the node.js application. I want to notice that you can add to your template option — clone: enable: false if don’t need to clone your project to Bitbucket cloud (your pipeline would work faster).


In conclusion, I recommend at least trying to automate your deployment because it’s super convenient. You can keep your code and CI/CD pipelines all in one place, making collaboration and project management a breeze.

Another cool thing is that setting up your CI/CD pipelines becomes a piece of cake. You just write a simple configuration file using YAML, and you’re good to go. It takes care of all the repetitive tasks like building and testing, so you can focus more on coding and less on setup.

Thanks for your attention, if you like this article you can 👏 , follow me and write a comment.