+ def charmFilename = sh(
+ returnStdout: true,
+ script: 'cat pack.json | jq -r ".charms[0], .bundles[0]" | grep -v null').trim()
+
+ withCredentials([string(credentialsId: 'Charmstore', variable: 'CHARMCRAFT_AUTH')]) {
+ try {
+ println("Uploading $charmFilename")
+ sh "charmcraft upload --format json --verbosity brief $charmFilename 1> upload.json 2> upload-stderr.log"
+ } catch (Exception e) {
+ def errorMessage = sh(
+ returnStdout: true,
+ script: 'cat upload.json | jq -r ".errors[0].message" | grep -v null').trim()
+ if (errorMessage.contains("already exists")) {
+ println("No changes, skipping upload")
+ return 0
+ }
+ sh "cat upload-stderr.log"
+ throw e
+ }
+ def charmRevision = sh(
+ returnStdout: true,
+ script: 'cat upload.json | jq -r ".revision"').trim()
+
+ def charmBranch="merged"
+ if ( !JOB_NAME.contains('merge') ) {
+ charmBranch="${GERRIT_REFSPEC}".replaceAll('/','-')
+ }
+ def channel="latest"
+ if (BRANCH_NAME.startsWith("v")) {
+ channel=BRANCH_NAME.substring(1)
+ } else if (BRANCH_NAME!="master" && JOB_NAME.contains('merge')) {
+ charmBranch=BRANCH_NAME
+ }
+
+ def charmName = sh(
+ returnStdout:true,
+ script: "if [ -f bundle.yaml ];then cat bundle.yaml;else cat metadata.yaml;fi|yq -r .name").trim()
+
+ def resourceArgument = ""
+ try {
+ def resourceName = sh(
+ returnStdout:true,
+ script: "cat metadata.yaml | yq '.resources | keys | .[0]'"
+ ).trim()
+ if( resourceName != "null" ) {
+ sh "charmcraft resource-revisions $charmName $resourceName | head -2 | tail -1 | awk '{print \$1}'"
+ def resourceVers = sh(
+ returnStdout:true,
+ script: "charmcraft resource-revisions $charmName $resourceName | head -2 | tail -1 | awk '{print \$1}'"
+ ).trim()
+
+ resourceArgument = "--resource $resourceName:$resourceVers"
+
+ resourceName = sh(
+ returnStdout:true,
+ script: "cat metadata.yaml | yq '.resources | keys | .[1]'").trim()
+
+ if( resourceName != "null" ) {
+ resourceVers = sh(
+ returnStdout:true,
+ script: "charmcraft resource-revisions $charmName $resourceName | head -2 | tail -1 | awk '{print \$1}'"
+ ).trim()
+ resourceArgument += " --resource $resourceName:$resourceVers"
+ }
+ }
+
+ } catch (Exception discard) {
+ print("No resource associated with bundle, skipping")
+ }
+
+ print("resourceArgument = $resourceArgument")
+
+ sh "charmcraft release ${charmName} --revision=${charmRevision} ${resourceArgument} --channel=${channel}/edge/${charmBranch}"
+ }
+ }
+ } finally {
+ concurrency++
+ }
+ }
+ }
+ }
+ }
+ parallel parallelSteps
+ }
+ if ( do_stage_3 ) {