Skip to main content

Exporting objects using Adeptia Automate APIs in Jenkins

Adeptia offers an export pipeline file, integrating Adeptia Automate APIs for exporting, which you can utilize to generate the export pipeline. This section elaborates on the steps to create an export pipeline and subsequently activate it to export objects from an environment.

The export pipeline relies on the Export package's ID, which you must have created beforehand as a prerequisite for the export process. For further information regarding the necessary prerequisites for exporting objects, refer to this page.

Creating and triggering the Export pipeline​

To begin exporting the objects, the initial step involves creating an export pipeline containing all necessary parameters for the export process. Once the pipeline is established, it needs to be triggered to execute the export operation.

To set up the pipeline in Jenkins utilizing the export pipeline supplied by Adeptia, adhere to the following steps.

  1. Log in to the Jenkins with admin privileges.

  2. Select New Item.

  3. Enter a name for the Export pipeline, and then select Pipeline.

  4. Click OK.

  5. Copy the content from the provided export pipeline file. [+] Export Pipeline [-] Hide

Code

//use JsonSlurperClassic because it produces HashMap that could be serialized by pipeline
import groovy.json.JsonSlurperClassic
import jenkins.model.Jenkins

def loginToken
def referenceId
def interval = 30
def timeout= 15
def statusExport


/*
Get username from credentials id
*/
def getUserName(id) {
def userName = null
withCredentials([usernamePassword(credentialsId: id, passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME')]) {
try {
userName = USERNAME
} catch (err) {
echo "Caught: ${err}. Error in extracting username from "+id+" ."
error("Caught: ${err}")
currentBuild.result = 'FAILURE'
}
}
return userName
}

/*
Get password from credentials id
*/
def getPassword(id) {
def password = null
withCredentials([usernamePassword(credentialsId: id, passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME')]) {
try {
password = PASSWORD;
} catch (err) {
echo "Caught: ${err}. Error in extracting password from "+id+" ."
error("Caught: ${err}")
currentBuild.result = 'FAILURE'
}
}
return password
}

/*
Push files to GitHub repository
*/
def pushToGitHub (GIT_BRANCH, GIT_CREDENTIALS_ID, FILE_PATH) {
echo "Pushing file ("+FILE_PATH+") to GitHub repo"
script {
echo "Pushing file (${WORKSPACE}/"+FILE_PATH+") to GitHub repo"
withCredentials([gitUsernamePassword(credentialsId: GIT_CREDENTIALS_ID, gitToolName: 'git-tool')]) {
try {
def gitUser = getUserName(GIT_CREDENTIALS_ID);
sh('sleep 10')
sh('git config --global user.name "'+gitUser+'"')
sh('git config --global user.email "you@example.com"')
sh("git add "+FILE_PATH)
sh('git commit -m "auto commit message" ')
sh('git push origin HEAD:'+GIT_BRANCH)
} catch (err) {
echo "Caught: ${err}. Error in pushing file to Github."
error("Caught: ${err}")
currentBuild.result = 'FAILURE'
}
}
}
}

pipeline {
parameters{
separator(name: 'separator-ce1a9ef5-cd10-4002-a43f-8ae24d9d0bb3', sectionHeader: '''GitHub Parameters''', sectionHeaderStyle: 'background-color:#eeeee4;font-size:15px;font-weight:normal;text-transform:uppercase;border-color:gray;', separatorStyle: '''font-weight:bold;line-height:1.5em;font-size:1.5em;''')
string(defaultValue: '', description: 'GitHub credentials ID configured in Jenkins e.g. gitCredential_id', name: 'GIT_CREDENTIALS_ID', trim: true)
string(defaultValue: '', description: 'GitHub server URL e.g https://github.com/adeptia/migration-defination.git', name: 'GIT_REPO_URL', trim: true)
string(defaultValue: 'main', description: 'GitHub Branch name e.g. main', name: 'GIT_BRANCH', trim: true)
string(defaultValue: 'exportzip.zip', description: 'Path to upload export zip file to GitHub repository. e.g. test/SA_PF.zip', name: 'EXPORT_ZIP_PATH', trim: true)
string(defaultValue: 'migration.log', description: 'export log file path to upload to GitHub repository. e.g. test/migration.log', name: 'EXPORT_LOG_PATH', trim: true)
separator(name: 'separator-ce1a9ef5-cd10-4002-a43f-8ae24d9d0bb3', sectionHeader: '''Migration Parameters''', sectionHeaderStyle: 'background-color:#eeeee4;font-size:15px;font-weight:normal;text-transform:uppercase;border-color:gray;', separatorStyle: '''font-weight:bold;line-height:1.5em;font-size:1.5em;''')
string(defaultValue: '', description: 'Application URL. e.g. https://test.api.com', name: 'APPLICATION_URL', trim: true)
string(defaultValue: 'Application_Credential_id', description: 'Application credentials ID configured in Jenkins e.g. Application_Credential_id', name: 'APPLICATION_CREDENTIALS_ID', trim: true)
string(defaultValue: '', description: 'Export package ID. e.g. 1235472230119256064', name: 'EXPORT_PACKAGE_ID', trim: true)
}
/*
agent {
label 'LinuxAgent'
}
*/
agent any

stages {
stage('Initialize git repository') {
steps {

echo 'Checkout from GitHub'
checkout([$class: 'GitSCM', branches: [[name: '*/'+GIT_BRANCH]], extensions: [], userRemoteConfigs: [[credentialsId: GIT_CREDENTIALS_ID, url: GIT_REPO_URL]]])
}
}
stage('Initialize to get user token') {
steps {
echo 'Initialize to get user token'
script {
withCredentials([gitUsernamePassword(credentialsId: APPLICATION_CREDENTIALS_ID)]) {
def response = httpRequest contentType: 'APPLICATION_JSON', httpMode: 'POST', requestBody: '{"username":\"'+getUserName(APPLICATION_CREDENTIALS_ID)+'\","password":\"'+getPassword(APPLICATION_CREDENTIALS_ID)+'\"}', url: APPLICATION_URL+'/rest/login/user'
loginToken = new JsonSlurperClassic().parseText(response.content).ACCESS_TOKEN
}
}
}
}
stage('API call to Trigger export using pkg ID') {
steps {
script {
echo 'API call to Trigger export using pkg ID'
def response = httpRequest contentType: 'APPLICATION_JSON', httpMode: 'POST', customHeaders: [[name: 'ACCESS_TOKEN', value: loginToken, maskValue: 'true']], url: APPLICATION_URL+"/rest/migration/export/execute/"+EXPORT_PACKAGE_ID
def triggerExport = response.content
referenceId = new JsonSlurperClassic().parseText(response.content).referenceId
}
}
}
stage('Status check using ref ID') {
steps {
script {
echo 'Status check using ref ID'
count = 1
for( int i = 1; i <= timeout; i++ ) {
def response = httpRequest contentType: 'APPLICATION_JSON', httpMode: 'GET', customHeaders: [[name: 'ACCESS_TOKEN', value: loginToken, maskValue: 'true']], url: APPLICATION_URL+"/rest/migration/status?id="+referenceId
statusExport = new JsonSlurperClassic().parseText(response.content).status
if(statusExport.equalsIgnoreCase('FINISHED'))
{
println('FINISHED')
break;
}
else {println(statusExport)}
sleep(interval)
echo count+" retry in "+interval*count+" seconds. timout:"+timeout
if ((count)>= timeout ){
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE'){echo "Caught: Migration deployment taking more then ideal time. See the migration logs."}
break;
}
count=count+1
}
}
}
}
stage("Download export zip") {
when {
expression {
return statusExport == 'FINISHED';
}
}
steps {
script {
echo 'Download export zip'
def response = httpRequest contentType: 'APPLICATION_ZIP', httpMode: 'GET', customHeaders: [[name: 'ACCESS_TOKEN', value: loginToken, maskValue: 'true']], url: APPLICATION_URL+"/rest/migration/downloadzip?id="+referenceId+"&type=exportzip", validResponseCodes: "200", outputFile: EXPORT_ZIP_PATH
}
}
}
stage("Commit export zip to GitHub") {
when {
expression {
return statusExport == 'FINISHED';
}
}
steps {
pushToGitHub (GIT_BRANCH, GIT_CREDENTIALS_ID, EXPORT_ZIP_PATH)
}
}
stage("Download migration logs") {
when {
expression {
return statusExport != 'FINISHED';
}
}
steps {
script {
echo 'Download log file'
def response = httpRequest contentType: 'TEXT_PLAIN', httpMode: 'GET', customHeaders: [[name: 'ACCESS_TOKEN', value: loginToken]], url: APPLICATION_URL+"/rest/migration/logs?id="+referenceId, outputFile: EXPORT_LOG_PATH
}
}
}
stage("Commit log file to GitHub") {
when {
expression {
return statusExport != 'FINISHED';
}
}
steps {
pushToGitHub (GIT_BRANCH, GIT_CREDENTIALS_ID, EXPORT_LOG_PATH)
}
}
}
post('Clean-up') {
always {
echo 'Cleanup workspace'
cleanWs()
}
success {
echo 'Pipeline succeeded!'
}
unstable {
echo 'Pipeline unstable :/'
}
failure {
echo 'Pipeline failed :('
}
}
}
  1. In the Pipeline Definition section, paste the copied content and uncheck the Use Groovy Sandbox checkbox.

    | Error | If you are using Jenkins on Windows OS, and have created an agent on Linux OS, you need to do the following in the export pipeline file.

    1. Uncomment the following code snippet.
    Code

/*
agent {
label 'LinuxAgent'
}
*/


Where,

LinuxAgent is the name of the agent that you have created.

2. Comment the following lines of code.
Code

agent any

| | --- | --- | 7. Click Save. 8. On the screen that follows, click Build Now. As you build the pipeline for the very first time, all the parameters get initialized. 9. Refresh the page. The Build Nowoption now changes to Build with Parameters. 10. Click Build with Parameters. You will see all the parameters inherited from the export pipeline file.  11. Enter the parameter values as per your requirement. [+] Click here to expand the list of Export parameters [-] Hide

ParametersValueDescription
GIT_CREDENTIALS_ID<credential ID generated by Jenkins>Credential ID for GitHub in Jenkins.

Refer to the prerequisitesfor more details.
GIT_REPO_URLhttps://github.com/adeptia/migration-definition.gitURL of the GitHub repository.
GIT_BRANCHmainGitHub branch name.
APPLICATION_URLhttps://abc.comAdeptia Automate application URL.
APPLICATION_CREDENTIALS_IDuser@abc.comAdeptia Automate User Id to be used for performing the Export operation.
EXPORT_ZIP_PATHtest/export.zipPath in the GitHub repository where the export zip will be placed after the Export operation completes.
EXPORT_LOG_PATHtest/migration.logPath in the GitHub repository where the export logs file will be placed after the Export operation completes.
EXPORT_PACKAGE_ID1235472230119256064Id of the export package.

For more details on viewing the Id of an Export package, refer to this section.
  1. Click Build to trigger the pipeline. This exports the objects to an export zip which is placed in the GitHub repository at the location you defined in the EXPORT_ZIP_PATH parameter. After you have exported the objects, you need to import them to the new environment by creating and triggering the import pipeline.