Last active
November 28, 2019 10:05
-
-
Save bmpc/1123afe5690daf47934be0821c500465 to your computer and use it in GitHub Desktop.
[Xray Cloud] Cucumber Jenkins pipeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pipeline { | |
environment { | |
xray_server = "https://xray.cloud.xpand-it.com" | |
client_id = "XXXXXX" | |
client_secret = "XXXXX" | |
} | |
agent { docker { image 'ruby' } } | |
stages { | |
stage("Sync Features") { | |
steps { | |
script { | |
echo "Parsing changed features..." | |
def feature_files = getFeatureFiles() | |
if (feature_files) { | |
echo "Generating changed features zip..." | |
echo "Features: ${feature_files}" | |
zip zipFile: "features.zip", archive: false, glob: "${feature_files}" | |
echo "Generating Xray token..." | |
def token = sh(script: "curl -H \"Content-Type: application/json\" -X POST --data '{ \"client_id\": \"${client_id}\",\"client_secret\": \"${client_secret}\" }' ${xray_server}/api/v1/authenticate", returnStdout: true) | |
echo "Authentication token: ${token}" | |
echo "Uploading feature files to Xray..." | |
sh "curl -H \"Content-Type: multipart/form-data\" -X POST -H \"Authorization: Bearer ${token}\" -F \"file=@features.zip\" \"${xray_server}/api/v1/import/feature?projectKey=STORE\" > sync-result.json" | |
} else { | |
echo "No *.feature files detected." | |
} | |
} | |
} | |
} | |
stage("Tests") { | |
steps { | |
script { | |
echo "Testing Cucumber Features..." | |
if (fileExists("sync-result.json")) { | |
def con = sh(script: "cat sync-result.json", returnStdout: true) | |
echo "${con}" | |
def props = readJSON file: "sync-result.json" | |
if (props) { | |
def testIssueKeys = props.updatedOrCreatedTests.collect{e -> e.key}.join(";") | |
if (testIssueKeys) { | |
echo "Test Issue Key: ${testIssueKeys}" | |
echo "ruby --version" | |
sh "gem install cucumber" | |
sh "gem install rspec-expectations" | |
echo "Generating Xray token..." | |
def token = sh(script: "curl -H \"Content-Type: application/json\" -X POST --data '{ \"client_id\": \"${client_id}\",\"client_secret\": \"${client_secret}\" }' ${xray_server}/api/v1/authenticate", returnStdout: true) | |
echo "Authentication token: ${token}" | |
echo "Fetching features from Xray..." | |
sh "curl -H \"Content-Type: application/json\" --output features/features.zip -X GET -H \"Authorization: Bearer ${token}\" ${xray_server}/api/v1/export/cucumber?keys=STORE-693" | |
sh "rm -f features/*.feature" | |
sh "pwd" | |
unzip zipFile: "features/features.zip", dir: "features/" | |
echo "Executing with Cucumber..." | |
sh "cucumber -x -f json -o data.json" | |
def shortCommit = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim() | |
echo "Creating info file for Test Exec..." | |
def meta = readJSON text: "{ \ | |
\"fields\": { \ | |
\"project\": { \ | |
\"key\": \"STORE\" \ | |
}, \ | |
\"issuetype\": { \ | |
\"name\": \"Test Execution\" \ | |
}, \ | |
\"summary\": \"GitLab results for commit ${shortCommit}\", \ | |
\"fixVersions\": [ \ | |
{ \ | |
\"name\": \"1.0\" \ | |
} \ | |
] \ | |
}, \ | |
\"xrayFields\": { \ | |
\"testPlanKey\": \"STORE-34\" \ | |
} \ | |
}" | |
writeJSON file: "info.json", json: meta | |
echo "Uploading results to Xray..." | |
sh "curl -H \"Content-Type: multipart/form-data\" -X POST -H \"Authorization: Bearer ${token}\" -F results=@data.json -F info=@info.json ${xray_server}/api/v1/import/execution/cucumber/multipart" | |
} | |
} | |
} else { | |
echo "No *.feature files detected. Skipping Test stage." | |
} | |
} | |
} | |
} | |
} | |
} | |
@NonCPS | |
def getFeatureFiles() { | |
def featureFiles = "" | |
echo "Gathering SCM changes" | |
def changeLogSets = currentBuild.changeSets | |
for (int i = 0; i < changeLogSets.size(); i++) { | |
def entries = changeLogSets[i].items | |
for (int j = 0; j < entries.length; j++) { | |
def entry = entries[j] | |
def files = new ArrayList(entry.affectedFiles) | |
for (int k = 0; k < files.size(); k++) { | |
def file = files[k] | |
if (file.path.endsWith(".feature")) { | |
featureFiles += file.path + ","; | |
} | |
echo "File: ${file.editType.name} ${file.path}" | |
} | |
} | |
} | |
if (!featureFiles) { | |
return null | |
} | |
return featureFiles | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment