Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Is it possible to deploy using "pm2 deploy production" on Bitbucket Pipelines?

HaroldFrost April 27, 2018

I tried to use the file 

image: node:8.11.1

pipelines:
default:
- step:
script: # Modify the commands below to build your repository.
- npm install -g pm2
- pm2 deploy production 

but it always throws with "Host key verification failed". I realize that pm2 require ssh keys to deploy. It there any work-arounds?

2 answers

1 vote
StannousBaratheon
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 2, 2018

Hi @HaroldFrost

You can set up SSH keys and known hosts in pipelines. Please refer to this page of the documentation for details: https://confluence.atlassian.com/bitbucket/use-ssh-keys-in-bitbucket-pipelines-847452940.html

Balasubramani May 25, 2018

Hello @StannousBaratheon, I had the same problem but to add an existing ssh key, Bitbucket asking for both public and private key? I only have the private key by default and don't have a public key. So how could I overcome this?

Answers are appreciated. Thank you.

StannousBaratheon
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 27, 2018

Hi @Balasubramani

Pipelines allows you to enter the public key so that you can store both private and public keys together. It only uses the private key during the build process however so if you don't have the public key you may set any value and SSH will still work as desired.

Balasubramani May 28, 2018

Thanks, @StannousBaratheon for your response. I have one more doubt. Below goes my PM2 ecosystem config file.

module.exports = {
apps: [
{
name: "my-app",
script: "./app.js"
}
],
deploy: {
production: {
user: "ubuntu",
host: "ec2-xx-xxx-xxx-xx.us-east-2.compute.amazonaws.com",
key: "~/.ssh/AWS-EC2-INSTANCE-LIVE.pem",
ref: "origin/master",
repo: "git@bitbucket.org:User/my-app.git",
path: "/home/ubuntu/my-app",
"post-deploy":
"npm install && pm2 startOrRestart ecosystem.config.js"
}
}
};

And here goes my PM2 deploy command in a package.json file,

"deploy" : "pm2 deploy ecosystem.config.js production" 

And below goes my bitbucket pipeline script block

script: # Modify the commands below to build your repository.
- npm install
- npm run-script deploy

Now I have added the AWS-EC2-INSTANCE-LIVE.pem (Private key) in my Bitbucket project repo and given some public key as some value. 

Now, how to refer this private key (ssh key added in repo settings) in my PM2 ecosystem config file? How PM2 know there is a private key in BitBucket and how it refers to it? 

Thank you.

StannousBaratheon
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 29, 2018

Pipelines makes your key available at: `/opt/atlassian/pipelines/agent/data/id_rsa` during the build.

It also configures it as a default identify file in ~/.ssh/config so that build tools that respect ssh configuration work automatically.

Balasubramani May 29, 2018

Sorry, @StannousBaratheon. Still, I am facing syntax problem I guess. The following is my updated PM2 ecosystem config script in a key path.

key: "/opt/atlassian/pipelines/agent/data/id_rsa" 

But still, it says, Host key verification failed. What am I missing?

Any idea?

Note: I added a key in my Project repository.  Not in my account.

0 votes
divyeshdhokiya January 20, 2019

@StannousBaratheon in my case, a pipeline is trying to read a key from '/root/.ssh/id_rsa' location and gives me error like "No such file or directory.Permission denied (publickey)." my key is under ec2user and not in root user..how to specify the path in the pipeline?

StannousBaratheon
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 20, 2019

Hi @divyeshdhokiya

Pipelines doesn't read the key per se but rather the build tool you're using within pipelines likely looks in the current user directory for a configured key. Since most Docker images start as the root user by default, the user's home directory will be /root

If you're creating this Docker image you can change the default user by adding the USER directive to your dockerfile (see https://docs.docker.com/engine/reference/builder/#user).

Otherwise, can you please provide additional information about what you're trying to do including the Docker image, build tooling etc. so that we can assist further?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events