diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..35826cb --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,48 @@ +def artifactName = "" // Declare globally + +pipeline { + agent any + + environment { + BUCKET_NAME = "project-1-bucket-a" + } + + stages { + stage('Clean old Artifacts') { + steps { + sh 'rm -rf target' + } + } + + stage('Build') { + steps { + sh 'mvn package' + } + } + + stage('Upload Artifact') { + steps { + script { + // Capture WAR file name + artifactName = sh(script: "cd target && ls WebAppCal-*.war", returnStdout: true).trim() + + // Upload to S3 + sh """ + cd target + aws s3 cp ${artifactName} s3://${env.BUCKET_NAME}/ + """ + } + } + } + + stage('Deploy') { + steps { + sh """ + cd ansible + ansible-playbook -i aws_ec2.yml playbook.yml \ + -e "BUCKET_NAME=${env.BUCKET_NAME} ARTIFACT_NAME=${artifactName}" + """ + } + } + } +} diff --git a/ansible/aws_ec2.yml b/ansible/aws_ec2.yml new file mode 100644 index 0000000..ae19334 --- /dev/null +++ b/ansible/aws_ec2.yml @@ -0,0 +1,11 @@ +# demo.aws_ec2.yml +plugin: amazon.aws.aws_ec2 + +regions: + - us-east-1 + +groups: + webservers: "'project-1-app' in tags.Name" + +hostnames: + - private-ip-address \ No newline at end of file diff --git a/ansible/group_vars/webservers.yml b/ansible/group_vars/webservers.yml new file mode 100644 index 0000000..56e3abc --- /dev/null +++ b/ansible/group_vars/webservers.yml @@ -0,0 +1,3 @@ +ansible_user: ec2-user +ansible_ssh_private_key_file: ~/.ssh/server-key +ansible_ssh_common_args: "-o StrictHostKeyChecking=no" \ No newline at end of file diff --git a/ansible/playbook.yml b/ansible/playbook.yml new file mode 100644 index 0000000..958531d --- /dev/null +++ b/ansible/playbook.yml @@ -0,0 +1,18 @@ +--- +- name: Update web servers + hosts: webservers + + + tasks: + - name: Get Artifact + amazon.aws.s3_object: + bucket: "{{BUCKET_NAME}}" + object: "{{ARTIFACT_NAME}}" + dest: ~/{{ARTIFACT_NAME}} + mode: get + delegate_to: localhost + + - name: Deploy Artifact To Hosts + ansible.builtin.copy: + src: ~/{{ARTIFACT_NAME}} + dest: /home/ec2-user/apache-tomcat/webapps/ROOT.war \ No newline at end of file diff --git a/pom.xml b/pom.xml index 9e1759e..dd4df1a 100644 --- a/pom.xml +++ b/pom.xml @@ -21,10 +21,23 @@ 2.5 - - - releases - http://52.204.135.48:8081/nexus/content/repositories/releases - - - + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.11.0 + + 17 + 17 + + + + org.apache.maven.plugins + maven-war-plugin + 3.3.2 + + + + \ No newline at end of file