com.atlassian.bamboo.specs.builders.task.DockerBuildImageTask Maven / Gradle / Ivy
package com.atlassian.bamboo.specs.builders.task;
import com.atlassian.bamboo.specs.model.task.docker.DockerBuildImageTaskProperties;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.nio.file.Path;
import static com.atlassian.bamboo.specs.util.FileUtils.readFileContent;
/**
* Task to build docker image.
*/
public class DockerBuildImageTask extends AbstractDockerTask {
@NotNull
private String imageName;
@Nullable
private String dockerfile;
@NotNull
private DockerBuildImageTaskProperties.DockerfileContent dockerfileContent = DockerBuildImageTaskProperties.DockerfileContent.INLINE;
private boolean useCache = true;
private boolean saveAsFile;
@Nullable
private String imageFilename;
/**
* Sets the image name. You can also optionally specify repository, namespace and a tag. E.g.
* registry.address:port/namespace/repository:tag
*
* This field is mandatory.
*
*/
public DockerBuildImageTask imageName(@NotNull String imageName) {
this.imageName = imageName;
return this;
}
/**
* Task will use Dockerfile which should be available in a working directory.
*/
public DockerBuildImageTask dockerfileInWorkingDir() {
this.dockerfileContent = DockerBuildImageTaskProperties.DockerfileContent.WORKING_DIR;
this.dockerfile = null;
return this;
}
/**
* Specifies content of a Dockerfile.
*
* Specifying content of a Dockerfile is mandatory, unless you use {@link #dockerfileInWorkingDir()}
*
*/
public DockerBuildImageTask dockerfile(String dockerfile) {
this.dockerfile = dockerfile;
this.dockerfileContent = DockerBuildImageTaskProperties.DockerfileContent.INLINE;
return this;
}
/**
* Specifies content of a Dockerfile with content of a file. File must be available on the path when running Bamboo Specs.
*
* Specifying content of a Dockerfile is mandatory, unless you use {@link #dockerfileInWorkingDir()}
*
*/
public DockerBuildImageTask dockerfileFromPath(@NotNull final Path path) {
return dockerfile(readFileContent(path, "Dockerfile path", "Error when reading Dockerfile from path: %s"));
}
/**
* Specifies if cache should be used. Defaults to true.
*/
public DockerBuildImageTask useCache(boolean useCache) {
this.useCache = useCache;
return this;
}
/**
* If set to true Docker image will be saved to file.
*
* If sets to true {@link #imageFilename} must be specified.
*
*/
public DockerBuildImageTask saveAsFile(boolean saveAsFile) {
this.saveAsFile = saveAsFile;
return this;
}
/**
* Specifies name of the file to which image will be saved.
*/
public DockerBuildImageTask imageFilename(String imageFilename) {
this.imageFilename = imageFilename;
return this;
}
@NotNull
@Override
protected DockerBuildImageTaskProperties build() {
return new DockerBuildImageTaskProperties(description,
taskEnabled,
imageName,
dockerfileContent,
dockerfile,
useCache,
saveAsFile,
imageFilename,
environmentVariables,
workingSubdirectory);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy