Skip to content

Commit

Permalink
Introduce tar/untar steps including gzip compression.
Browse files Browse the repository at this point in the history
  • Loading branch information
falkena committed Nov 4, 2021
1 parent 977b73f commit 645705a
Show file tree
Hide file tree
Showing 35 changed files with 1,885 additions and 263 deletions.
4 changes: 4 additions & 0 deletions docs/STEPS.md
Expand Up @@ -10,6 +10,10 @@
* `verifySha256` - Verifies the SHA-256 of a given file. ([help](../src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/fs/FileVerifySha256Step/help.html))
* `tee` - Tee output to file

### Tar/tar.gz/tgz Files
* `tar` - Create Tar file. ([help](../src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/tar/TarStep/help.html))
* `untar` - Extract Tar file ([help](../src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/tar/UnTarStep/help.html))

### Zip Files
* `zip` - Create Zip file. ([help](../src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/zip/ZipStep/help.html))
* `unzip` - Extract/Read Zip file ([help](../src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStep/help.html))
Expand Down
@@ -0,0 +1,117 @@
package org.jenkinsci.plugins.pipeline.utility.steps;

import org.kohsuke.stapler.DataBoundSetter;

public abstract class AbstractFileCompressStep extends AbstractFileStep {
private String dir;
private String glob;
private String exclude;
private boolean archive = false;
private boolean overwrite = false;

/**
* The relative path of the base directory to create the archive from.
* Leave empty to create from the current working directory.
*
* @return the dir
*/
public String getDir() {
return dir;
}

/**
* The relative path of the base directory to create the archive from.
* Leave empty to create from the current working directory.
*
* @param dir the dir
*/
@DataBoundSetter
public void setDir(String dir) {
this.dir = dir;
}

/**
* <a href="https://ant.apache.org/manual/dirtasks.html#patterns" target="_blank">Ant style pattern</a>
* of files to include in the archive.
* Leave empty to include all files and directories.
*
* @return the include pattern
*/
public String getGlob() {
return glob;
}

/**
* <a href="https://ant.apache.org/manual/dirtasks.html#patterns" target="_blank">Ant style pattern</a>
* of files to include in the archive.
* Leave empty to include all files and directories.
*
* @param glob the include pattern
*/
@DataBoundSetter
public void setGlob(String glob) {
this.glob = glob;
}

/**
* <a href="https://ant.apache.org/manual/dirtasks.html#patterns" target="_blank">Ant style pattern</a>
* of files to exclude from the archive.
*
* @return the exclude pattern
*/
public String getExclude() {
return exclude;
}

/**
* <a href="https://ant.apache.org/manual/dirtasks.html#patterns" target="_blank">Ant style pattern</a>
* of files to exclude in the archive.
*
* @param exclude the exclude pattern
*/
@DataBoundSetter
public void setExclude(String exclude) {
this.exclude = exclude;
}

/**
* If the archive file should be archived as an artifact of the current build.
* The file will still be kept in the workspace after archiving.
*
* @return if it should be archived or not
*/
public boolean isArchive() {
return archive;
}

/**
* If the archive file should be archived as an artifact of the current build.
* The file will still be kept in the workspace after archiving.
*
* @param archive if it should be archived or not
*/
@DataBoundSetter
public void setArchive(boolean archive) {
this.archive = archive;
}

/**
* If the archive file should be overwritten in case of already existing a file with the same name.
*
* @return if the file should be overwritten or not in case of existing.
*/
public boolean isOverwrite() {
return overwrite;
}

/**
* If the archive file should be overwritten in case of already existing a file with the same name.
*
* @param overwrite if the file should be overwritten or not in case of existing.
*/
@DataBoundSetter
public void setOverwrite(boolean overwrite) {
this.overwrite = overwrite;
}

}
@@ -0,0 +1,99 @@
package org.jenkinsci.plugins.pipeline.utility.steps;

import org.kohsuke.stapler.DataBoundSetter;

public abstract class AbstractFileDeCompressStep extends AbstractFileStep {
private String dir;
private String glob;
private boolean test = false;
private boolean quiet = false;

/**
* The relative path of the base directory to create the archive from.
* Leave empty to create from the current working directory.
*
* @return the dir
*/
public String getDir() {
return dir;
}

/**
* The relative path of the base directory to create the archive from.
* Leave empty to create from the current working directory.
*
* @param dir the dir
*/
@DataBoundSetter
public void setDir(String dir) {
this.dir = dir;
}

/**
* <a href="https://ant.apache.org/manual/dirtasks.html#patterns" target="_blank">Ant style pattern</a>
* of files to extract from the archive.
* Leave empty to include all files and directories.
*
* @return the include pattern
*/
public String getGlob() {
return glob;
}

/**
* <a href="https://ant.apache.org/manual/dirtasks.html#patterns" target="_blank">Ant style pattern</a>
* of files to extract from the archive.
* Leave empty to include all files and directories.
*
* @param glob the include pattern
*/
@DataBoundSetter
public void setGlob(String glob) {
this.glob = glob;
}

/**
* Test the integrity of the archive instead of extracting it.
* When this parameter is enabled, all other parameters <em>(except for {@link #getFile()})</em> will be ignored.
* The step will return <code>true</code> or <code>false</code> depending on the result
* instead of throwing an exception.
*
* @return if the archive should just be tested or not
*/
public boolean isTest() {
return test;
}

/**
* Test the integrity of the archive instead of extracting it.
* When this parameter is enabled, all other parameters <em>(except for {@link #getFile()})</em> will be ignored.
* The step will return <code>true</code> or <code>false</code> depending on the result
* instead of throwing an exception.
*
* @param test if the archive should just be tested or not
*/
@DataBoundSetter
public void setTest(boolean test) {
this.test = test;
}

/**
* Suppress the verbose output that logs every single file that is dealt with.
*
* @return if verbose logging should be suppressed
*/
public boolean isQuiet() {
return quiet;
}

/**
* Suppress the verbose output that logs every single file that is dealt with.
*
* @param quiet if verbose logging should be suppressed
*/
@DataBoundSetter
public void setQuiet(boolean quiet) {
this.quiet = quiet;
}

}
@@ -1,31 +1,10 @@
package org.jenkinsci.plugins.pipeline.utility.steps;

import org.jenkinsci.plugins.workflow.steps.Step;
import org.kohsuke.stapler.DataBoundSetter;

public abstract class AbstractFileOrTextStep extends Step {
protected String file;
public abstract class AbstractFileOrTextStep extends AbstractFileStep {
protected String text;

/**
* The path to a file in the workspace to read from.
*
* @return the path
*/
public String getFile() {
return file;
}

/**
* The path to a file in the workspace to read from.
*
* @param file the path
*/
@DataBoundSetter
public void setFile(String file) {
this.file = file;
}

/**
* A String containing the formatted data.
*
Expand Down
@@ -0,0 +1,28 @@
package org.jenkinsci.plugins.pipeline.utility.steps;

import org.jenkinsci.plugins.workflow.steps.Step;
import org.kohsuke.stapler.DataBoundSetter;

public abstract class AbstractFileStep extends Step {
private String file;

/**
* The path to a file in the workspace to read from.
*
* @return the path
*/
public String getFile() {
return file;
}

/**
* The path to a file in the workspace to read from.
*
* @param file the path
*/
@DataBoundSetter
public void setFile(String file) {
this.file = file;
}

}
@@ -0,0 +1,104 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2021 Alexander Falkenstern
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.jenkinsci.plugins.pipeline.utility.steps.tar;

import com.google.common.collect.ImmutableSet;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.Descriptor;
import hudson.model.TaskListener;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.pipeline.utility.steps.AbstractFileCompressStep;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import java.util.Set;

/**
* Creates a tar file.
*
* @author Alexander Falkenstern &lt;Alexander.Falkenstern@gmail.com&gt;.
*/
public class TarStep extends AbstractFileCompressStep {
private boolean compress = true;

@DataBoundConstructor
public TarStep(String file) throws Descriptor.FormException {
if (StringUtils.isBlank(file)) {
throw new Descriptor.FormException("Can not be empty", "file");
}
setFile(file);
}

/**
* If the tar file should be compressed with gzip.
*
* @return if tar should be compressed with gzip
*/
public boolean isCompress() {
return compress;
}

/**
* If the tar file should be compressed with gzip.
*
* @param compress if it should be compressed with gz or not
*/
@DataBoundSetter
public void setCompress(boolean compress) {
this.compress = compress;
}


@Override
public StepExecution start(StepContext context) throws Exception {
return new TarStepExecution(this, context);
}

@Extension
public static class DescriptorImpl extends StepDescriptor {

public DescriptorImpl() {

}

@Override
public Set<? extends Class<?>> getRequiredContext() {
return ImmutableSet.of(TaskListener.class, FilePath.class);
}

@Override
public String getFunctionName() {
return "tar";
}

@Override
public String getDisplayName() {
return "Create Tar file";
}
}
}

0 comments on commit 645705a

Please sign in to comment.