mirror of
https://github.com/Burnett01/rsync-deployments.git
synced 2026-09-21 06:05:24 +08:00
Compare commits
45 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| febcb5ac34 | |||
| 682623851b | |||
| 050c29bcea | |||
| 6093fd5ca1 | |||
| 0186c3a6b2 | |||
| 2f8edbc7f1 | |||
| d573c19867 | |||
| 609b9969e2 | |||
| 1e3e469683 | |||
| c22c1a5555 | |||
| 6b3db75d87 | |||
| c4c3b6821b | |||
| 2f0d5a19fa | |||
| 5536ad8c42 | |||
| e9dd3cdb51 | |||
| 72f04677de | |||
| 1eb5088cc2 | |||
| 24cfa35ecb | |||
| 530c686e9a | |||
| fd809f93e2 | |||
| cd8d80d480 | |||
| 65d16d5e1b | |||
| 7681fdf023 | |||
| 0ce1e18957 | |||
| 7d15fca650 | |||
| ab6e32151b | |||
| fa33e6606d | |||
| 44615a7931 | |||
| 84e60e763c | |||
| a7b38d9b16 | |||
| cdb8f481ea | |||
| afc3d5d8d4 | |||
| e07b616b2e | |||
| 59a1203852 | |||
| f8b88bb4fa | |||
| 7d8c19d72a | |||
| 0370813668 | |||
| 8ad6165315 | |||
| c36b09ff5d | |||
| 2026161543 | |||
| 9abeae5926 | |||
| 65ac047ad6 | |||
| 980b08d70e | |||
| f8860c9bce | |||
| dc7ac312a6 |
+9
-13
@@ -1,16 +1,12 @@
|
||||
FROM alpine:3.20.0
|
||||
MAINTAINER Dr Internet <internet@limelightgaming.net>
|
||||
FROM ubuntu:latest
|
||||
|
||||
# Install RSync and Open SSH.
|
||||
RUN apk update && apk add --no-cache rsync openssh-client
|
||||
RUN rm -rf /var/cache/apk/*
|
||||
# Update
|
||||
RUN apt-get update
|
||||
|
||||
# Prepare SSH dir.
|
||||
RUN mkdir ~/.ssh
|
||||
# Install packages
|
||||
RUN apt-get -yq install rsync openssh-client
|
||||
|
||||
# Copy in our executables.
|
||||
COPY agent-* hosts-* /bin/
|
||||
RUN chmod +x /bin/agent-* /bin/hosts-*
|
||||
|
||||
# Prepare for known hosts.
|
||||
RUN hosts-clear
|
||||
# Copy entrypoint
|
||||
ADD entrypoint.sh /entrypoint.sh
|
||||
RUN chmod +x /entrypoint.sh
|
||||
ENTRYPOINT ["/entrypoint.sh"]
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020 Joshua Piper
|
||||
Copyright (c) 2019 Contention
|
||||
Copyright (c) 2019 Burnett01
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
@@ -1,93 +1,123 @@
|
||||
# rsync docker image.
|
||||
# rsync deployments
|
||||
|
||||
A simple alpine based docker image for rsync and ssh deployments.
|
||||
|
||||
## Using this image
|
||||
This image has two primary uses. Firstly, as a deployment image for GitLab CI runs. Secondly, as a base image for other images.
|
||||
|
||||
### gitlab-ci.yml
|
||||
```yml
|
||||
image: drinternet/rsync:1.0.1
|
||||
...
|
||||
before_script:
|
||||
- source agent-autostart "$CI_PROJECT_ID-$CI_PIPELINE_ID-$_CI_CONCURRENT_ID"
|
||||
- hosts-add "$SSH_KNOWN_HOSTS"
|
||||
|
||||
after_script:
|
||||
- agent-stop "$CI_PROJECT_ID-$CI_PIPELINE_ID-$_CI_CONCURRENT_ID"
|
||||
```
|
||||
|
||||
### Base image in a `Dockerfile
|
||||
```dockerfile
|
||||
FROM drinternet/rsync:1.0.1
|
||||
COPY some/file or/whatever
|
||||
```
|
||||
|
||||
## Inbuilt commands.
|
||||
|
||||
This base image also includes a few shell scripts, to help with managing SSH agents and known hosts files.
|
||||
### SSH Agent Management
|
||||
#### agent-start
|
||||
This command starts the SSH agent, if it isn't already started (SSH_AGENT_PID set or ssh agent ID file found).
|
||||
It takes one optional argument, for the name of the agent to be started. Defaults to "default".
|
||||
This program needs to be source'd to work correctly.
|
||||
`source agent-start "default"`
|
||||
|
||||
#### agent-stop
|
||||
This command stops the SSH agent, if it is started (SSH_AGENT_PID set or ssh agent ID file found).
|
||||
It takes one optional argument, for the name of the agent to be stopped. Defaults to "default".
|
||||
`agent-stop "my-agent-name"`
|
||||
|
||||
#### agent-add
|
||||
This command adds a key to the currently running SSH agent. The key is taken from stdin, and the agent used is that in SSH_AGENT_PID.
|
||||
|
||||
#### agent-autostart
|
||||
This command starts the SSH agent and loads the private key from the "SSH_PRIVATE_KEY" environment var. The command takes one optional argument, for the name of the agent to be started. Defaults to "default".
|
||||
As with agent-start, this command needs to be sourced.
|
||||
|
||||
#### agent-askpass
|
||||
This command is called by ssh-add when the [SSH_ASKPASS](https://man.openbsd.org/ssh-add.1#ENVIRONMENT) variable is set active. The command returns the SSH_PASS to [ssh-askpass(1)](https://man.openbsd.org/ssh-askpass.1).
|
||||
|
||||
This command is ignored by ssh-add if the key does not require a passphrase.
|
||||
|
||||
### known_hosts management
|
||||
#### hosts-clear
|
||||
This command truncates the known_hosts file and sets its permissions.
|
||||
|
||||
#### hosts-add
|
||||
This command adds an entry to the known hosts file, and ensures its permissions are correct. It takes one argument, which is the new key to add.
|
||||
|
||||
## Tags
|
||||
Both the repository and Docker Hub images follow the [semantic versioning](https://semver.org/) standard.
|
||||
Docker Hub image versions are prefixed with v, and contain the full version, version sub patch number and version sub minor and patch.
|
||||
|
||||
For example, the repository tag 1.2.3, creates the Hub tags v1.2.3, v1.2 and v1, to allow for binding to a specific version, specific minor version or specific major version.
|
||||
Forked from [Contention/rsync-deployments](https://github.com/Contention/rsync-deployments)
|
||||
|
||||
|
||||
## Example gitlab-ci.yml
|
||||
```yml
|
||||
image: drinternet/rsync:1.0.1
|
||||
This GitHub Action deploys files in `GITHUB_WORKSPACE` to a folder on a server via rsync over ssh.
|
||||
|
||||
stages:
|
||||
- deploy
|
||||
Use this action in a build/test workflow which leaves deployable code in `GITHUB_WORKSPACE`.
|
||||
|
||||
before_script:
|
||||
- source agent-autostart "$CI_PROJECT_ID-$CI_PIPELINE_ID-$_CI_CONCURRENT_ID"
|
||||
- hosts-add "$SSH_KNOWN_HOSTS"
|
||||
---
|
||||
|
||||
after_script:
|
||||
- agent-stop "$CI_PROJECT_ID-$CI_PIPELINE_ID-$_CI_CONCURRENT_ID"
|
||||
## Inputs
|
||||
|
||||
deploy:
|
||||
stage: deploy
|
||||
script:
|
||||
- rsync -zrSlhaO --chmod=D2775,F664 --delete-after . $FTP_USER@$FTP_HOST:/var/www/deployment/
|
||||
```
|
||||
- `switches`* - The first is for any initial/required rsync flags, eg: `-avzr --delete`
|
||||
|
||||
## Using with passphrase protected key
|
||||
- `rsh` - Remote shell commands
|
||||
|
||||
You can supply a passphrase with ``SSH_PASS`` to ``agent-add``, ``agent-start`` or ``agent-autostart``.
|
||||
- `path` - The source path. Defaults to GITHUB_WORKSPACE
|
||||
|
||||
- `remote_path`* - The deployment target path
|
||||
|
||||
- `remote_host`* - The remote host
|
||||
|
||||
- `remote_port` - The remote port. Defaults to 22
|
||||
|
||||
- `remote_user`* - The remote user
|
||||
|
||||
- `remote_key`* - The remote ssh key
|
||||
|
||||
``* = Required``
|
||||
|
||||
## Required secret
|
||||
|
||||
This action needs a `DEPLOY_KEY` secret variable. This should be the private key part of a ssh key pair. The public key part should be added to the authorized_keys file on the server that receives the deployment. This should be set in the Github secrets section and then referenced as the `remote_key` input.
|
||||
|
||||
## Example usage
|
||||
|
||||
Simple:
|
||||
|
||||
```
|
||||
SSH_PASS="THE_PASSPHRASE" agent-add
|
||||
name: DEPLOY
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: rsync deployments
|
||||
uses: burnett01/rsync-deployments@2.0
|
||||
with:
|
||||
switches: -avzr --delete
|
||||
path: src/
|
||||
remote_path: /var/www/html/
|
||||
remote_host: example.com
|
||||
remote_user: debian
|
||||
remote_key: ${{ secrets.DEPLOY_KEY }}
|
||||
```
|
||||
|
||||
Advanced:
|
||||
|
||||
```
|
||||
name: DEPLOY
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: rsync deployments
|
||||
uses: burnett01/rsync-deployments@2.0
|
||||
with:
|
||||
switches: -avzr --delete --exclude="" --include="" --filter=""
|
||||
path: src/
|
||||
remote_path: /var/www/html/
|
||||
remote_host: example.com
|
||||
remote_port: 5555
|
||||
remote_user: debian
|
||||
remote_key: ${{ secrets.DEPLOY_KEY }}
|
||||
```
|
||||
|
||||
For better security, I suggest you create additional secrets for remote_host, remote_port and remote_user inputs.
|
||||
|
||||
```
|
||||
name: DEPLOY
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: rsync deployments
|
||||
uses: burnett01/rsync-deployments@2.0
|
||||
with:
|
||||
switches: -avzr --delete
|
||||
path: src/
|
||||
remote_path: /var/www/html/
|
||||
remote_host: ${{ secrets.DEPLOY_HOST }}
|
||||
remote_port: ${{ secrets.DEPLOY_PORT }}
|
||||
remote_user: ${{ secrets.DEPLOY_USER }}
|
||||
remote_key: ${{ secrets.DEPLOY_KEY }}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Version 1.0 (EOL)
|
||||
|
||||
Looking for version 1.0?
|
||||
|
||||
Check here: https://github.com/Burnett01/rsync-deployments/tree/1.0
|
||||
|
||||
Please note that version 1.0 has reached end of life state.
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
name: 'Rsync Deployments Action'
|
||||
description: 'GitHub Action for deploying code via rsync over ssh'
|
||||
author: 'Burnett01'
|
||||
inputs:
|
||||
switches:
|
||||
description: 'The switches'
|
||||
required: true
|
||||
rsh:
|
||||
description: 'The remote shell argument'
|
||||
required: false
|
||||
default: ''
|
||||
path:
|
||||
description: 'The local path'
|
||||
required: false
|
||||
default: ''
|
||||
remote_path:
|
||||
description: 'The remote path'
|
||||
required: true
|
||||
remote_host:
|
||||
description: 'The remote host'
|
||||
required: true
|
||||
remote_port:
|
||||
description: 'The remote port'
|
||||
required: false
|
||||
default: 22
|
||||
remote_user:
|
||||
description: 'The remote user'
|
||||
required: true
|
||||
remote_key:
|
||||
description: 'The remote key'
|
||||
required: true
|
||||
runs:
|
||||
using: 'docker'
|
||||
image: 'Dockerfile'
|
||||
branding:
|
||||
icon: 'send'
|
||||
color: 'gray-dark'
|
||||
@@ -1,4 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
source agent-start "${1:-default}"
|
||||
cat - | tr -d '\r' | DISPLAY=1 SSH_ASKPASS=agent-askpass ssh-add - >/dev/null
|
||||
@@ -1,2 +0,0 @@
|
||||
#!/bin/sh
|
||||
echo "$SSH_PASS"
|
||||
@@ -1,4 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
source agent-start "${1:-default}"
|
||||
echo "$SSH_PRIVATE_KEY" | agent-add
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
FOLDER=${1:-default}
|
||||
STORE_PATH="/tmp/ssh-agent/$FOLDER"
|
||||
mkdir -p "$STORE_PATH"
|
||||
|
||||
# Start the SSH agent if it isn't already.
|
||||
if [ -z "$SSH_AGENT_PID" ]; then
|
||||
if [ -f "$STORE_PATH/id" ]; then
|
||||
# Our auth agent is already running.
|
||||
# Reload the vars, and export them.
|
||||
SSH_AGENT_PID=$(cat "$STORE_PATH/id")
|
||||
export SSH_AGENT_PID
|
||||
|
||||
SSH_AUTH_SOCK=$(cat "$STORE_PATH/sock")
|
||||
export SSH_AUTH_SOCK
|
||||
else
|
||||
eval "$(ssh-agent)" > /dev/null
|
||||
echo "$SSH_AGENT_PID" > "$STORE_PATH"/id
|
||||
echo "$SSH_AUTH_SOCK" > "$STORE_PATH"/sock
|
||||
fi
|
||||
fi
|
||||
-35
@@ -1,35 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
if [ ! -z "$SSH_AGENT_PID" ]; then
|
||||
# Here, the environment is set already, just kill the script.
|
||||
eval $(ssh-agent -k) >/dev/null
|
||||
exit $?
|
||||
else
|
||||
# The env isn't set, construct the file path.
|
||||
FOLDER=${1:-default}
|
||||
STORE_PATH="/tmp/ssh-agent/$FOLDER"
|
||||
if [ ! -d "$STORE_PATH" ]; then
|
||||
echo "Store Path $STORE_PATH doesn't exist!" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# And check our files exist.
|
||||
if [ -f "$STORE_PATH/id" ]; then
|
||||
# Grab our PID and socket.
|
||||
SSH_AGENT_PID=$(cat "$STORE_PATH/id")
|
||||
export SSH_AGENT_PID
|
||||
rm "$STORE_PATH/id"
|
||||
|
||||
SSH_AUTH_SOCK=$(cat "$STORE_PATH/sock")
|
||||
export SSH_AUTH_SOCK
|
||||
rm "$STORE_PATH/sock"
|
||||
|
||||
|
||||
rmdir "$STORE_PATH"
|
||||
eval $(ssh-agent -k) >/dev/null
|
||||
exit $?
|
||||
else
|
||||
echo "SSH_AGENT_PID not set, $STORE_PATH/id doesn't exist!" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
Executable
+18
@@ -0,0 +1,18 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -eu
|
||||
|
||||
# Set deploy key
|
||||
SSH_PATH="$HOME/.ssh"
|
||||
|
||||
# Create .ssh dir if it doesn't exist
|
||||
[ -d "$SSH_PATH" ] || mkdir "$SSH_PATH"
|
||||
|
||||
# Place deploy_key into .ssh dir
|
||||
echo "$INPUT_REMOTE_KEY" > "$SSH_PATH/key"
|
||||
|
||||
# Set r+w to user only
|
||||
chmod 600 "$SSH_PATH/key"
|
||||
|
||||
# Do deployment
|
||||
sh -c "rsync $INPUT_SWITCHES -e 'ssh -i $SSH_PATH/key -o StrictHostKeyChecking=no -p $INPUT_REMOTE_PORT $INPUT_RSH' $GITHUB_WORKSPACE/$INPUT_PATH $INPUT_REMOTE_USER@$INPUT_REMOTE_HOST:$INPUT_REMOTE_PATH"
|
||||
@@ -1,4 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
echo "$@" >> ~/.ssh/known_hosts
|
||||
chmod 0664 ~/.ssh/known_hosts
|
||||
@@ -1,4 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
truncate -s 0 ~/.ssh/known_hosts
|
||||
chmod 0664 ~/.ssh/known_hosts
|
||||
Reference in New Issue
Block a user