如何使用jenkins将spring boot jar文件部署到EC2?

问题描述:

我正在尝试将Spring启动应用程序部署到AWS EC2实例。我已经看到很多博客和教程完全解释了部署过程,这是可以理解的。我正在努力如何在jenkins中进行持续部署或交付,其中主要功能是春季启动应用程序名称或jar文件名称随时间的变化。

i am trying to deploy the spring boot app to AWS EC2 instances. i have seen lot of blog and tutorial explained deployment process completely which is understandable. i am struggling how to do continuous deployment or delivery in jenkins which main feature where spring boot app name or jar file name changes that time.

我的管道

  pipeline {
    agent any

    tools{
       maven 'localmaven' 
    }
    stages {
        stage('Build') { 
            steps {
               sh 'mvn clean package' 
            }
            post {
               success {
                    echo 'Now Archiving...'
                    archiveArtifacts artifacts: '**/target/*.jar'
                   }
              } 
          }


    stage('Deliver') {
        steps {
             sh 'scp -v -o StrictHostKeyChecking=no  -i /var/lib/jenkins/secrets/mykey target/*.jar ubuntu@00.00.00.00:/home/ubuntu'
             sh "sshpass -p password ssh -o StrictHostKeyChecking=no -i /var/lib/jenkins/secrets/mykey ubuntu@00.00.00.00 '/home/ubuntu/start.sh'"
        }
    }
}

}

服务器启动,停止和重启是在shell脚本中处理的。

Server start and stop and restart are handled in shell script.

我的start.sh

my start.sh

#!/bin/bash
nohup java -jar /home/ubuntu/aws-0.0.1-SNAPSHOT.jar > /home/ubuntu/log.txt 2>&1 &
echo $! > /home/ubuntu/pid.file

这启动我的服务器完美且工作正常..

This start my server perfectly and works fine..

她我的疑问目前在start.sh我使用相同的jar文件名,所以它工作正常,但在生产中版本更改jar文件名也改变了如何处理这种情况.help我知道这个过程。我可以提前得到完整的想法和所有内容

her my doubt is currently in start.sh i am using same jar file name so it works fine but in production with version change jar file name also change how to handle that situation.help me to know about that process. where i can get that complete idea and everything thanks in advance

我必须说你应该将工件版本作为标准流程维护用于非prod和prod部署。通常在非产品环境中,您可以规划 SNAPSHOT 版本,在生产中您应该选择 RELEASE 版本,该版本可以使用 mvn发布准备版本生成使用 maven-release-plugin 执行 >。它将提升您的pom版本以用于下一个后续版本。您可以将工件存储到AWS S3或Artifactory或Nexus(用于高可用性),就像您在此处引用的ubuntu机器一样。

I must say you should maintain you artifact’s version as a standard process for non-prod and prod deployment. Usually in non-prod environment you can plan for SNAPSHOT version and in production you should go for RELEASE version which can be generated using mvn release prepare release perform using maven-release-plugin. It will bump up your pom version for next subsequent releases. You can store your artifact to AWS S3 or Artifactory or Nexus (for high availability ) like the ubuntu machine that you are referring here.

现在我建议您再添加一个名为 stage('Release')的阶段,您应该使用maven-release-plugin发布版本并将其存储到单独的路径,如

Now I would suggest you should add one more stage named like stage('Release') where you should use using maven-release-plugin to release the version and store it to a separate path like


ubuntu@00.00.00.00:/ home / ubuntu / RELEASE / $ {version }

ubuntu@00.00.00.00:/home/ubuntu/RELEASE/${version}

并根据你的舞台('Build')应该复制到另一条路径,如

and as per you stage('Build') should copy to another path like


ubuntu@00.00.00.00:/ home / ubuntu / SNAPSHOT / $ {version}

ubuntu@00.00.00.00:/home/ubuntu/SNAPSHOT/${version}

您可以根据Jenkins管道的条件输入参数执行阶段'发布'和' Prod-Deliver '。
在这种情况下,这将是一个平滑CICD的可能解决方案。

You can execute stage 'Release' and 'Prod-Deliver' based on conditional input parameter of your Jenkins pipeline. Here would be a possible solution for a smooth CICD in your case.

   pipeline {
    agent any

    tools{
       maven 'localmaven' 
    }
    stages {
        stage('Build') { 
            steps {
               sh 'mvn clean install' 
            }
            post {
               success {
                    echo 'Now Archiving...'
                   }
              } 
          }

        stage('Release') { 
            steps {
               sh 'elease:prepare release:perform' 
            }
            post {
               success {
                    ////
                   }
              } 
          }

      stage('NonProd-Deliver') {
          steps {
               /*
               You can extract the version from pom.xml,replace you project location in jenkins workspace in the below command
               */
               sh 'version=$(echo -e 'setns x=http://maven.apache.org/POM/4.0.0\ncat /x:project/x:version/text()' | xmllint --shell ${YOUR_PROJECT_LOCATION}/pom.xml | grep -v /)'
               sh 'scp -v -o StrictHostKeyChecking=no  -i /var/lib/jenkins/secrets/mykey target/*.jar ubuntu@00.00.00.00:/home/ubuntu/SNAPSHOT/${version}'
               sh "sshpass -p password ssh -o StrictHostKeyChecking=no -i /var/lib/jenkins/secrets/mykey ubuntu@00.00.00.00 '/home/ubuntu/start.sh nonprod $version'"
          }
      }

       stage('Prod-Deliver') {
        steps {
              /*
               For production release you should pass the version as a parameter to your jenkins pipeline which is going to be in production
               */
             sh 'scp -v -o StrictHostKeyChecking=no  -i /var/lib/jenkins/secrets/mykey target/*.jar ubuntu@00.00.00.00:/home/ubuntu/RELEASE/${version} '
             sh "sshpass -p password ssh -o StrictHostKeyChecking=no -i /var/lib/jenkins/secrets/mykey ubuntu@00.00.00.00 '/home/ubuntu/start.sh prod ${version}'"
        }
    }

}
}

你必须添加condtion in你的脚本文件如下所示

You have to add the condtion in your script file as well like below

#!/bin/bash
release_type=$1
version=$2
if [[ ${release_type} == "prod" ]]; then
  # non snapshot release to production env
  nohup java -jar /home/ubuntu/RELEASE/${version}/aws-0.0.1.jar > /home/ubuntu/log.txt 2>&1 & 
else
  # snapshot release to non production env
  nohup java -jar /home/ubuntu/SNAPSHOT/${version}/aws-0.0.1-SNAPSHOT.jar > /home/ubuntu/log.txt 2>&1 &
fi
echo $! > /home/ubuntu/pid.file