Skip to main content

Importing objects using Adeptia Automate APIs in Jenkins

Adeptia provides an import pipeline file that incorporates Adeptia Automate APIs for importing, allowing you to generate the import pipeline. This section outlines the steps for creating an import pipeline and then activating it to import objects into a target environment.

The import pipeline requires the ID of the Import package, which should be created beforehand as a prerequisite for the import process. For more details on the necessary prerequisites for importing objects, refer to this page.

Creating and triggering the import pipeline ​

To initiate the import of objects, the initial step involves creating an import pipeline with all necessary parameters. Once the pipeline is established, it must be triggered to execute the import operation.

Follow the steps below to create the pipeline using the provided import pipeline file from Adeptia and activate it.

  1. Log in to the Jenkins with admin privileges.

  2. Select New Item.

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

  4. Click OK.

  5. Copy the content from the provided import pipeline file. [+] Import 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 statusImport

/*
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: 'rollbackzip.zip', description: 'Path to upload rollback zip file to GitHub repository. e.g. test/SA_PF.zip', name: 'ROLLBACK_ZIP_PATH', trim: true)
string(defaultValue: 'migration.log', description: 'import log file path to upload to GitHub repository. e.g. test/import.log', name: 'IMPORT_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: 'Import package ID. e.g. 1235562031212253184', name: 'IMPORT_PACKAGE_ID', trim: true)
string(defaultValue: 'exportzip.zip', description: 'Path of source zip file to download from GitHub repository. e.g. test/SA_PF.zip', name: 'SOURCE_ZIP_PATH', 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 Import using pkg ID') {

steps {
//hide password field
wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [[password:loginToken]]]) {
script {
echo 'API call to Trigger Import using pkg ID'
def response = sh (
script: 'curl --location \''+APPLICATION_URL+'/rest/migration/import/execute/'+IMPORT_PACKAGE_ID+'\' --header \'ACCESS_TOKEN: '+loginToken+'\' --form \'sourceZipFile=@\"'+WORKSPACE+'/'+SOURCE_ZIP_PATH+'\"\'',
returnStdout: true
).trim()
println("respose: "+response)
if (response.contains('success'))
{
def success = new JsonSlurperClassic().parseText(response).success
if (success.equals(true)){
println("Triggered successfully")
referenceId = new JsonSlurperClassic().parseText(response).referenceId
} else {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE', catchInterruptions: true){echo "CaughtError: "+response}
sh "exit 1"
}
}
else if (response.contains('status'))
{
def status = new JsonSlurperClassic().parseText(response).status
if (status.equalsIgnoreCase("ERROR")){
def message = new JsonSlurperClassic().parseText(response).message
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE', catchInterruptions: true){echo "CaughtError: "+message}
sh "exit 1"
}
}
else {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE', catchInterruptions: true){echo "CaughtError: "+response}
sh "exit 1"
}
}
}
}
}
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
statusImport = new JsonSlurperClassic().parseText(response.content).status
if(statusImport.equalsIgnoreCase('FINISHED'))
{
println('FINISHED')
break;
}
else {println(statusImport)}
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 rollback zip") {
when {
expression {
return statusImport == 'FINISHED';
}
}
steps {
script {
echo 'Download rollback 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=rollbackzip", validResponseCodes: "200", outputFile: ROLLBACK_ZIP_PATH
}
}
}
stage("Commit rollback zip to GitHub") {
when {
expression {
return statusImport == 'FINISHED';
}
}
steps {
pushToGitHub (GIT_BRANCH, GIT_CREDENTIALS_ID, ROLLBACK_ZIP_PATH)
}
}
stage("Download migration logs") {
when {
expression {
return statusImport != '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: IMPORT_LOG_PATH
}
}
}
stage("Commit log file to GitHub") {
when {
expression {
return statusImport != 'FINISHED';
}
}
steps {
pushToGitHub (GIT_BRANCH, GIT_CREDENTIALS_ID, IMPORT_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 import pipeline file.  11. Enter the parameter values as per your requirement.

InformationIt is mandatory to provide a valid value for all the Jenkins parameters.

[+] Click here to expand the list of Import 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 Import operation.
SOURCE_ZIP_PATHtest/export.zipGitHub repository path of the export zip file that you want to use to perform the Import operation.
IMPORT_LOG_PATHtest/migration.logPath in the GitHub repository where the import logs file will be placed after the import operation completes.
IMPORT_PACKAGE_ID1235472230119257845Id of the Import package.

For more details on viewing the Id of an Import package, refer to this section.
ROLLBACK_ZIP_PATHtest/rollback.zipPath in the GitHub repository where the rollback zip will be placed after the import operation completes.
  1. Click Build to trigger the pipeline. This imports the objects to the target environment and creates a corresponding rollback zip which is placed in the GitHub repository at the location you defined in the ROLLBACK_ZIP_PATH  parameter.