Compare commits
6 Commits
feature/an
...
feature/an
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
45e509506b | ||
|
|
3b6780360c | ||
|
|
3208a0e63e | ||
|
|
c70bfe4bbd | ||
|
|
2b7f3b1471 | ||
|
|
9573355ec3 |
32
Jenkinsfile-v2 → Jenkinsfile
vendored
32
Jenkinsfile-v2 → Jenkinsfile
vendored
@@ -1,45 +1,37 @@
|
|||||||
pipeline {
|
pipeline {
|
||||||
agent any
|
agent any
|
||||||
environment {
|
environment {
|
||||||
ANSIBLE_SERVER = "157.230.120.252"
|
ANSIBLE_SERVER = "167.99.136.157"
|
||||||
}
|
}
|
||||||
stages {
|
stages {
|
||||||
stage("copy ansible folder and ec2 access key to ansible-server") {
|
stage("copy files to ansible server") {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
|
echo "copying all neccessary files to ansible control node"
|
||||||
sshagent(['ansible-server-key']) {
|
sshagent(['ansible-server-key']) {
|
||||||
echo "copying ansible folder to ansible server"
|
sh "scp -o StrictHostKeyChecking=no ansible/* root@${ANSIBLE_SERVER}:/root"
|
||||||
// ${ANSIBLE_SERVER}:/root without root will give jenkins@${ANSIBLE_SERVER}:/root
|
|
||||||
sh "scp -o StrictHostKeyChecking=no ansible/* root@${ANSIBLE_SERVER}:/root"
|
|
||||||
|
|
||||||
echo "copying ssh keys for ec2 instances"
|
|
||||||
withCredentials([sshUserPrivateKey(credentialsId: 'ec2-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]) {
|
withCredentials([sshUserPrivateKey(credentialsId: 'ec2-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]) {
|
||||||
sh 'scp $keyfile root@$ANSIBLE_SERVER:/root/ssh-key.pem'
|
sh 'scp $keyfile root@$ANSIBLE_SERVER:/root/ssh-key.pem'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stage("execute ansible playbook from the ansible-server") {
|
stage("execute ansible playbook") {
|
||||||
environment {
|
|
||||||
AWS_ACCESS_KEY_ID = credentials('jenkins_aws_access_key_id')
|
|
||||||
AWS_SECRET_ACCESS_KEY = credentials('jenkins_aws_secret_access_key')
|
|
||||||
}
|
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
echo "executing ansible-playbook"
|
echo "calling ansible playbook to configure ec2 instances"
|
||||||
|
|
||||||
def remote = [:]
|
def remote = [:]
|
||||||
remote.name = "ansible-server"
|
remote.name = "ansible-server"
|
||||||
remote.host = ANSIBLE_SERVER
|
remote.host = ANSIBLE_SERVER
|
||||||
remote.allowAnyHosts = true
|
remote.allowAnyHosts = true
|
||||||
|
|
||||||
withCredentials([sshUserPrivateKey(credentialsId: 'ansible-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]) {
|
|
||||||
remote.identityFile = keyfile
|
|
||||||
remote.user = user
|
|
||||||
|
|
||||||
sshCommand remote: remote, command: "ls -l"
|
withCredentials([sshUserPrivateKey(credentialsId: 'ansible-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]){
|
||||||
sshCommand remote: remote, command: "ansible-playbook docker-and-compose.yaml"
|
remote.user = user
|
||||||
|
remote.identityFile = keyfile
|
||||||
|
sshScript remote: remote, script: "prepare-ansible-server.sh"
|
||||||
|
sshCommand remote: remote, command: "ansible-playbook my-playbook.yaml"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
pipeline {
|
|
||||||
agent any
|
|
||||||
environment {
|
|
||||||
ANSIBLE_SERVER = "157.230.120.252"
|
|
||||||
}
|
|
||||||
stages {
|
|
||||||
stage("copy ansible folder and ec2 access key to ansible-server") {
|
|
||||||
steps {
|
|
||||||
script {
|
|
||||||
sshagent(['ansible-server-key']) {
|
|
||||||
echo "copying ansible folder to ansible server"
|
|
||||||
// ${ANSIBLE_SERVER}:/root without root will give jenkins@${ANSIBLE_SERVER}:/root
|
|
||||||
sh "scp -o StrictHostKeyChecking=no ansible/* root@${ANSIBLE_SERVER}:/root"
|
|
||||||
|
|
||||||
echo "copying ssh keys for ec2 instances"
|
|
||||||
withCredentials([sshUserPrivateKey(credentialsId: 'ec2-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]) {
|
|
||||||
sh 'scp $keyfile root@$ANSIBLE_SERVER:/root/ssh-key.pem'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stage("execute ansible playbook from the ansible-server") {
|
|
||||||
environment {
|
|
||||||
AWS_ACCESS_KEY_ID = credentials('jenkins_aws_access_key_id')
|
|
||||||
AWS_SECRET_ACCESS_KEY = credentials('jenkins_aws_secret_access_key')
|
|
||||||
}
|
|
||||||
steps {
|
|
||||||
script {
|
|
||||||
echo "executing ansible-playbook"
|
|
||||||
|
|
||||||
def remote = [:]
|
|
||||||
remote.name = "ansible-server"
|
|
||||||
remote.host = ANSIBLE_SERVER
|
|
||||||
remote.allowAnyHosts = true
|
|
||||||
|
|
||||||
withCredentials([sshUserPrivateKey(credentialsId: 'ansible-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]) {
|
|
||||||
remote.identityFile = keyfile
|
|
||||||
remote.user = user
|
|
||||||
|
|
||||||
sshCommand remote: remote, command: "ls -l"
|
|
||||||
|
|
||||||
// set AWS credentials
|
|
||||||
sshScript remote: remote, script: "ansible/prepare-server.sh"
|
|
||||||
|
|
||||||
sshCommand remote: remote, command: "export AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}; export AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}; ansible-playbook docker-and-compose.yaml"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
def gv
|
|
||||||
|
|
||||||
pipeline {
|
|
||||||
agent any
|
|
||||||
stages {
|
|
||||||
stage("build jar") {
|
|
||||||
steps {
|
|
||||||
script {
|
|
||||||
echo "building the application..."
|
|
||||||
sh 'mvn package'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stage("build image") {
|
|
||||||
steps {
|
|
||||||
script {
|
|
||||||
echo "building the docker image..."
|
|
||||||
withCredentials([usernamePassword(credentialsId: 'docker-hub-repo', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
|
|
||||||
sh 'docker build -t nanajanashia/demo-app:jma-2.0 .'
|
|
||||||
sh "echo $PASS | docker login -u $USER --password-stdin"
|
|
||||||
sh 'docker push nanajanashia/demo-app:jma-2.0'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stage("deploy") {
|
|
||||||
steps {
|
|
||||||
script {
|
|
||||||
echo 'deploying the application...'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
plugin: aws_ec2
|
plugin: aws_ec2
|
||||||
regions:
|
regions:
|
||||||
- eu-west-3
|
- eu-west-3
|
||||||
keyed_groups:
|
keyed_groups:
|
||||||
- key: tags
|
- key: tags
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
apt update
|
|
||||||
apt install ansible -y
|
|
||||||
apt install python3-pip -y
|
|
||||||
pip3 install boto3 botocore
|
|
||||||
|
|
||||||
# export AWS_ACCESS_KEY_ID=$1
|
|
||||||
# export AWS_SECRET_ACCESS_KEY=$2
|
|
||||||
|
|
||||||
6
prepare-ansible-server.sh
Normal file
6
prepare-ansible-server.sh
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
apt update
|
||||||
|
apt install ansible -y
|
||||||
|
apt install python3-pip -y
|
||||||
|
pip3 install boto3 botocore
|
||||||
19
script.groovy
Normal file
19
script.groovy
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
def buildJar() {
|
||||||
|
echo "building the application..."
|
||||||
|
sh 'mvn package'
|
||||||
|
}
|
||||||
|
|
||||||
|
def buildImage() {
|
||||||
|
echo "building the docker image..."
|
||||||
|
withCredentials([usernamePassword(credentialsId: 'docker-hub-repo', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
|
||||||
|
sh 'docker build -t nanajanashia/demo-app:jma-2.0 .'
|
||||||
|
sh "echo $PASS | docker login -u $USER --password-stdin"
|
||||||
|
sh 'docker push nanajanashia/demo-app:jma-2.0'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def deployApp() {
|
||||||
|
echo 'deploying the application...'
|
||||||
|
}
|
||||||
|
|
||||||
|
return this
|
||||||
Reference in New Issue
Block a user