Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/test/groovy/ApmBasePipelineTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ class ApmBasePipelineTest extends BasePipelineTest {
}
}
if (lastError) {
updateBuildStatus('FAILURE')
throw lastError
}
})
Expand Down
3 changes: 2 additions & 1 deletion src/test/groovy/WithVaultTokenStepTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ class WithVaultTokenStepTests extends ApmBasePipelineTest {
}
} catch(e){
//NOOP
assertTrue(e instanceof Exception)
assertTrue(e.toString().contains('Mock an error'))
}
printCallStack()
assertTrue(assertMethodCallContainsPattern('error', 'withVaultToken: error'))
assertTrue(assertMethodCallContainsPattern('sh', 'rm .vault-token'))
assertJobStatusFailure()
}
Expand Down
8 changes: 6 additions & 2 deletions vars/checkout.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ def call(params) {
def retryWithSleep(int i, body) {
retry(i) {
log(level: 'DEBUG', text: "Let's checkout (${i} tries).")
sleep(20)
body()
try {
body()
} catch(e) {
sleep(20)
throw e
}
}
}
18 changes: 12 additions & 6 deletions vars/dockerLogin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ def call(Map params = [:]){
"DOCKER_PASSWORD=${dockerPassword}"
]) {
retry(3) {
sleep randomNumber(min: 5, max: 10)
sh(label: "Docker login", script: """
set +x
host ${registry} 2>&1 > /dev/null
docker login -u "\${DOCKER_USER}" -p "\${DOCKER_PASSWORD}" "${registry}" 2>/dev/null
""")
try {
sh(label: "Docker login", script: """
set +x
host ${registry} 2>&1 > /dev/null
docker login -u "\${DOCKER_USER}" -p "\${DOCKER_PASSWORD}" "${registry}" 2>/dev/null
""")
} catch(e) {
// When running in the CI with multiple parallel stages
// the access could be considered as a DDOS attack. Let's sleep a bit if it fails.
sleep randomNumber(min: 5, max: 10)
throw e
}
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions vars/getVaultSecret.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@ def readSecret(secret) {
def props = null
log(level: 'INFO', text: 'getVaultSecret: Getting secrets')
readSecretWrapper() {
retry(2) {
sleep randomNumber(min: 5, max: 10)
def token = getVaultToken(env.VAULT_ADDR, env.VAULT_ROLE_ID, env.VAULT_SECRET_ID)
props = getVaultSecretObject(env.VAULT_ADDR, secret, token)
retry(3) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

increased the number of retries a shorter sleep

try {
def token = getVaultToken(env.VAULT_ADDR, env.VAULT_ROLE_ID, env.VAULT_SECRET_ID)
props = getVaultSecretObject(env.VAULT_ADDR, secret, token)
} catch(e) {
// When running in the CI with multiple parallel stages
// the access could be considered as a DDOS attack. Let's sleep a bit if it fails.
sleep randomNumber(min: 2, max: 5)
throw e
}
}
//we do not have permissions to revoke a token.
//revokeToken(env.VAULT_ADDR, token)
Expand Down
8 changes: 6 additions & 2 deletions vars/git.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ def call(params) {
def retryWithSleep(int i, body) {
retry(i) {
log(level: 'DEBUG', text: "Let's git (${i} tries).")
sleep(20)
body()
try {
Copy link
Contributor

@kuisathaverat kuisathaverat Feb 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a new step in the horizon 😄

sleepOnError(min: 0, max:30){
  echo "I am a command"
}
def call(Map params = [:], Closure body) {
  def min = params.containsKey('min') ? params.min : 0
  def max = params.containsKey('max') ? params.max : 30
  try{
    body()
  } catch (e){
    sleep randomNumber(min: min, max: max)
    throw e
  }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, I actually was thinking to provide that step as a built-in step in the declarative pipeline:

retry(times: 3, sleep: 5)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jenkinsci/workflow-basic-steps-plugin#97 is the one for that particular requirement :)

body()
} catch(e) {
sleep(20)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sleep only if it fails, then let's honor those steps that don't have a timeout issue

throw e
}
}
}
7 changes: 4 additions & 3 deletions vars/withVaultToken.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ def call(Map params = [:], Closure body) {
def path = params.containsKey('path') ? params.path : env.WORKSPACE
def tokenFile = params.containsKey('tokenFile') ? params.tokenFile : '.vault-token'
getVaultSecret.readSecretWrapper {
retry(2) {
sleep randomNumber(min: 5, max: 10)
retry(3) {
def token = getVaultSecret.getVaultToken(env.VAULT_ADDR, env.VAULT_ROLE_ID, env.VAULT_SECRET_ID)
dir(path) {
writeFile file: tokenFile, text: token
}
try {
body()
} catch (err) {
error "withVaultToken: error ${err}"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrequired error, throw err is the one in charge.

// When running in the CI with multiple parallel stages
// the access could be considered as a DDOS attack. Let's sleep a bit if it fails.
sleep randomNumber(min: 2, max: 5)
throw err
} finally {
// ensure any sensitive details are deleted
Expand Down