jenkins配置,脚本代码

    1. pipeline {
    2. agent any
    3. parameters {
    4. string (
    5. name: 'PROJECT_NAME',
    6. defaultValue: 'motolive.58moto.com',
    7. description: '项目名称',
    8. trim: true
    9. )
    10. string (
    11. name: 'ALIAS',
    12. defaultValue: 'live',
    13. description: '子项目',
    14. trim: true
    15. )
    16. string (
    17. name: 'REMOTE_PATH',
    18. defaultValue: '/data/wwwroot/motolive.58moto.com',
    19. description: '目标路径',
    20. trim: true
    21. )
    22. string (
    23. name: 'GIT_URL',
    24. defaultValue: 'http://gitlab.jddmoto.com/h5/oss_moto_live.git',
    25. description: 'GIT地址',
    26. trim: true
    27. )
    28. string (
    29. name: 'GIT_BRANCH',
    30. defaultValue: 'develop',
    31. description: 'GIT分支',
    32. trim: true
    33. )
    34. string (
    35. name: 'HOST_IP',
    36. defaultValue: '172.16.247.31',
    37. description: '服务器IP',
    38. trim: true
    39. )
    40. string (
    41. name: 'HOST_PORT',
    42. defaultValue: '62222',
    43. description: '服务器PORT',
    44. trim: true
    45. )
    46. string (
    47. name: 'HOST_USER',
    48. defaultValue: 'root',
    49. description: '服务器账号',
    50. trim: true
    51. )
    52. password (
    53. name: 'HOST_PASS',
    54. defaultValue: '123qwe!@#',
    55. description: '服务器密码'
    56. )
    57. }
    58. environment{
    59. ZIP_FILE_NAME = ''
    60. }
    61. stages {
    62. stage('代码获取') {
    63. environment{
    64. GIT_CRED_ID='749e2eb1-3eb3-499e-a9df-ef9f0d1232ae'
    65. }
    66. steps {
    67. script {
    68. git branch: params.GIT_BRANCH, credentialsId: GIT_CRED_ID, url: params.GIT_URL
    69. def commit_no = sh(returnStdout: true, script: 'git rev-parse HEAD').trim().take(6)
    70. def date = new Date().format('yyyyMMdd')
    71. def version = params.ALIAS+'_'+params.GIT_BRANCH+'_'+commit_no+'_'+date+'_'+env.BUILD_NUMBER
    72. ZIP_FILE_NAME = params.PROJECT_NAME+'.'+version+'.zip'
    73. }
    74. }
    75. }
    76. stage('NPM Build') {
    77. steps {
    78. script {
    79. sh "npm run build:dev"
    80. zip dir: "dist/${params.PROJECT_NAME}", archive: true, glob:'', zipFile: ZIP_FILE_NAME
    81. }
    82. }
    83. }
    84. stage('部署至服务器') {
    85. steps {
    86. script {
    87. def ansible_str = params.HOST_IP+' ansible_ssh_user='+params.HOST_USER+' ansible_ssh_port='+params.HOST_PORT+' ansible_ssh_pass="'+params.HOST_PASS+'"'
    88. writeFile file: 'ansible-hosts', text: '[all]'+'\n'+ansible_str
    89. sh "ansible ${params.HOST_IP} -i ansible-hosts -m shell -a 'mkdir -p ${REMOTE_PATH}'"
    90. sh "ansible ${params.HOST_IP} -i ansible-hosts -m unarchive -a 'src=${ZIP_FILE_NAME} dest=${REMOTE_PATH} mode=0755'"
    91. }
    92. }
    93. }
    94. }
    95. post {
    96. cleanup {
    97. deleteDir()
    98. dir(env.WORKSPACE+"@tmp") {
    99. deleteDir()
    100. }
    101. }
    102. }
    103. }

    上诉代码可以通过,参数化配置生成
    image.png
    参数配置,操作界面,有配置界面自动生成,并且接受相关参数
    image.png