From ca82210d430f03cc14944fad512da17ee54c1494 Mon Sep 17 00:00:00 2001 From: Marco Dalla Santa Date: Fri, 18 Dec 2020 17:22:26 +0100 Subject: [PATCH] Initial commit --- .github/workflows/main.yml | 23 ++++++++++++++ CHANGELOG.md | 9 ++++++ Dockerfile | 13 ++++++++ LICENSE | 22 ++++++++++++++ README.md | 30 +++++++++++++++++++ action.yml | 61 ++++++++++++++++++++++++++++++++++++++ entrypoint.sh | 15 ++++++++++ with_key.sh | 24 +++++++++++++++ with_pass.sh | 19 ++++++++++++ 9 files changed, 216 insertions(+) create mode 100644 .github/workflows/main.yml create mode 100644 CHANGELOG.md create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 README.md create mode 100644 action.yml create mode 100755 entrypoint.sh create mode 100644 with_key.sh create mode 100644 with_pass.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..9bc551e --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,23 @@ +name: Test + +on: + push: + branches: + - main + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + + - uses: underscore69/ssh-scp-deploy@main + with: + local: "./" + remote: "~/" + host: ${{secrets.HOST}} + user: ${{secrets.USER}} + key: ${{secrets.KEY}} + pre_upload: echo "pre_upload ๐Ÿ‘ˆ" + post_upload: echo "post_upload ๐Ÿ‘‰" \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..08ba07c --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +# [Unreleased] + +## [v1.0.0] - 2020-12-18 + +### Added +- First definition of the action. + + +[v1.0.0]: https://github.com/underscore69/ssh-scp-deploy/tree/v1.0.0 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0e11a7f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM alpine:latest + +RUN apk update && \ + apk add --no-cache ca-certificates \ + openssh-client \ + sshpass \ + bash + +COPY LICENSE README.md / + +COPY entrypoint.sh /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..07f9716 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ + +The MIT License (MIT) + +Copyright (c) 2020 Marco Dalla Santa and contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..49c1327 --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# ssh-scp-deploy + +[![Action type](https://img.shields.io/badge/Docker%20action-262b31?logo=docker&logoColor=2496ed)](https://docs.github.com/en/actions/creating-actions/about-actions#types-of-actions) +[![License](https://img.shields.io/github/license/underscore69/ssh-scp-deploy)](https://opensource.org/licenses/MIT) +[![Pipeline status](https://img.shields.io/github/workflow/status/underscore69/ssh-scp-deploy/Test?label=test)](./) + +This action provide an easy and higly customizable way to upload files via scp and execute a set of command via ssh before or/and after. + +## Supported runners +[![Supported runner](https://img.shields.io/badge/Linux-262b31?style=for-the-badge&logo=linux&logoColor=fcc624)](https://docs.github.com/en/actions/creating-actions/about-actions#docker-container-actions) + +## Usage +```yaml + - uses: underscore69/ssh-scp-deploy@v1.0.0 + with: + local: './' # Local file path - REQUIRED false - DEFAULT ./ + remote: '~/' #ย Remote file path - REQUIRED false - DEFAULT ~/ + host: ${{secrets.HOST}} #ย Remote server address - REQUIRED true + port: ${{secrets.PORT}} #ย Remote server port - REQUIRED false - DEFAULT 22 + user: ${{secrets.USER}} # Remote server user - REQUIRED true + password: ${{secrets.PASSWORD}} #ย User password - REQUIRED at least one of "password" or "key" + key: ${{secrets.KEY}} #ย Remote server private key - REQUIRED at least one of "password" or "key" + pre_upload: echo "This will be executed before the upload!" #ย Command to run via ssh before scp upload - REQUIRED false + post_upload: echo "This will be executed after the upload!" #ย Command to run via ssh after scp upload - REQUIRED false + ssh_options: -o StrictHostKeyChecking=no # A set of ssh_option separated by -o - REQUIRED false - DEFAULT -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null + scp_options: -v # Flags to use during scp - REQUIRED false - DEFAULT '' +``` + +## License +The source code, scripts and documentation in this project are released under the [MIT License](LICENSE) \ No newline at end of file diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..c0b7f2b --- /dev/null +++ b/action.yml @@ -0,0 +1,61 @@ +name: 'ssh-scp-deploy' +description: 'A GitHub Action that upload the artifact via scp and runs commands before or/and after.' +author: 'Marco Dalla Santa' + +inputs: + local: + description: 'Local file path' + required: false + default: './' + + remote: + description: 'Remote file path' + required: false + default: '~/' + + host: + description: 'Remote server address' + required: true + + port: + description: 'Remote server port (default 22)' + required: false + default: 22 + + user: + description: 'Remote server user' + required: true + + password: + description: 'User password' + required: false + + key: + description: 'Remote server private key' + required: false + + pre_upload: + description: 'Command to run via ssh before scp upload' + required: false + + post_upload: + description: 'Command to run via ssh after scp upload' + required: false + + ssh_options: + description: 'A set of ssh_option separated by -o' + required: false + default: -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null + + scp_options: + description: 'Flags to use during scp' + required: false + default: + +runs: + using: 'docker' + image: 'Dockerfile' + +branding: + icon: 'upload-cloud' + color: 'green' \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..f4e9b79 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,15 @@ +#!/bin/bash +PASSWORD=${INPUT_PASSWORD} +KEY=${INPUT_KEY} +if [ -z "$PRE_UPLOAD" and "$PASSWORD"]; then + echo "๐Ÿ”‘ Please provide at least a key or a password..."; + exit 0; +fi + +if [[ ! -z "$KEY" ]]; then + echo "๐Ÿ”‘ Using key file..."; + . with_key.sh; +else + echo "๐Ÿ”‘ Using password..."; + . with_pass.sh; +fi \ No newline at end of file diff --git a/with_key.sh b/with_key.sh new file mode 100644 index 0000000..ae54939 --- /dev/null +++ b/with_key.sh @@ -0,0 +1,24 @@ +echo "๐Ÿ”‘ Adding ssh key..." && +eval $(ssh-agent -s) && +ssh-add <(echo "${INPUT_KEY}") && +echo "๐Ÿ” Added ssh key"; + +PRE_UPLOAD=${INPUT_PRE_UPLOAD} +if [ ! -z "$PRE_UPLOAD" ]; then + echo "๐Ÿ‘Œ Executing pre-upload script..." && + ssh ${INPUT_SSH_OPTIONS} ${INPUT_USER}@${INPUT_HOST} "$INPUT_PRE_UPLOAD && exit" && + echo "โœ… Executed pre-upload script"; +fi + +echo "๐Ÿšš Uploading via scp..." && +scp ${INPUT_SSH_OPTIONS} ${INPUT_SCP_OPTIONS} -P "${INPUT_PORT}" -r ${INPUT_LOCAL} ubuntu@${INPUT_HOST}:"${INPUT_REMOTE}" && +echo "๐Ÿ™Œ Uploaded via scp"; + +POST_UPLOAD=${INPUT_POST_UPLOAD} +if [ ! -z "$POST_UPLOAD" ]; then + echo "๐Ÿ‘Œ Executing post-upload script..." && + ssh ${INPUT_SSH_OPTIONS} ${INPUT_USER}@${INPUT_HOST} "$POST_UPLOAD && exit" && + echo "โœ… Executed post-upload script"; +fi + +echo "๐ŸŽ‰ Done"; \ No newline at end of file diff --git a/with_pass.sh b/with_pass.sh new file mode 100644 index 0000000..32a968a --- /dev/null +++ b/with_pass.sh @@ -0,0 +1,19 @@ +PRE_UPLOAD=${INPUT_PRE_UPLOAD} +if [ ! -z "$PRE_UPLOAD" ]; then + echo "๐Ÿ‘Œ Executing pre-upload script..." && + sshpass -p ${PASSWORD} ssh ${INPUT_SSH_OPTIONS} ${INPUT_USER}@${INPUT_HOST} "$INPUT_PRE_UPLOAD && exit" && + echo "โœ… Executed pre-upload script"; +fi + +echo "๐Ÿšš Uploading via scp..." && +sshpass -p ${PASSWORD} scp ${INPUT_SSH_OPTIONS} ${INPUT_SCP_OPTIONS} -P "${INPUT_PORT}" -r ${INPUT_LOCAL} ubuntu@${INPUT_HOST}:"${INPUT_REMOTE}" && +echo "๐Ÿ™Œ Uploaded via scp"; + +POST_UPLOAD=${INPUT_POST_UPLOAD} +if [ ! -z "$POST_UPLOAD" ]; then + echo "๐Ÿ‘Œ Executing post-upload script..." && + sshpass -p ${PASSWORD} ssh ${INPUT_SSH_OPTIONS} ${INPUT_USER}@${INPUT_HOST} "$POST_UPLOAD && exit" && + echo "โœ… Executed post-upload script"; +fi + +echo "๐ŸŽ‰ Done"; \ No newline at end of file