如何在Jenkins中仅构建Git变更文件并通过FTP发送变更文件?
Hey there! Let's tackle your two Jenkins optimization needs one by one—cutting down on unnecessary builds and FTP transfers by only handling files that have actually changed in Git.
1. Only Build Changed Git Files
There are a couple of reliable ways to achieve this, depending on your project's build setup:
Option 1: Use Git Diff in a Shell Script Step
You can leverage Git's built-in diff functionality to identify changed files between the current commit and the last successful build, then pass those files to your build command.
- First, get the hash of the last successful build's commit. Jenkins provides an environment variable for this:
GIT_PREVIOUS_SUCCESSFUL_COMMIT(note: this only works if you've had at least one successful build before). - Add a Execute shell (or Execute Windows batch command) step to your Jenkins job with something like this:
# Get list of changed files CHANGED_FILES=$(git diff --name-only $GIT_PREVIOUS_SUCCESSFUL_COMMIT $GIT_COMMIT) # If there are changed files, run build command only on those if [ ! -z "$CHANGED_FILES" ]; then # Replace this with your actual build command (e.g., javac, npm run build for specific files) for file in $CHANGED_FILES; do echo "Building $file..." # Your build logic for individual files goes here done fi - Adjust the build logic to match your project (e.g., if you're using a build tool like Maven, you might need to map changed source files to their corresponding modules).
Option 2: Use the Git Changed Files Plugin
This plugin simplifies fetching changed files without writing custom scripts:
- Install the Git Changed Files Plugin from Jenkins' Plugin Manager.
- In your job configuration, after the Git checkout step, the plugin will automatically populate variables like
${gitChangedFiles.files}(a space-separated list of changed files) and${gitChangedFiles.filesArray}(a list for script loops). - Use these variables in your build steps to target only changed files. For example, in a shell step:
for file in ${gitChangedFiles.filesArray}; do echo "Processing changed file: $file" # Build logic here done
2. Only FTP Changed Files via "Published over FTP" Plugin
The default behavior of this plugin is to transfer all specified files, so we need to pair it with the changed files list we generated above:
Option 1: Copy Changed Files to a Temporary Directory First
This is a straightforward approach that ensures only changed files get transferred:
- Add a shell step after building to copy all changed files to a dedicated temporary directory:
# Create temp dir if it doesn't exist mkdir -p /tmp/jenkins-changed-files # Copy changed files to temp dir (preserving directory structure) for file in $CHANGED_FILES; do mkdir -p /tmp/jenkins-changed-files/$(dirname $file) cp $file /tmp/jenkins-changed-files/$file done - In your Published over FTP configuration:
- Set "Source files" to
/tmp/jenkins-changed-files/** - Set "Remove prefix" to
/tmp/jenkins-changed-filesso the directory structure matches your project on the FTP server.
- Set "Source files" to
Option 2: Use Changed Files List Directly in FTP Plugin
If you prefer not to use a temp directory, you can pass the changed files list directly to the plugin:
- In the "Files to transfer" field of the FTP step, use the variable from the Git Changed Files Plugin (or your custom
CHANGED_FILESvariable). Note: You may need to format the list correctly (e.g., separate files with newlines or commas, depending on the plugin's expected format). - For example, if using the plugin's variable:
${gitChangedFiles.files}(test this to ensure the plugin recognizes the list correctly).
Handling Deleted Files (Optional)
If you want to mirror deletions from Git to your FTP server:
- Use
git diff --name-status $GIT_PREVIOUS_SUCCESSFUL_COMMIT $GIT_COMMITto get file statuses (D for deleted). - Add a shell step to generate a list of deleted files, then use the FTP plugin's "Delete remote files" option (or a separate FTP command via shell) to remove those files from the server.
内容的提问来源于stack exchange,提问作者CHUL LIM

