Introduce BitBucket

Abstract

BitBucket Pipelines is already approached by myself, take a note and sharing about how we can implement with BitBucket Pipelines

Documentation

Feature

Articles

First Pipeline in BitBucket

Info

The first definition pipeline in BitBucket, It just focus deploy static web frontend to staging and production, including: Build and Deploy with SCP

Extension to use inside pipeline: Pipe - SCP Deploy

Prerequisites

  1. Enable 2FA of BitBucket, read more about this enable at Enable two-step verification
  2. Turn on pipelines feature in repository setting
  3. Provision runner for BitBucket, read more about this one at Adding a new runner in Bitbucket
  4. To use SCP, you need to add SSH Key into bitbucket for managing and securing with transparent. In other one, you need to add SSH Key into host for permitting agent can connect. Make sure you fetch fingerprint of knowhost to validate connection, explore at issue Bitbucket Pipeline Host key verification failed. (Centos)

Detailing Pipeline

# The `pipelines` property is used to define the build process for a repository.
# Documentation: https://support.atlassian.com/bitbucket-cloud/docs/pipeline-start-conditions/
pipelines:
  # `default` pipeline runs on every push (excluding tag pushes) to the repository
  # NOTE: Run on any branch not set policies (Checkout branch will be affected)
  default:
    - step:
        name: Build and Upload Artifact
        image: node:20.18.0
        script:
          - npm install
          - npm run build
        artifacts:
          - dist/**
        runs-on:
          - self.hosted
          - linux
 
  # Run deploy only to staging branch
  branches:
    staging:
      - step:
          name: Build code to dist bundle
          image: node:20.18.0
          script:
            - npm install
            - npm run build
          artifacts:
            - dist/**
          runs-on:
            - self.hosted
            - linux
      - step:
          name: Deploy dist artifacts using SCP to Staging
          # Setting `deployment` help retrive variable depend on environment definition in deployment tab of repository setting
          # Documentation: https://support.atlassian.com/bitbucket-cloud/docs/step-options/#Deployment
          deployment: Staging
          script:
            - pipe: atlassian/scp-deploy:1.5.1
              variables:
                USER: "$USER"
                SERVER: "$LS_IP"
                REMOTE_PATH: "/var/www/html"
                LOCAL_PATH: "dist/*"
          runs-on:
            - self.hosted
            - linux
 
  # Run deploy only to main branch
    main:
      - step:
          name: Build code to dist bundle
          image: node:20.18.0
          script:
            - npm install
            - npm run build
          artifacts:
            - dist/**
          runs-on:
            - self.hosted
            - linux
      - step:
          name: Deploy dist artifacts using SCP to Production
          # Setting `deployment` help retrive variable depend on environment definition in deployment tab of repository setting
          # Documentation: https://support.atlassian.com/bitbucket-cloud/docs/step-options/#Deployment
          deployment: Production
          script:
            - pipe: atlassian/scp-deploy:1.5.1
              variables:
                USER: "$USER"
                SERVER: "$LS_IP"
                REMOTE_PATH: "/var/www/html"
                LOCAL_PATH: "dist/*"
          runs-on:
            - self.hosted
            - linux