It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Run a bash command on EC2 only with pipeline

I already have setup EC2 server and have a pull-code.sh file with commands listed to deploy the application in EC2 server.

The bash script does the following steps.

1. Pull the latest code from the master branch

2. Run migrations

3. Restart the server

The pipeline documentation states to configure an S3 bucket and upload the zip file for deployment. It is not getting clear from where this zip file will come and how to upload to S3 in each cycle.

 

In my case, I only want to run the script each time pipeline is run which will further handle the deployment as specified in the script.

bash pull-code.sh

How can I configure the pipeline to run the bash command only by SSHing to the EC2 server? Do I still need to configure S3 Bucket?

2 answers

As mentioned in the documentation, you need S3 Bucket if you are using inbuilt `AWS Code Deployment` service in your YAML file.

If you haven't configured anything for AWS Code Deployment, the forget about what documentation says.

The zip file you are talking about is comming from when you build application. Like production Built or something. Say for example if your using angular, you will run `ng build` to build your client for production and it will create files in the `dist` folder of your project. Then after that you have to zip the contents and send it to your server using `AWS Code Deploy` bitbucket service.

Tell me this, where do you want to run this sheel script `pull-code.sh`? On your server after all the steps finished?

If that is the case, i will create a very simple restapi client and i run it in my server. This server will have few endpoints like `https://yourwebsite.com/deploy/app-a`

Basically when you hit this api, what it will do is, it will trigger the shell script.

And in YAML after finishing all the steps, i trigger that API using `curl`. 

 

This is one way, but the best solution is to use `AWS Code Deploy` in bitbucket YAML.

Reference for using AWS Deploy: https://confluence.atlassian.com/bitbucket/deploy-to-amazon-aws-875304040.html

Good tutorial for Deploying to AWS using Pipeline: https://hackernoon.com/deploy-to-ec2-with-aws-codedeploy-from-bitbucket-pipelines-4f403e96d50c

In my case, I do not have to build the application. I just need to pull the master branch to the EC2 server and run a few commands. That's what that pull_code.sh script is doing.

 

It is running below commands

git pull origin master

pip manage.py migrate

...

Since I'm not building and don't need to move files to the server, do I still need S3? 

 

The pull_code.sh is already on the server at the root level. I just have to SSH and run that script using

bash pull_code.sh
0 votes

Anuj,
It looks like this pipe might do what you need https://bitbucket.org/atlassian/ssh-run/src. It was designed specifically for running shell commands or scripts on a remote server. Your pipe configuration might look like this:

script:
  - pipe: atlassian/ssh-run:0.1.1
    variables:
      SSH_USER: 'ec2-user' # or whatever the username is
      SERVER: '127.0.0.1' # your server host 
      COMMAND: 'bash pull_code.sh'

 Hope that helps! Cheers

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Bitbucket Pipelines

Building a Bitbucket Pipe as a casual coder

...ipe.sh :  #!/bin/bash source "$(dirname "$0")/common.sh" enable_debug extra_args="" if [[ "${DEBUG}" == "true" ]]; then extra_args="--verbose" fi # mandatory variables R...

4,400 views 4 22
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you