├── Dockerfile ├── LICENSE ├── README.md ├── action.yml └── entrypoint.sh /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine 2 | 3 | LABEL "maintainer"="maddox " 4 | LABEL "repository"="https://github.com/alinz/actions" 5 | LABEL "version"="1.0.0" 6 | 7 | LABEL "com.github.actions.name"="test" 8 | LABEL "com.github.actions.description"="Run test" 9 | LABEL "com.github.actions.icon"="server" 10 | LABEL "com.github.actions.color"="orange" 11 | 12 | RUN apk update && \ 13 | apk add ca-certificates && \ 14 | apk add --no-cache openssh-client && \ 15 | apk add --no-cache openssl && \ 16 | apk add --no-cache --upgrade bash && \ 17 | rm -rf /var/cache/apk/* 18 | 19 | ADD entrypoint.sh /entrypoint.sh 20 | 21 | ENTRYPOINT ["/entrypoint.sh"] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 Ali Najafizadeh and contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SSH and SCP Setup in GITHUB's Action 2 | 3 | Setup ssh agent for both ssh and scp. Script can be run before and after scp operation has been completed 4 | 5 | ## USAGE 6 | 7 | ```yml 8 | - name: Operations 9 | uses: alinz/ssh-scp-action@master 10 | env: 11 | HELLO: cool 12 | MESSAGE: hello world 13 | with: 14 | key: ${{ secrets.SSH_KEY }} 15 | host: example.com 16 | port: 22 17 | user: john 18 | # runs this on remove server 19 | ssh_before: | 20 | rm -rf sample1.dat sample2.dat 21 | echo $HELLO 22 | echo $MESSAGE 23 | ls -lath 24 | 25 | # then uploads these 2 files 26 | scp: | 27 | sample1.txt john@example.com:~/sample1.dat 28 | sample2.txt john@example.com:~/sample2.dat 29 | 30 | # then run these commands 31 | ssh_after: | 32 | echo $HELLO 33 | echo $MESSAGE 34 | ls -lath 35 | ``` 36 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: 'SSH and SCP Setup' 2 | description: 'Executing remote ssh and scp commands' 3 | author: 'Ali Najafizadeh' 4 | inputs: 5 | host: 6 | description: 'ssh remote host' 7 | port: 8 | description: 'ssh remote port' 9 | default: 22 10 | user: 11 | description: 'ssh user' 12 | key: 13 | description: 'content of ssh private key. ex raw content of ~/.ssh/id_rsa' 14 | ssh_before: 15 | description: 'execute commands before SCP' 16 | scp: 17 | description: 'scp local and remote' 18 | ssh_after: 19 | description: 'execute commands after SCP' 20 | runs: 21 | using: 'docker' 22 | image: 'Dockerfile' 23 | 24 | branding: 25 | icon: 'terminal' 26 | color: 'gray-dark' 27 | -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | setupSSH() { 6 | local SSH_PATH="$HOME/.ssh" 7 | 8 | mkdir -p "$SSH_PATH" 9 | touch "$SSH_PATH/known_hosts" 10 | 11 | echo "$INPUT_KEY" > "$SSH_PATH/deploy_key" 12 | 13 | chmod 700 "$SSH_PATH" 14 | chmod 600 "$SSH_PATH/known_hosts" 15 | chmod 600 "$SSH_PATH/deploy_key" 16 | 17 | eval $(ssh-agent) 18 | ssh-add "$SSH_PATH/deploy_key" 19 | 20 | ssh-keyscan -t rsa $INPUT_HOST >> "$SSH_PATH/known_hosts" 21 | } 22 | 23 | executeSSH() { 24 | local LINES=$1 25 | local COMMAND="" 26 | 27 | # holds all commands separated by semi-colon 28 | local COMMANDS="" 29 | 30 | # this while read each commands in line and 31 | # evaluate each line agains all environment variables 32 | while IFS= read -r LINE; do 33 | LINE=$(eval 'echo "$LINE"') 34 | LINE=$(eval echo "$LINE") 35 | COMMAND=$(echo $LINE) 36 | 37 | if [ -z "$COMMANDS" ]; then 38 | COMMANDS="$COMMAND" 39 | else 40 | COMMANDS="$COMMANDS;$COMMAND" 41 | fi 42 | done <<< $LINES 43 | 44 | echo "$COMMANDS" 45 | ssh -o StrictHostKeyChecking=no -p ${INPUT_PORT:-22} $INPUT_USER@$INPUT_HOST "$COMMANDS" 46 | } 47 | 48 | executeSCP() { 49 | local LINES=$1 50 | local COMMAND= 51 | 52 | # this while read each commands in line and 53 | # evaluate each line agains all environment variables 54 | while IFS= read -r LINE; do 55 | LINE=$(eval 'echo "$LINE"') 56 | LINE=$(eval echo "$LINE") 57 | COMMAND=$(echo $LINE) 58 | 59 | # scp will fail if COMMAND is empty, this condition protects scp 60 | if [[ $COMMAND = *[!\ ]* ]]; then 61 | echo "scp -r -o StrictHostKeyChecking=no $COMMAND" 62 | scp -r -o StrictHostKeyChecking=no $COMMAND 63 | fi 64 | done <<< $LINES 65 | } 66 | 67 | setupSSH 68 | echo "+++++++++++++++++++RUNNING BEFORE SSH+++++++++++++++++++" 69 | executeSSH "$INPUT_SSH_BEFORE" 70 | echo "+++++++++++++++++++RUNNING BEFORE SSH+++++++++++++++++++" 71 | echo "+++++++++++++++++++RUNNING SCP+++++++++++++++++++" 72 | executeSCP "$INPUT_SCP" 73 | echo "+++++++++++++++++++RUNNING SCP+++++++++++++++++++" 74 | echo "+++++++++++++++++++RUNNING AFTER SSH+++++++++++++++++++" 75 | executeSSH "$INPUT_SSH_AFTER" 76 | echo "+++++++++++++++++++RUNNING AFTER SSH+++++++++++++++++++" --------------------------------------------------------------------------------