diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..b1c58ef --- /dev/null +++ b/.github/workflows/deploy.yml @@ -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/ssh-action@v0.1.8 + 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 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7a55e7e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,2 @@ +FROM tomcat:9.0 +COPY target/*.war /usr/local/tomcat/webapps diff --git a/Jenkinsfile b/Jenkinsfile index 7eca275..9997adc 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -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' + } + } + } } - - } } diff --git a/pom.xml b/pom.xml index aa8d86d..40e1366 100644 --- a/pom.xml +++ b/pom.xml @@ -1,3 +1,4 @@ + @@ -9,7 +10,7 @@ CounterWebApp Maven Webapp http://maven.apache.org - 1.7 + 1.8 4.1.1.RELEASE 1.2 4.11