Set up Jenkins Job to deploy your API
This section provides with sample Jenkins Pipeline script and other prequisites to set up a CI/CD pipeline to deploy your APIs.
Configure the following dynamic values in the publishing job:
- Token - Access Key is availabe by logging into Trimble Cloud Console. For more details, refer the Access Key topic.
- Product Name - The API Product should be available in Trimble Cloud Console.
- Proxy Name - Accepts Varchar
- Proxy Version - Example: 1.0, v1
- Basepath - Backend service URL path. Do not change the basepath when deploying in multiple environments of the proxy. If you change the basepath for each environment, Jenkins job will fail.
- namespace - Namespace tags API Proxies by Trimble applications or products. For example, SketchUp, Trimble Cloud Platform, Tekla and so on. Namespace has to be same when deploying into each environment. If you change the namespaces for each environment, the Jenkins job will fail.
- Deploy environment - Available environments are dev, qa, stage, prod.
- Git URL - Must point to your source control
- Branch
- Scope Prefix - Name of the resource server application that will be registered with identity for the proxy. The Scope Prefix must preferably same as the API Proxy name, in lowercase. However, you can edit this field. The Scope Prefix helps API publishers create applications (separate entries for Pre-prod and Prod) directly without using the Application menu. This field is introduced to restrict application usage in future release cycles.
Sample Jenkins Pipeline Script
pipeline { agent any
stages { stage('PreRequisites') { steps{ sh 'java -version' } } stage('checkout-zip-proxy') { steps {
// check out proxy template from scm. // for eg - here checkout from git SCM url - ssh://git@xxxxx/sample.git // credentials to connect git scm is configured in jenkins as prerequisite checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'svcacct_tcloudcibuild', url: '${git_url}']]])
// sh "mkdir apiproxy" sh "zip -r proxy.zip apiproxy"
}
}
stage('create-proxy') { steps{
sh label: 'curl call', script: 'curl -X POST \"https://cloud.api.trimble.com/proxy-management/publisher/api/${proxy}/version/${version}/environment/${deployEnvironment}?product=${productName}&scopePrefix=${scopePrefix}\" -H \'Content-Type:multipart/form-data\' -H \"Authorization: Bearer ${token}\" -F file=@./proxy.zip -F basepath=${basepath} -F namespace=${namespace}' sh "rm -rf apiproxy" sh "rm -f proxy.zip"
} } stage('deploystatus') { steps {
script { for (int i = 0; i < 15; ++i) { def statusvalue = sh(returnStdout: true, script: 'curl -XGET \"https://cloud.api.trimble.com/proxy-management/publisher/api/${proxy}-${version}/environment/${deployEnvironment}/deploystatus\" -H \'Content-Type:application/json\' -H \"Authorization: Bearer ${token}\" ').trim() echo "${statusvalue}"
if (statusvalue == "{\"status\":\"DEPLOY_COMPLETED\",\"phases\":null}" || statusvalue == "{\"status\":\"DEPLOY_FAILED\",\"phases\":null}") { echo "value matches" break } else { echo "value dowsntmatch" } }
}
} } }}