Coding Jenkinsfile

小程序自动打包上传

此代码片以 uniapp 项目为例,可以根据需要自己改进 Jenkinsfile 编排

coding 为例

pipeline {
  agent {
        docker {
            reuseNode 'true'
            registryUrl 'https://coding-public-docker.pkg.coding.net'
            image 'public/docker/nodejs:18-2022'
        }
    }
  stages  {
    stage("检出") {
      steps {
        checkout(
          [$class: 'GitSCM',
          branches: [[name: GIT_BUILD_REF]],
          userRemoteConfigs: [[
            url: GIT_REPO_URL,
              credentialsId: CREDENTIALS_ID
            ]]]
        )
      }
    }
        
    stage('安装依赖') {
        steps {
            sh "npm install miniprogram-ci pnpm -g --registry https://registry.npmmirror.com"
            sh "pnpm install --registry https://registry.npmmirror.com"
        }
    }
    
    stage('编译') {
      steps {
        sh "pnpm run build:mp-weixin"
      }
    }

    stage('上传到微信小程序') {
        environment {
            GIT_LAST_COMMIT = "${sh(script:'git log --oneline -n 1', returnStdout: true).trim()}"
        }
        
        steps {
          script {
              if (env.TAG_NAME ==~ /.*/ ) {
                ARTIFACT_VERSION = "${env.TAG_NAME}"
              } else if (env.MR_SOURCE_BRANCH ==~ /.*/ ) {
                ARTIFACT_VERSION = "mr-${env.MR_RESOURCE_ID}-${env.GIT_COMMIT_SHORT}"
              } else {
                ARTIFACT_VERSION = "${env.BRANCH_NAME.replace('/', '-')}-${env.GIT_COMMIT_SHORT}"
              }
            }
            withCredentials([sshUserPrivateKey(credentialsId: "${env.WX_PRIVATE_KEY}",keyFileVariable: 'identity')]) {
                sh "miniprogram-ci upload --pp ./dist/build/mp-weixin/ --pkp ${identity} --appid ${WX_APPID} --uv ${ARTIFACT_VERSION} -r 1 --upload-description '${TRIGGER_USER_NAME} / ${GIT_LAST_COMMIT}' --enable-es6 true  --enable-minifyJS true --enable-minifyWXSS true --enable-autoPrefixWXSS true"
            }
        }
    }
  }
}

参数说明:

  • ARTIFACT_VERSION: 版本号, 参考 自动生成版本号open in new window
  • TRIGGER_USER_NAME: 触发用户
  • GIT_LAST_COMMIT: 最后提交 commit
  • WX_PRIVATE_KEY: 配置ci变量小程序上传证书 (coding凭据)
  • WX_APPID: 配置ci变量小程序appid (字符串)

Tip

  • 仅推送 tag 时触发,配置正则规则 ^refs/tags/.+$
Last Updated:
Contributors: 余小波