Jenkinsfile 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. pipeline{
  2. agent any
  3. environment {
  4. // 目标主机
  5. dsthost = "192.168.50.21"
  6. // 目标目录
  7. dstroot = "/opt/jenkins/wf-platform-equipment-automation"
  8. // 凭证ID
  9. credentials = 'jayhaw'
  10. // Java 运行参数
  11. java_opts = "-Xms512m -Xmx1024m"
  12. }
  13. options {
  14. // 不允许同时执行流水线, 防止同时访问共享资源等
  15. disableConcurrentBuilds()
  16. // 显示具体的构建流程时间戳
  17. timestamps()
  18. }
  19. stages{
  20. stage('Prepare') {
  21. steps{
  22. echo "1. Prepare Stage"
  23. checkout scm
  24. //sh "echo $ref"
  25. sh "printenv"
  26. script {
  27. sh 'ls -l'
  28. }
  29. }
  30. }
  31. stage('Maven'){
  32. steps{
  33. echo "2. Maven Stage"
  34. withMaven(
  35. globalMavenSettingsConfig: 'stongmaven'
  36. ) {
  37. // Run the maven build
  38. sh "mvn clean install package -U -Dmaven.test.failure.ignore"
  39. }
  40. // script{
  41. // //sh "cat /etc/os-release"
  42. //
  43. // sh "mvn -Dmaven.test.failure.ignore clean package"
  44. // }
  45. }
  46. }
  47. stage('Deploy'){
  48. steps{
  49. echo "3. Deploy Stage"
  50. script{
  51. def _pom = readMavenPom file: 'pom.xml'
  52. sh "echo ${_pom.getVersion()}"
  53. _version = _pom.getVersion()
  54. _artifactId = _pom.getArtifactId()
  55. sh "sed -i 's/<API_NAME>/${_artifactId}-${_version}/' Launch.sh"
  56. sh "sed -i 's/<JAVA_OPTS>/${java_opts}/' Launch.sh"
  57. //sh "scp hello-0.0.1-SNAPSHOT.jar"
  58. withCredentials([usernamePassword(credentialsId: 'stong_test_jenkins', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME')]) {
  59. def remote = [:]
  60. remote.name = "dest"
  61. remote.host = env.dsthost
  62. remote.allowAnyHosts = true
  63. remote.user = USERNAME
  64. remote.password = PASSWORD
  65. sshCommand remote: remote, command: "mkdir -p ${env.dstroot}"
  66. sshPut remote: remote, from: 'Launch.sh', into: "${env.dstroot}"
  67. sshPut remote: remote, from: "target/app.jar", into: "${env.dstroot}"
  68. sshCommand remote: remote, command: "chmod +x ${env.dstroot}/Launch.sh"
  69. sshCommand remote: remote, command: "cd ${env.dstroot} && ./Launch.sh restart"
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }