This plugin's main goal is to provide cross platform file operations as Build step. It also provides the same functionality as Pipeline step.
No need to search for *Nix or Win commands to do file operations.
For more details about about each step please refer to: File Operations Steps
This section provides examples of how to use the File Operations Plugin in Jenkins pipelines. These examples demonstrate the most common file operations, which you can easily incorporate into your own pipeline scripts.
To use these examples in your Jenkins pipeline, follow these steps:
SetUp Your Jenkins Environment
: Ensure that Jenkins is set up and that the File Operations Plugin is installed.
Create or Edit a Jenkins Pipeline Job
:
Add the Pipeline Script
: Copy and paste the relevant example script into the pipeline script area of your job configuration.
Run the Pipeline
: Save the job configuration and run the pipeline to see the file operations in action.
This operation copies files from one location to another. It is useful for scenarios where you need to move or replicate files as part of your build process.
pipeline {
agent any
stages {
stage('Copy Files') {
steps {
fileOperations([
fileCopyOperation(sourceFiles: 'src/*.txt', targetLocation: 'dest/')
])
}
}
}
}
Explanation:
sourceFiles
:'src/*.txt': Specifies the files to copy from the source directory. In this case, all .txt
files in the src
directory.
targetLocation
:'dest/': Defines the destination directory where the files will be copied to.
This operation deletes files based on a specified pattern. It is useful for cleaning up old or unnecessary files during the build process.
pipeline {
agent any
stages {
stage('Delete Files') {
steps {
fileOperations([
fileDeleteOperation(includeFilePattern: '**/old-files/*.log')
])
}
}
}
}
Explanation:
includeFilePattern
: '**/old-files/*.log'
: Specifies the pattern for files to delete. In this case, all .log
files in any old-files
directory.
This operation creates a new file with specified content. It is useful for generating configuration files or other necessary files as part of the build process.
pipeline {
agent any
stages {
stage('Create File') {
steps {
fileOperations([
fileCreateOperation(filePath: 'newfile.txt', fileContent: 'Hello, World!')
])
}
}
}
}
Explanation:
filePath
:'newfile.txt'
: Specifies the path for the new file.
fileContent
: 'Hello, World!'
: Defines the content to be written to the new file.
This operation downloads a file from a specified URL and saves it to a designated location. It is useful for retrieving external resources or dependencies needed for the build.
pipeline {
agent any
stages {
stage('Download File') {
steps {
fileOperations([
fileDownloadOperation(url: 'https://example.com/file.zip', targetLocation: 'downloads/')
])
}
}
}
}
Explanation:
url
: 'https://example.com/file.zip'
: The URL of the file to download.
targetLocation
: 'downloads/'
: The directory where the downloaded file will be saved.
See the changelog
See the Job DSL usage example.