├── .github └── workflows │ └── test.yml ├── Dockerfile ├── LICENSE ├── README.md ├── action.yml ├── entrypoint.sh └── result.png /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | on: 3 | push: 4 | branches: 5 | - stage 6 | 7 | env: 8 | FIRST_NAME: Mona 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: ls -a via OPEN SSH Private Key 14 | uses: fifsky/ssh-action@master 15 | with: 16 | command: | 17 | cd /tmp 18 | ls -a 19 | echo "env_var:$FIRST_NAME" 20 | exit 1 21 | host: ${{ secrets.HOST }} 22 | user: root 23 | key: ${{ secrets.PRIVATE_KEY}} 24 | # pass: ${{ secrets.PASS }} 25 | args: "-tt -vvv" 26 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:latest 2 | RUN apk update && \ 3 | apk add --no-cache ca-certificates \ 4 | openssh-client \ 5 | sshpass \ 6 | bash 7 | 8 | COPY LICENSE README.md / 9 | 10 | COPY entrypoint.sh /entrypoint.sh 11 | 12 | ENTRYPOINT ["/entrypoint.sh"] 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2018 GitHub, Inc. and contributors 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Remote SSH Commands 2 | 3 | ![](https://github.com/fifsky/ssh-action/workflows/test/badge.svg) 4 | 5 | Simple GitHub Action to run a command on a remote server using SSH. This is working with the latest [GitHub Actions](https://github.com/features/actions). 6 | 7 | ⭐️ SSH password are supported from version 0.0.3 8 | 9 | ## ✨ Example Usage 10 | 11 | **Example using OpenSSH private key** 12 | 13 | ```yml 14 | - name: ls -a via ssh 15 | uses: fifsky/ssh-action@master 16 | with: 17 | command: | 18 | cd /tmp 19 | ls -a 20 | host: ${{ secrets.HOST }} 21 | user: root 22 | key: ${{ secrets.PRIVATE_KEY}} 23 | ``` 24 | 25 | 🔐 Set your secrets here: `https://github.com/USERNAME/REPO/settings/secrets`. 26 | 27 | Check out [the workflow example](.github/workflows/test.yml) for a minimalistic yaml workflow in GitHub Actions. 28 | 29 | **Result** 30 | 31 | ![result of example ssh workflow](result.png) 32 | 33 | ## Options 34 | 35 | - **host** - _string_ - Hostname or IP address of the server. **Default:** `'localhost'` 36 | 37 | - **port** - _integer_ - Port number of the server. **Default:** `22` 38 | 39 | - **user** - _string_ - Username for authentication. **Default:** (root) 40 | 41 | - **key** - _string_ - Required, that contains a private key for either key-based or hostbased user authentication (OpenSSH format). **Default:** (none) 42 | 43 | - **pass** - _string_ - Password for authentication. 44 | 45 | - **args** - _string_ - SSH parameters for example: -tt. 46 | 47 | > Password and Private Key can only be configured one item 48 | 49 | 50 | If you need to add some extra SSH parameters, you can setting the args option. 51 | 52 | For example, add `-tt` parameter to solve: https://github.com/fifsky/ssh-action/issues/4 53 | 54 | ``` 55 | Pseudo-terminal will not be allocated because stdin is not a terminal. 56 | ``` 57 | 58 | ## Tips 59 | 60 | If emitting "mesg: ttyname failed: Inappropriate ioctl for device", You need to modify your Linux files as follows 61 | 62 | ``` 63 | vim /root/.profile 64 | // Modify the "mesg n || true" to "tty -s && mesg n || true" 65 | ``` 66 | 67 | 68 | ## Thanks 69 | 70 | Documentation and parameters design from: 71 | https://github.com/garygrossgarten/github-action-ssh 72 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: 'Remote SSH Commands' 2 | description: 'Run remote ssh commands' 3 | author: 'fifsky@gmail.com' 4 | inputs: 5 | command: 6 | description: "Command to execute on the remote server." 7 | required: true 8 | host: 9 | description: "Hostname or IP address of the server." 10 | required: false 11 | default: "localhost" 12 | user: 13 | description: "Username for authentication." 14 | required: false 15 | default: "root" 16 | port: 17 | description: "Port number of the server." 18 | required: false 19 | default: "22" 20 | key: 21 | description: "String that contains a private key for either key-based or hostbased user authentication (OpenSSH format)" 22 | required: false 23 | keyfile: 24 | description: "File location to an identity file" 25 | required: false 26 | pass: 27 | description: "Password for authentication." 28 | required: false 29 | args: 30 | description: "SSH parameters for example: -tt." 31 | required: false 32 | 33 | runs: 34 | using: 'docker' 35 | image: 'Dockerfile' 36 | 37 | branding: 38 | color: "green" 39 | icon: "lock" 40 | -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -eu 4 | 5 | printf '\033[33m Warning: This action does not currently support host verification; verification is disabled. \n \033[0m\n' 6 | 7 | SSHPATH="$HOME/.ssh" 8 | 9 | if [ ! -d "$SSHPATH" ] 10 | then 11 | mkdir "$SSHPATH" 12 | fi 13 | 14 | if [ ! -f "$SSHPATH/known_hosts" ] 15 | then 16 | touch "$SSHPATH/known_hosts" 17 | fi 18 | 19 | echo "$INPUT_KEY" > "$SSHPATH/deploy_key" 20 | if [ "$INPUT_KEYFILE" = "" ] 21 | then 22 | KEYFILE="$SSHPATH/deploy_key" 23 | else 24 | KEYFILE=$INPUT_KEYFILE 25 | fi 26 | 27 | chmod 700 "$SSHPATH" 28 | chmod 600 "$SSHPATH/known_hosts" 29 | chmod 600 "$SSHPATH/deploy_key" 30 | 31 | echo "$INPUT_COMMAND" > $HOME/shell.sh 32 | echo "exit \$?" >> $HOME/shell.sh 33 | cat $HOME/shell.sh 34 | 35 | echo Start Run Command 36 | 37 | if [ "$INPUT_PASS" = "" ] 38 | then 39 | sh -c "ssh $INPUT_ARGS -i $KEYFILE -o StrictHostKeyChecking=no -p $INPUT_PORT ${INPUT_USER}@${INPUT_HOST} < $HOME/shell.sh" 40 | else 41 | sh -c "sshpass -p '$INPUT_PASS' ssh $INPUT_ARGS -o StrictHostKeyChecking=no -p $INPUT_PORT ${INPUT_USER}@${INPUT_HOST} < $HOME/shell.sh" 42 | fi 43 | -------------------------------------------------------------------------------- /result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifsky/ssh-action/5a841de87115730aff750ac127773e3b2b3f56dd/result.png --------------------------------------------------------------------------------