Skip to content

Instantly share code, notes, and snippets.

@shankar-bavan
Last active June 9, 2023 16:35
Show Gist options
  • Save shankar-bavan/ccc0d69e40f958e76aa23777871955f5 to your computer and use it in GitHub Desktop.
Save shankar-bavan/ccc0d69e40f958e76aa23777871955f5 to your computer and use it in GitHub Desktop.
CI CD with bitbucket pipleline and shell script

1. Setup ssh key on server

follow this guide https://support.atlassian.com/bitbucket-cloud/docs/set-up-an-ssh-key/

or

ssh-keygen eval $(ssh-agent) ssh-add ~/.ssh/<private_key_file>

Go to Bitbucket -> repo -> repo-setting-> -> Pipelines -> Settings Enable pipeline

Go to Bitbucket -> repo -> repo-setting-> Access Keys -> Add Key copy public key cat ~/.ssh/id_rsa.pub

ssh -T [email protected] The command message tells you which of your Bitbucket accounts can log in with that key. If there any issues run below command (Permission denied (publickey)) ssh-add -l ssh-add ~/.ssh/<private_key_file>

Go to Bitbucket -> repo -> repo-setting-> Access Keys -> Known hosts Add IP address of the server and click on Fetch and Add Host

=============================================================

2. Update remote url

Go to project directory git remote -v

set ssh format url git remote set-url origin [email protected]:<workspace_ID>/<repo_name>.git

=============================================================

3. Bitbucket pipeline settings (variables)

Go to Bitbucket -> repo -> repo-setting-> -> Pipelines -> Repository variables Name: SSH_USER Value : centos (server username) Name: PORT Value: 22 Name:SERVER Value: IP address untick secured

===============================================================

4. Bitbucket pipeline settings (SSH Keys)

Go to Bitbucket -> repo -> repo-setting-> -> Pipelines -> SSH Keys Click on Generate keys Copy public key to ~/.ssh/authorized_keys on the remote host (login to server and go to above path and edit file with nano and paste the public key in new line)

===============================================================

5. create file and copy below code.

# This is a sample build configuration for PHP.
# Check our guides at https://confluence.atlassian.com/x/e8YWN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: php:7.1.29

pipelines:

  branches:
    production:
      - step:
          name: Deploy to production
          deployment: production
          script:
            - echo "Deploying to production environment"
            - pipe: atlassian/ssh-run:0.2.2
              variables:
                SSH_USER: $SSH_USER
                SERVER: $SERVER
                #SSH_KEY: $MYKEY
                PORT: $PORT
                COMMAND: "/var/folder-name/deploy.sh" ```

===============================================================

6. Sh script

Note: for centos #!/bin/sh for ubuntu #!/bin/bash

Bash path must be same than results of: $ which bash

#!/bin/sh

repos=( 
  "/var/www/project1"
  "/var/www/project2"
)

echo ""
echo "Getting latest for" ${#repos[@]} "repositories using pull"

for repo in "${repos[@]}"
do
  echo ""
  echo "****** Getting latest for" ${repo} "******"
  cd "${repo}"
  git pull
  echo "******************************************"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment