Run a bash command on EC2 only with pipeline

Anuj March 20, 2019

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

1 vote
Alexander Zhukov
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 8, 2019

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

0 votes
Gnanamadhu March 22, 2019

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

Anuj March 24, 2019

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events