SFTP Deployment Workflow GitHub
January 25, 2025 · AI Conversations
on:
push:
branches:
– master # Trigger on push to master branch
jobs:
deploy:
runs-on: ubuntu-latest
steps:
– name: Checkout code
uses: actions/checkout@v2
– name: Set up SSH
env:
PEM_FILE: ${{ secrets.PEM_FILE }} # Store your PEM file content in GitHub secrets
run: |
echo "$PEM_FILE" > key.pem
chmod 600 key.pem
– name: Deploy to EC2
env:
EC2_USER: ${{ secrets.EC2_USER }} # Store your EC2 username in GitHub secrets
EC2_HOST: ${{ secrets.EC2_HOST }} # Store your EC2 host in GitHub secrets
run: |
rsync -avz -e "ssh -i key.pem -o StrictHostKeyChecking=no" ./ $EC2_USER@$EC2_HOST:/var/www/share
rm key.pem # Clean up the PEM file after deployment
create above for other sftp not aws ec2
name: Deploy to SFTP
on:
push:
branches:
- master # Trigger on push to master branch
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install lftp
run: |
sudo apt-get update
sudo apt-get install -y lftp
- name: Deploy to SFTP
env:
SFTP_USER: ${{ secrets.SFTP_USER }} # Store your SFTP username in GitHub secrets
SFTP_HOST: ${{ secrets.SFTP_HOST }} # Store your SFTP host in GitHub secrets
SFTP_PASSWORD: ${{ secrets.SFTP_PASSWORD }} # Store your SFTP password in GitHub secrets
run: |
lftp -u $SFTP_USER,$SFTP_PASSWORD $SFTP_HOST <<EOF
set ftp:ssl-allow no
mirror --reverse --delete --verbose ./ /path/on/sftp
bye
EOF
### Key Changes:
1. **Install lftp**: The workflow installs `lftp`, which is a powerful FTP client that supports SFTP.
2. **Deployment Step**: The `lftp` command is used to connect to the SFTP server and mirror the local files to the specified directory on the SFTP server.
– Replace `/path/on/sftp` with the desired remote path on your SFTP server.
3. **Secrets Management**: Ensure that you store your SFTP credentials (`SFTP_USER`, `SFTP_HOST`, and `SFTP_PASSWORD`) in GitHub secrets for secure access.
Make sure to customize the paths and any other options as needed for your specific deployment requirements.
1. **`scp` (Secure Copy Protocol)**: You can use `scp` if you just need to copy files over SFTP. Here's an example:
name: Deploy to SFTP
on:
push:
branches:
- master
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Deploy to SFTP
env:
SFTP_USER: ${{ secrets.SFTP_USER }}
SFTP_HOST: ${{ secrets.SFTP_HOST }}
SFTP_PASSWORD: ${{ secrets.SFTP_PASSWORD }}
run: |
scp -o StrictHostKeyChecking=no -r ./ $SFTP_USER@$SFTP_HOST:/path/on/sftp
Note: You'll need to ensure that SSH key authentication is set up, or you can use a password prompt (which is not recommended for security reasons).
2. **`sshpass`**: If you want to use password authentication, you can use `sshpass` to automate entering the password. Here’s how to set it up:
name: Deploy to SFTP
on:
push:
branches:
- master
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install sshpass
run: |
sudo apt-get update
sudo apt-get install -y sshpass
- name: Deploy to SFTP
env:
SFTP_USER: ${{ secrets.SFTP_USER }}
SFTP_HOST: ${{ secrets.SFTP_HOST }}
SFTP_PASSWORD: ${{ secrets.SFTP_PASSWORD }}
run: |
sshpass -p "$SFTP_PASSWORD" scp -o StrictHostKeyChecking=no -r ./ $SFTP_USER@$SFTP_HOST:/path/on/sftp
3. **`rsync` over SSH**: You can also use `rsync` over SSH if you have SSH access to the SFTP server. This can be more efficient for larger deployments since `rsync` only transfers changed files:
name: Deploy to SFTP
on:
push:
branches:
- master
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Deploy to SFTP
env:
SFTP_USER: ${{ secrets.SFTP_USER }}
SFTP_HOST: ${{ secrets.SFTP_HOST }}
SFTP_PASSWORD: ${{ secrets.SFTP_PASSWORD }}
run: |
sshpass -p "$SFTP_PASSWORD" rsync -avz -e "ssh -o StrictHostKeyChecking=no" ./ $SFTP_USER@$SFTP_HOST:/path/on/sftp
### Summary
Using `lftp`, `scp`, `sshpass`, or `rsync` depends on your specific needs and the configuration of your SFTP server. Each method has its own advantages, so choose the one that fits your deployment strategy best.