Skip to content

Commit

Permalink
Merge pull request #598 from zgyorffi/support-dockerfile-agent-declar…
Browse files Browse the repository at this point in the history
…ation-with-filename

Support dockerfile agent declaration with filename
  • Loading branch information
nre-ableton committed Apr 17, 2023
2 parents 33593e1 + 2b5d65c commit 9ab5768
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lesfurets.jenkins.unit.declarative

import com.lesfurets.jenkins.unit.declarative.agent.DockerAgentDeclaration
import com.lesfurets.jenkins.unit.declarative.agent.DockerfileAgentDeclaration
import com.lesfurets.jenkins.unit.declarative.agent.KubernetesAgentDeclaration
import groovy.transform.ToString

Expand All @@ -9,27 +10,12 @@ import static groovy.lang.Closure.DELEGATE_FIRST
@ToString(includePackage = false, includeNames = true, ignoreNulls = true)
class AgentDeclaration extends GenericPipelineDeclaration {

String additionalBuildArgs = null
String args = null
String label
DockerAgentDeclaration docker
KubernetesAgentDeclaration kubernetes
Boolean dockerfile = null
String dockerfileDir
String filename = null
Boolean reuseNode = null
DockerfileAgentDeclaration dockerfileAgent
String customWorkspace
def binding = null
String registryCredentialsId = null
String registryUrl = null

def additionalBuildArgs(String additionalBuildArgs) {
this.additionalBuildArgs = additionalBuildArgs
}

def args(String args) {
this.args = args
}

def label(String label) {
this.label = label
Expand All @@ -43,10 +29,6 @@ class AgentDeclaration extends GenericPipelineDeclaration {
this.customWorkspace = workspace
}

def reuseNode(boolean reuse) {
this.reuseNode = reuse
}

def docker(String image) {
this.docker = new DockerAgentDeclaration().with { it.image = image; it }
}
Expand All @@ -67,21 +49,18 @@ class AgentDeclaration extends GenericPipelineDeclaration {
this.@kubernetes = createComponent(KubernetesAgentDeclaration, closure)
}

def dockerfile(boolean dockerfile) {
this.dockerfile = dockerfile
def dockerfile(boolean _) {
dockerfile([:])
}

def dockerfile(@DelegatesTo(AgentDeclaration) Closure closure) {
closure.call()
def dockerfile(Object dockerfile) {
this.@dockerfileAgent = dockerfile as DockerfileAgentDeclaration
}

def dir(String dir) {
this.dockerfileDir = dir
def dockerfile(@DelegatesTo(strategy = DELEGATE_FIRST, value = DockerfileAgentDeclaration) Closure closure) {
this.@dockerfileAgent = createComponent(DockerfileAgentDeclaration, closure)
}

def filename(String filename) {
this.filename = filename
}

def getCurrentBuild() {
return binding?.currentBuild
Expand All @@ -95,14 +74,6 @@ class AgentDeclaration extends GenericPipelineDeclaration {
return binding?.params
}

def registryCredentialsId(String registryCredentialsId) {
this.registryCredentialsId = registryCredentialsId
}

def registryUrl(String registryUrl) {
this.registryUrl = registryUrl
}

def execute(Object delegate) {
def agentDesc = null

Expand All @@ -112,11 +83,8 @@ class AgentDeclaration extends GenericPipelineDeclaration {
else if (docker) {
agentDesc = '[docker:' + docker.toString() + ']'
}
else if (dockerfile) {
agentDesc = '[dockerfile:' + dockerfile.toString() + ']'
}
else if (dockerfileDir && dockerfileDir.exists()) {
agentDesc = '[dockerfileDir:' + dockerfileDir.toString() + ']'
else if (dockerfileAgent) {
agentDesc = '[dockerfile:' + dockerfileAgent.toString() + ']'
}
else if (kubernetes) {
agentDesc = '[kubernetes:' + kubernetes.toString() + ']'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.lesfurets.jenkins.unit.declarative.agent


import com.lesfurets.jenkins.unit.declarative.GenericPipelineDeclaration
import groovy.transform.ToString

@ToString(includePackage = false, includeNames = true, ignoreNulls = true)
class DockerfileAgentDeclaration extends GenericPipelineDeclaration {
String additionalBuildArgs
String args
String customWorkspace
String dockerfileDir
String filename
String label
String registryCredentialsId
String registryUrl
Boolean reuseNode

def additionalBuildArgs(String additionalBuildArgs) {
this.additionalBuildArgs = additionalBuildArgs
}

def args(String args) {
this.args = args
}

def customWorkspace(final String customWorkspace) {
this.customWorkspace = customWorkspace
}

def dir(String dir) {
this.dockerfileDir = dir
}

def filename(String filename) {
this.filename = filename
}

def label(final String label) {
this.label = label
}

def registryCredentialsId(final String registryCredentialsId) {
this.registryCredentialsId = registryCredentialsId
}

def registryUrl(final String registryUrl) {
this.registryUrl = registryUrl
}

def reuseNode(boolean reuse) {
this.reuseNode = reuse
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,23 @@ class TestDockerAgentInStep extends DeclarativePipelineTest {
}

@Test
void test_dockerfile_agent_callstack_does_not_contain_binding() {
void test_dockerfile_agent_only_filename_specified() {
runScript("Dockerfile_Agent_Only_Filename_JenkinsFile")
assertCallStackContains('Executing on agent [dockerfile')
assertJobStatusSuccess()
}

@Test void test_dockerfile_default_agent() throws Exception {
runScript('Dockerfile_Agent_Default_Jenkinsfile')
assertCallStackContains('Executing on agent [dockerfile')
assertJobStatusSuccess()
}

@Test
void test_docker_agent_callstack_does_not_contain_binding() {
runScript("Docker_agentInStep_JenkinsFile")
assertJobStatusSuccess()
assertCallStack().doesNotContain('binding:groovy.lang.Binding@')
assertCallStack().contains('Docker_agentInStep_JenkinsFile.echo(Executing on agent [docker:[image:maven, reuseNode:false, stages:[:], args:, alwaysPull:true, containerPerStageRoot:false, label:latest]])')
assertCallStackContains('Docker_agentInStep_JenkinsFile.echo(Executing on agent [docker:[image:maven, reuseNode:false, stages:[:], args:, alwaysPull:true, containerPerStageRoot:false, label:latest]])')
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pipeline {
stages {
stage('Build') {
agent {
dockerfile true
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pipeline {
stages {
stage('Build') {
agent {
dockerfile {
filename 'Dockerfile'
}
}
}
}
}

0 comments on commit 9ab5768

Please sign in to comment.