Attempt atomic update of repositories.

Change-Id: I76eb5010235d4f705b6f55bb536844727a46e6e0
diff --git a/cicd/pipelines/tasks/task_publish_unstable.jenkins b/cicd/pipelines/tasks/task_publish_unstable.jenkins
index 13c4f85..6c9c4e3 100644
--- a/cicd/pipelines/tasks/task_publish_unstable.jenkins
+++ b/cicd/pipelines/tasks/task_publish_unstable.jenkins
@@ -10,6 +10,16 @@
     return sh(returnStdout: true, script: script)
 }
 
+def installGpgKeyring() {
+    sh """
+       install -d -m 700 -o root -g root /var/lib/aptly/.gnupg
+       tar -C /var/lib/aptly/.gnupg -zxf /var/lib/aptly/keyring/release-keyring.tar.gz
+       chown -R root:root /var/lib/aptly/.gnupg
+       find /var/lib/aptly/.gnupg -type d -exec chmod 700 '{}' ';'
+       find /var/lib/aptly/.gnupg -type f -exec chmod 600 '{}' ';'
+       """
+}
+
 def workspacePath = "/home/jenkins/workspace"
 def buildLabel = "task.publish.unstable-${UUID.randomUUID().toString()}"
 def sourcePath = "${workspacePath}/src"
@@ -33,27 +43,21 @@
             container('debian') {
                 sh "cp /etc/aptly.conf ~/.aptly.conf"
                 withEnv(['GNUPGHOME=/var/lib/aptly/.gnupg']) {
-                    sh """
-                       install -d -m 700 -o root -g root /var/lib/aptly/.gnupg
-                       tar -C /var/lib/aptly/.gnupg -zxf /var/lib/aptly/keyring/release-keyring.tar.gz
-                       chown -R root:root /var/lib/aptly/.gnupg
-                       find /var/lib/aptly/.gnupg -type d -exec chmod 700 '{}' ';'
-                       find /var/lib/aptly/.gnupg -type f -exec chmod 600 '{}' ';'
-                       gpg --list-keys
-                       gpg --list-secret-keys
-                       """
-
                     def debianMirrorSnapshotName = getLatestSnapshot('debian-buster')
                     def coreSnapshotName = getLatestSnapshot('core-unstable')
                     def bspSnapshotName  = getLatestSnapshot('bsp-enterprise-unstable')
                     def date = new Date()
                     String stamp = date.format("yyyyMMdd-HHmmss")
 
+                    installGpgKeyring()
+
                     sh """
                        mkdir -p /var/lib/aptly/publishes/unstable
                        aptly snapshot merge core-full-unstable-${stamp} ${debianMirrorSnapshotName} ${coreSnapshotName}
-                       aptly publish snapshot --batch --passphrase-file=/var/lib/aptly/keyring/passphrase.txt --distribution=unstable core-full-unstable-${stamp} filesystem:unstable:unstable
-                       aptly publish snapshot --batch --passphrase-file=/var/lib/aptly/keyring/passphrase.txt --distribution=unstable ${bspSnapshotName} filesystem:unstable:unstable
+                       aptly publish snapshot --batch --passphrase-file=/var/lib/aptly/keyring/passphrase.txt --distribution=unstable core-full-unstable-${stamp} filesystem:unstable:unstable \
+                           || aptly publish switch --batch --passphrase-file=/var/lib/aptly/keyring/passphrase.txt unstable filesystem:unstable:unstable core-full-unstable-${stamp}
+                       aptly publish snapshot --batch --passphrase-file=/var/lib/aptly/keyring/passphrase.txt --distribution=unstable ${bspSnapshotName} filesystem:unstable:unstable \
+                           || aptly publish switch --batch --passphrase-file=/var/lib/aptly/keyring/passphrase.txt unstable filesystem:unstable:unstable ${bspSnapshotName}
                        """
                 }
             }