Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
name: cicd workflow to create docker image and upload image into DockerHub
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup JDK 11
uses: actions/setup-java@v2
with:
distribution: adopt
java-version: "11"
- name: Build with Maven
run: mvn clean install
- name: Login to DockerHUB
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push docker image
env:
IMAGE_TAG: ${{ github.sha }}
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/mydimages:latest .
- name: Push docker image
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/mydimages:latest
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- name: SSH to Docker Host and Deploy Container
uses: appleboy/[email protected]
with:
host: ${{ secrets.DOCKER_HOST }}
username: ${{ secrets.SSH_USERNAME }}
password: ${{secrets.SSH_PASSWORD }}
script: >
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/mydimages:latest

sudo docker stop myjavaapp || true

sudo docker rm myjavaapp || true

sudo docker run -d --name myjavaapp -p8080:8080 ${{ secrets.DOCKERHUB_USERNAME }}/mydimages:latest
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM tomcat:9.0
COPY target/*.war /usr/local/tomcat/webapps
114 changes: 39 additions & 75 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,92 +1,56 @@
pipeline {
agent {
label 'maven-build-agent'
}

agent any
tools {
// Install the Maven version configured as "M3" and add it to the path.
maven "Maven3.9.5"
maven "Maven3.910"
}

stages {
stage('Checkout') {
stage('checkout'){
steps{
git 'https://github.com/keyspaceits/javawebapp.git'
}
}
stage('Build') {
steps {
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/keyspaceits/javawebapp.git']])
}
}
stage('Build') {
steps {

// Run Maven on a Unix agent.
sh 'mvn clean install -f pom.xml'

// To run Maven on a Windows agent, use
// bat "mvn -Dmaven.test.failure.ignore=true clean package"
}

post {
// If Maven was able to run the tests, even if some of the test
// failed, record the test results and archive the jar file.
success {
slackSend channel: '#operations', message: 'Javaweb app build success', teamDomain: 'ksops', tokenCredentialId: 'slack', username: 'ksops'
}

failure {
slackSend channel: '#operations', message: 'Javaweb app build has failed', teamDomain: 'ksops', tokenCredentialId: 'slack', username: 'ksops'
}
}



script {
try {
sh "mvn -Dmaven.test.failure.ignore=true clean package"
slackSend(channel: 'ci-cd-alerts', color: 'good', message: "Build Success! Job Name: ${env.JOB_NAME} Build Num: ${env.BUILD_NUMBER} (${env.BUILD_URL})", teamDomain: 'keyspaceitsgroup', tokenCredentialId: 'slackcred', username: 'suresh')
} catch (Exception e) {
slackSend(channel: 'ci-cd-alerts', color: 'danger', message: "Build Failed! Job Name: ${env.JOB_NAME} Build Num: ${env.BUILD_NUMBER} (${env.BUILD_URL})", teamDomain: 'keyspaceitsgroup', tokenCredentialId: 'slackcred', username: 'suresh')
error 'Build failed'
}
}
}
}
stage ('CodeQulity') {
steps{
withSonarQubeEnv('SonarQube') {
withSonarQubeEnv('SonarQubeServer') {
sh 'mvn clean install -f pom.xml sonar:sonar'
}
}
}

stage ('NexusUpload') {
steps {
nexusArtifactUploader artifacts: [[artifactId: 'CounterWebApp', classifier: '', file: '/home/jenkins/workspace/javawebapp-declarative-pipeline/target/CounterWebApp.war', type: 'war']], credentialsId: 'NexusCred', groupId: 'com.mkyong', nexusUrl: '52.2.21.21:8081', nexusVersion: 'nexus3', protocol: 'http', repository: 'maven-snapshots', version: '1.0-SNAPSHOT'
}

}
stage("Stage deploy") {
steps {
deploy adapters: [tomcat9(credentialsId: 'tomcat-server', path: '', url: 'http://34.226.163.28:8080')], contextPath: null, war: '**/*.war'
}
}
stage('Stage apprl for Prod') {
steps {
echo "taking approval from Dev Manager"
timeout(time: 7, unit: 'DAYS') {
input message: 'Do you want to proceed to QA?', submitter:'admin'
}
}
}
stage('Deploy to Prod') {
steps {
deploy adapters: [tomcat9(credentialsId: 'tomcat-server', path: '', url: 'http://34.226.163.28:8080/')], contextPath: null, war: '**/*.war'
}

post {
// If Maven was able to run the tests, even if some of the test
// failed, record the test results and archive the jar file.
success {
slackSend channel: '#operations', message: 'Javaweb app deployment success', teamDomain: 'ksops', tokenCredentialId: 'slack', username: 'ksops'
}

failure {
slackSend channel: '#operations', message: 'Javaweb app deployment has failed', teamDomain: 'ksops', tokenCredentialId: 'slack', username: 'ksops'
}
}



stage ('Prod Aprove') {
steps {
echo "taking approval from Prod manager for Prod Deployment"
timeout(time: 7, unit: 'DAYS') {
input message: 'Do you approve Prod Deployment?', submitter: 'admin'
}
}
}
stage('deploy to prod') {
steps {
script {
try {
deploy adapters: [tomcat9(alternativeDeploymentContext: '', credentialsId: 'tomcatdeployer', path: '', url: 'http://172.25.0.229:8080')], contextPath: null, war: '**/*.war'
slackSend(channel: 'ci-cd-alerts', color: 'good', message: "Prod Deployment was successful! here is the Job Name: ${env.JOB_NAME} Build Num: ${env.BUILD_NUMBER} (${env.BUILD_URL})", teamDomain: 'keyspaceitsgroup', tokenCredentialId: 'slackcred')
} catch (Exception e) {
slackSend(channel: 'ci-cd-alerts', color: 'danger', message: "Prod Deployment Failed! here is the Job Name: ${env.JOB_NAME} Build Num: ${env.BUILD_NUMBER} (${env.BUILD_URL})", teamDomain: 'keyspaceitsgroup', tokenCredentialId: 'slackcred')
error 'Deployment failed'
}
}
}
}


}
}
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
Expand All @@ -9,7 +10,7 @@
<name>CounterWebApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<jdk.version>1.7</jdk.version>
<jdk.version>1.8</jdk.version>
<spring.version>4.1.1.RELEASE</spring.version>
<jstl.version>1.2</jstl.version>
<junit.version>4.11</junit.version>
Expand Down
Loading