add terraform config for deploying to ec2

This commit is contained in:
Nana Janashia
2020-12-19 10:56:16 +01:00
parent 6123875bc8
commit df4698ad04
4 changed files with 159 additions and 4 deletions

31
Jenkinsfile vendored
View File

@@ -34,17 +34,40 @@ pipeline {
}
}
}
stage('provision server') {
environment {
AWS_ACCESS_KEY_ID = credentials('jenkins_aws_access_key_id')
AWS_SECRET_ACCESS_KEY = credentials('jenkins_aws_secret_access_key')
TF_VAR_env_prefix = 'test'
}
steps {
script {
dir('terraform') {
sh "terraform init"
sh "terraform apply --auto-approve"
EC2_PUBLIC_IP = sh(
script: "terraform output ec2_public_ip"
returnStdout: true
).trim()
}
}
}
}
stage('deploy') {
steps {
script {
echo "waiting for EC2 server to initialize"
sleep(time: 90, unit: "SECONDS")
echo 'deploying docker image to EC2...'
echo "${EC2_PUBLIC_IP}"
def shellCmd = "bash ./server-cmds.sh ${IMAGE_NAME}"
def ec2Instance = "ec2-user@35.180.251.121"
def ec2Instance = "ec2-user@${EC2_PUBLIC_IP}"
sshagent(['ec2-server-key']) {
sh "scp server-cmds.sh ${ec2Instance}:/home/ec2-user"
sh "scp docker-compose.yaml ${ec2Instance}:/home/ec2-user"
sshagent(['server-ssh-key']) {
sh "scp -o StrictHostKeyChecking=no server-cmds.sh ${ec2Instance}:/home/ec2-user"
sh "scp -o StrictHostKeyChecking=no docker-compose.yaml ${ec2Instance}:/home/ec2-user"
sh "ssh -o StrictHostKeyChecking=no ${ec2Instance} ${shellCmd}"
}
}