πŸ’ΈSave up to $132K/month in CI costs!πŸ‘‰ Try Free✨
Skip to main content
← Back to workflows

Using SCP for GitHub Actions to Transfer Files via SSH

appleboy-scp-action -
GitHub Action
v0.1.7
1,218
Contributors
Contributor - appleboyContributor - ilyabrin
Categories

Optimize Your CI/CD Pipeline

Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.

45% Faster Builds
60% Cost Reduction
Usage
name: 'Usage of scp-action GitHub Action'
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: copy file via ssh password
uses: appleboy/scp-[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
port: ${{ secrets.PORT }}
source: "tests/a.txt,tests/b.txt"
target: your_server_target_folder_path

scp-action logo

SCP Action

GitHub Action that copy files and artifacts via SSH.


What is SCP Action?​

Deploying or transferring files securely between our CI environment and remote servers is essential for operations such as deploying to staging or production environments. Using Secure Copy Protocol (SCP) within GitHub Actions can streamline these processes by automating the secure transfer of files via SSH. Here’s how to set up SCP for GitHub Actions, enabling file transfers on Linux-based Docker containers.

How to Copy Files via SCP with SSH Password Authentication​

Transferring files securely between our CI systems and remote servers is essential for many of our operations, including deployments and backups. Using SCP (Secure Copy Protocol) within GitHub Actions provides a robust solution. This guide details how to configure file transfers using both SSH password and SSH key authentication methods.

- name: copy file via ssh password
uses: appleboy/scp-[email protected]
with:
host: example.com
username: foo
password: bar
port: 22
source: "tests/a.txt,tests/b.txt"
target: your_server_target_folder_path

How to Copy Files via SCP with SSH Key Authentication​

For enhanced security, use SSH key authentication. This method is more secure and recommended for sensitive or production environments.

- name: Copy file via SSH key
uses: appleboy/scp-[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
port: ${{ secrets.PORT }}
key: ${{ secrets.KEY }}
source: "tests/a.txt,tests/b.txt"
target: your_server_target_folder_path

How to Exclude Specific Files When Using SCP​

Sometimes, you might want to exclude specific files from being transferred. You can specify an ignore list directly in the source parameter.

- name: Copy file via SSH key
uses: appleboy/scp-[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
port: ${{ secrets.PORT }}
key: ${{ secrets.KEY }}
source: "tests/*.txt,!tests/a.txt"
target: your_server_target_folder_path

How to Setup the GitHub Actions Workflow for Artifact Deployment​

Securely transferring files between our CI systems and remote servers is essential for many of our operations, including deployments and backups. Using SCP (Secure Copy Protocol) within GitHub Actions provides a robust solution. This guide details how to configure file transfers using both SSH password and SSH key authentication methods. Ensuring efficient and secure deployment of artifacts to remote servers is crucial in our CI/CD pipelines. Using GitHub Actions, we can automate the process from artifact creation to deployment, including secure file transfer via SCP. This guide provides a step-by-step approach to setting up this workflow.

deploy:
name: deploy artifact
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v4

- run: echo hello > world.txt

- uses: actions/upload-artifact@v4
with:
name: my-artifact
path: world.txt

- uses: actions/download-artifact@v4
with:
name: my-artifact
path: distfiles

- name: copy file to server
uses: appleboy/scp-[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.KEY }}
port: ${{ secrets.PORT }}
source: distfiles/*
target: your_server_target_folder_path

Configure Path Adjustments for Transfers​

This configuration is essential when the local directory structure does not match the structure required on the remote server.

- name: Adjust Path Structure for Deployment
uses: appleboy/scp-[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.KEY }}
port: ${{ secrets.PORT }}
source: "tests/a.txt,tests/b.txt"
target: your_server_target_folder_path
strip_components: 1

How to Copy Only Newer Files to a Remote Server​

Efficiently managing file transfers to a remote server is crucial in our development process, especially with frequent changes across multiple files. We can optimize our deployment process by ensuring only changed or updated files are copied, reducing transfer times and network load. Here’s a guide on setting up a GitHub Actions workflow that identifies changed files and transfers only those files to a remote server if they are newer than the files already present.

 changes:
name: test changed-files
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v4

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v35
with:
since_last_remote_commit: true
separator: ","

- name: copy file to server
uses: appleboy/scp-[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.KEY }}
port: ${{ secrets.PORT }}
source: ${{ steps.changed-files.outputs.all_changed_files }}
target: your_server_target_folder_path