如何在Mercurial仓库下替代git-ftp实现Bitbucket Pipelines FTP增量部署?
Great question! Since you're working with a Mercurial repo instead of Git, git-ftp won't work—but there are solid alternatives that let you deploy only changed files via FTP through Bitbucket Pipelines. Here are two reliable options tailored to your needs:
Option 1: hg-ftp (Direct Git-FTP Equivalent for Mercurial)
hg-ftp is a purpose-built Mercurial extension that mimics the core functionality of git-ftp: it tracks the last synced revision and only uploads files that have changed since then. It's the closest drop-in replacement you'll find.
Updated Bitbucket Pipeline Config
Replace your original YAML with this, which sets up hg-ftp in your pipeline:
image: python:3.9-slim # Uses a Python-based image to easily install Mercurial and hg-ftp pipelines: default: - step: script: # Install system dependencies - apt-get update && apt-get install -y mercurial --no-install-recommends # Install the hg-ftp extension via pip - pip install hg-ftp # Configure Mercurial to enable the hg-ftp extension - echo "[extensions]" > ~/.hgrc - echo "hgftp =" >> ~/.hgrc # Push only changed files to your FTP server - hg ftp push --user $FTP_USERNAME --password $FTP_PASSWORD ftp://YOUR_SERVER_ADDRESS/PATH_TO_WEBSITE/
Quick Tips:
- Store
FTP_USERNAMEandFTP_PASSWORDas repository variables in Bitbucket (under Repository Settings > Repository Variables) to keep your credentials secure—never hardcode them! hg-ftpautomatically keeps track of the last deployed revision in your pipeline environment, so you don't need to manually manage state between runs.
Option 2: lftp with Mercurial Change Detection
If you prefer a more flexible, script-driven approach, you can use lftp (a powerful command-line FTP client) paired with Mercurial's log to identify and upload only changed files.
Pipeline Config for This Approach
image: debian:stable-slim pipelines: default: - step: script: # Install required tools - apt-get update && apt-get install -y mercurial lftp --no-install-recommends # Get a list of files changed in the latest commit - CHANGED_FILES=$(hg log -l 1 --template "{files}\n" | tr ' ' '\n') # Upload each changed file to the FTP server - | for file in $CHANGED_FILES; do # Only upload if the file exists (avoids deleted files) if [ -f "$file" ]; then lftp -u $FTP_USERNAME,$FTP_PASSWORD ftp://YOUR_SERVER_ADDRESS << EOF put -O /PATH_TO_WEBSITE/ "$file" quit EOF fi done
Notes:
- This script targets files changed in the most recent commit. If you need to deploy changes across multiple commits, you can extend it to track the last deployed revision (e.g., store the revision hash in a file on your FTP server or a Bitbucket variable) and compare it to the current repo state.
lftpsupports more advanced features like resume on broken transfers, which can be handy for larger files.
Final Recommendations
- Start with
hg-ftpif you want simplicity and a direct git-ftp replacement—it's the least effort to set up. - Use the
lftpapproach if you need custom logic (like handling deleted files or complex path mappings). - Always test your pipeline on a staging server first to ensure only the intended files are being uploaded!
内容的提问来源于stack exchange,提问作者solid_luffy

