You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jenkins中仅构建Git变更文件并通过FTP发送变更文件?

Optimizing Jenkins Workflow: Build & FTP Only Changed Git Files

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:

  1. Install the Git Changed Files Plugin from Jenkins' Plugin Manager.
  2. 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).
  3. 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:

  1. 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
    
  2. In your Published over FTP configuration:
    • Set "Source files" to /tmp/jenkins-changed-files/**
    • Set "Remove prefix" to /tmp/jenkins-changed-files so the directory structure matches your project on the FTP server.

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_FILES variable). 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:

  1. Use git diff --name-status $GIT_PREVIOUS_SUCCESSFUL_COMMIT $GIT_COMMIT to get file statuses (D for deleted).
  2. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:25:18