Skip to content

Commit

Permalink
update updatecli share library to use agent label instead of image
Browse files Browse the repository at this point in the history
  • Loading branch information
smerle33 committed Jan 15, 2024
1 parent e6f0f38 commit 6e1dc28
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 56 deletions.
15 changes: 4 additions & 11 deletions test/groovy/UpdatecliStepTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ class UpdatecliStepTests extends BaseTest {
// Then we expect a successful build
assertJobStatusSuccess()

// And the correct pod template defined
assertTrue(assertMethodCallContainsPattern('containerTemplate', 'jenkinsciinfra/helmfile:'))
// And the correct default container memory
assertTrue(assertMethodCallContainsPattern('containerTemplate', 'resourceRequestMemory=512Mi'))
assertTrue(assertMethodCallContainsPattern('containerTemplate', 'resourceLimitMemory=512Mi'))
// And the correct pod agent used
assertTrue(assertMethodCallContainsPattern('node', 'jnlp-linux-arm64'))

// And the repository checkouted
assertTrue(assertMethodCallContainsPattern('checkout', ''))
Expand Down Expand Up @@ -75,17 +72,13 @@ class UpdatecliStepTests extends BaseTest {
// Then we expect a successful build
assertJobStatusSuccess()


// And the repository checkouted
assertTrue(assertMethodCallContainsPattern('checkout',''))

// And only the default command called with custom config and NO values
assertTrue(assertMethodCallContainsPattern('sh','updatecli diff --config ./ops/config.yml'))
assertFalse(assertMethodCallContainsPattern('sh','--values'))

// And the correct container memory
assertTrue(assertMethodCallContainsPattern('containerTemplate', 'resourceRequestMemory=512Mi'))
assertTrue(assertMethodCallContainsPattern('containerTemplate', 'resourceLimitMemory=512Mi'))
}

@Test
Expand Down Expand Up @@ -113,7 +106,7 @@ class UpdatecliStepTests extends BaseTest {
def script = loadScript(scriptName)

// when calling the "updatecli" function with a custom Docker image
script.call(updatecliDockerImage: 'golang:1.16-alpine')
script.call(updatecliAgentLabel: 'jnlp-linux-amd64')
printCallStack()

// Then we expect a successful build
Expand All @@ -123,7 +116,7 @@ class UpdatecliStepTests extends BaseTest {
assertTrue(assertMethodCallContainsPattern('checkout',''))

// And the correct pod template defined
assertTrue(assertMethodCallContainsPattern('containerTemplate', 'golang:1.16-alpine'))
assertTrue(assertMethodCallContainsPattern('node', 'jnlp-linux-amd64'))

// And only the diff command called with default values
assertTrue(assertMethodCallContainsPattern('sh','updatecli diff --config ./updatecli/updatecli.d --values ./updatecli/values.yaml'))
Expand Down
71 changes: 28 additions & 43 deletions vars/updatecli.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ def call(userConfig = [:]) {
action: 'diff', // Updatecli subcommand to execute
config: './updatecli/updatecli.d', // Config manifest used by updatecli (can be a file or a directory)
values: './updatecli/values.yaml', // Values file used by updatecli
updatecliDockerImage: 'jenkinsciinfra/helmfile:3.0.59', // Container image to use for running updatecli
containerMemory: '512Mi', // When using 'updatecliDockerImage', this is the memory limit+request of the container
updatecliAgentLabel: 'jnlp-linux-arm64', // replace updatecliDockerImage
cronTriggerExpression: '', // When specified, it enables cron trigger for the calling pipeline
credentialsId: 'github-app-updatecli-on-jenkins-infra', // githubApp or usernamePassword credentials id to use to get an Access Token. The corresponding populated env vars are USERNAME_VALUE & UPDATECLI_GITHUB_TOKEN
]
Expand All @@ -29,45 +28,31 @@ def call(userConfig = [:]) {
properties([pipelineTriggers([cron(finalConfig.cronTriggerExpression)])])
}

// The podTemplate must define only a single container, named `jnlp`
// Ref - https://support.cloudbees.com/hc/en-us/articles/360054642231-Considerations-for-Kubernetes-Clients-Connections-when-using-Kubernetes-Plugin
podTemplate(
containers: [
containerTemplate(
name: 'jnlp',
image: finalConfig.updatecliDockerImage,
resourceRequestCpu: '1',
resourceLimitCpu: '1',
resourceRequestMemory: finalConfig.containerMemory,
resourceLimitMemory: finalConfig.containerMemory,
),
]
) {
node(POD_LABEL) {
final String updatecliRunStage = "Run updatecli: ${finalConfig.action}"
boolean runUpdatecli = true
stage("Check if updatecli folder exists: ${finalConfig.action}") {
checkout scm
if (!fileExists('updatecli/')) {
echo 'WARNING: no updatecli folder.'
runUpdatecli = false
org.jenkinsci.plugins.pipeline.modeldefinition.Utils.markStageSkippedForConditional(updatecliRunStage)
}
}
stage(updatecliRunStage) {
if (runUpdatecli) {
withCredentials([
usernamePassword(
credentialsId: finalConfig.credentialsId,
usernameVariable: 'USERNAME_VALUE', // Setting this variable is mandatory, even if of not used when the credentials is a githubApp one
passwordVariable: 'UPDATECLI_GITHUB_TOKEN'
)
]) {
sh 'updatecli version'
sh updatecliCommand
} // withCredentials
} // if (runUpdateCli)
} // stage
} // node
} // podTemplate

node (finalConfig.updatecliAgentLabel) {
final String updatecliRunStage = "Run updatecli: ${finalConfig.action}"
boolean runUpdatecli = true
stage("Check if updatecli folder exists: ${finalConfig.action}") {
checkout scm
if (!fileExists('updatecli/')) {
echo 'WARNING: no updatecli folder.'
runUpdatecli = false
org.jenkinsci.plugins.pipeline.modeldefinition.Utils.markStageSkippedForConditional(updatecliRunStage)
}
}
stage(updatecliRunStage) {
if (runUpdatecli) {
withCredentials([
usernamePassword(
credentialsId: finalConfig.credentialsId,
usernameVariable: 'USERNAME_VALUE', // Setting this variable is mandatory, even if of not used when the credentials is a githubApp one
passwordVariable: 'UPDATECLI_GITHUB_TOKEN'
)
]) {
sh 'updatecli version'
sh updatecliCommand
} // withCredentials
} // if (runUpdateCli)
} // stage
}
}
3 changes: 1 addition & 2 deletions vars/updatecli.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
<li>String action: (Optional - Default: "diff") Updatecli action (e.g. subcommand) to execute.</li>
<li>String config: (Optional - Default: "./updatecli/updatecli.d") path to the file or directory with the updatecli configuration (flag "--config").</li>
<li>String values: (Optional - Default: "./updatecli/values.yaml") path to the file with the updatecli values (flag "--values").</li>
<li>String updatecliDockerImage: (Optional - Default: "jenkinsciinfra/helmfile:3.0.59") Docker Image of updatecli to be used in the process.</li>
<li>String updatecliAgentLabel: (Optional - Default: "jnlp-linux-arm64") agent to be used in the process.</li>
<li>String cronTriggerExpression: (Optional - Default: "") Enable periodic execution by providing a cron-like expression.</li>
<li>String containerMemory: (Optional - Default: "512Mi") specify the amount of memory dedicated to the updatecli container.</li>
<li>String credentialsId: (Optional - Default: "github-app-updatecli-on-jenkins-infra") specify the githubApp or usernamePassword credentials id to use to get an Access Token. The corresponding populated env vars are USERNAME_VALUE & UPDATECLI_GITHUB_TOKEN</li>
</ul>

Expand Down

0 comments on commit 6e1dc28

Please sign in to comment.