CICD Actions for executing remote ssh commands
502
Important: Only support Linux docker container.
All sensitive variables should be set as encrypted secrets in the action's configuration.
name: remote ssh command
on: [push]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: executing remote ssh commands using password
uses: docker://toumoro/ssh-commands-action:latest
env:
INPUT_HOST: ${{ secrets.HOST }}
INPUT_USERNAME: ${{ secrets.USERNAME }}
INPUT_SCRIPT_PASSWORD: ${{ secrets.PASSWORD }}
INPUT_PORT: ${{ secrets.PORT }}
INPUT_SCRIPT: whoami
stage:
...
- ssh_command
...
ssh_command:
image: toumoro/ssh-commands-action:latest
script: [ 'true' ]
variables:
INPUT_HOST: $HOST
INPUT_USERNAME: $USERNAME
INPUT_PASSWORD: $PASSWORD
INPUT_PORT: $PORT
INPUT_SCRIPT: whoami
pipelines:
default:
...
- step:
name: SSH Commands
script:
- *auto_devops
- set_environment_variable
- pipe: docker://toumoro/ssh-commands-action:latest
variables:
INPUT_HOST: $HOST
INPUT_USERNAME: $USERNAME
INPUT_PASSWORD: $PASSWORD
INPUT_PORT: $PORT
INPUT_SCRIPT: whoami
Executing remote ssh commands.
name: remote ssh command
on: [push]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: executing remote ssh commands using password
uses: docker://toumoro/ssh-commands-action:latest
env:
INPUT_HOST: ${{ secrets.HOST }}
INPUT_USERNAME: ${{ secrets.USERNAME }}
INPUT_SCRIPT_PASSWORD: ${{ secrets.PASSWORD }}
INPUT_PORT: ${{ secrets.PORT }}
INPUT_SCRIPT: whoami
output:
======CMD======
whoami
======END======
out: ***
==============================================
✅ Successfully executed commands to all host.
==============================================
2230s10mSSH Proxy Setting:
2230sExecuting remote ssh commands using password.
- name: executing remote ssh commands using password
uses: docker://toumoro/ssh-commands-action:latest
env:
INPUT_HOST: ${{ secrets.HOST }}
INPUT_USERNAME: ${{ secrets.USERNAME }}
INPUT_SCRIPT_PASSWORD: ${{ secrets.PASSWORD }}
INPUT_PORT: ${{ secrets.PORT }}
INPUT_SCRIPT: whoami
Using private key
- name: executing remote ssh commands using ssh key
uses: docker://toumoro/ssh-commands-action:latest
env:
INPUT_HOST: ${{ secrets.HOST }}
INPUT_USERNAME: ${{ secrets.USERNAME }}
INPUT_KEY: ${{ secrets.KEY }}
INPUT_PORT: ${{ secrets.PORT }}
INPUT_SCRIPT: whoami
Multiple Commands
- name: multiple command
uses: docker://toumoro/ssh-commands-action:latest
env:
INPUT_HOST: ${{ secrets.HOST }}
INPUT_USERNAME: ${{ secrets.USERNAME }}
INPUT_KEY: ${{ secrets.KEY }}
INPUT_PORT: ${{ secrets.PORT }}
INPUT_SCRIPT: |
whoami
ls -al
Multiple Hosts
- name: multiple host
uses: docker://toumoro/ssh-commands-action:latest
env:
- INPUT_HOST: "foo.com"
+ INPUT_HOST: "foo.com,bar.com"
INPUT_USERNAME: ${{ secrets.USERNAME }}
INPUT_KEY: ${{ secrets.KEY }}
INPUT_PORT: ${{ secrets.PORT }}
INPUT_SCRIPT: |
whoami
ls -al
Synchronous execution on multiple hosts
- name: multiple host
uses: docker://toumoro/ssh-commands-action:latest
env:
INPUT_HOST: "foo.com,bar.com"
+ INPUT_SYNC: true
INPUT_USERNAME: ${{ secrets.USERNAME }}
INPUT_KEY: ${{ secrets.KEY }}
INPUT_PORT: ${{ secrets.PORT }}
INPUT_SCRIPT: |
whoami
ls -al
Pass environment variable to shell script
- name: pass environment
uses: docker://toumoro/ssh-commands-action:latest
env:
+ FOO: "BAR"
+ BAR: "FOO"
+ SHA: ${{ github.sha }}
INPUT_HOST: ${{ secrets.HOST }}
INPUT_USERNAME: ${{ secrets.USERNAME }}
INPUT_KEY: ${{ secrets.KEY }}
INPUT_PORT: ${{ secrets.PORT }}
+ INPUT_ENVS: FOO,BAR,SHA
INPUT_SCRIPT: |
echo "I am $FOO"
echo "I am $BAR"
echo "sha: $SHA"
Inside env object, you need to pass every environment variable as a string, passing Integer data type or any other may output unexpected results.
Stop script after first failure. ex: missing abc folder
- name: stop script if command error
uses: docker://toumoro/ssh-commands-action:latest
env:
INPUT_HOST: ${{ secrets.HOST }}
INPUT_USERNAME: ${{ secrets.USERNAME }}
INPUT_KEY: ${{ secrets.KEY }}
INPUT_PORT: ${{ secrets.PORT }}
+ INPUT_SCRIPT_STOP: true
INPUT_SCRIPT: |
mkdir abc/def
ls -al
output:
======CMD======
mkdir abc/def
ls -al
======END======
2019/11/21 01:16:21 Process exited with status 1
err: mkdir: cannot create directory ‘abc/def’: No such file or directory
##[error]Docker run failed with exit code 1
How to connect remote server using ProxyCommand?
+--------+ +----------+ +-----------+
| Laptop | <--> | Jumphost | <--> | FooServer |
+--------+ +----------+ +-----------+
in your ~/.ssh/config, you will see the following.
Host Jumphost
HostName Jumphost
User ubuntu
Port 22
IdentityFile ~/.ssh/keys/jump_host.pem
Host FooServer
HostName FooServer
User ubuntu
Port 22
ProxyCommand ssh -q -W %h:%p Jumphost
How to convert to YAML format of GitHubActions.
- name: ssh proxy command
uses: docker://toumoro/ssh-commands-action:latest
env:
INPUT_HOST: ${{ secrets.HOST }}
INPUT_USERNAME: ${{ secrets.USERNAME }}
INPUT_KEY: ${{ secrets.KEY }}
INPUT_PORT: ${{ secrets.PORT }}
+ INPUT_PROXY_HOST: ${{ secrets.PROXY_HOST }}
+ INPUT_PROXY_USERNAME: ${{ secrets.PROXY_USERNAME }}
+ INPUT_PROXY_KEY: ${{ secrets.PROXY_KEY }}
+ INPUT_PROXY_PORT: ${{ secrets.PROXY_PORT }}
INPUT_SCRIPT: |
mkdir abc/def
ls -al
Protecting a Private Key. The purpose of the passphrase is usually to encrypt the private key. This makes the key file by itself useless to an attacker. It is not uncommon for files to leak from backups or decommissioned hardware, and hackers commonly exfiltrate files from compromised systems.
- name: ssh key passphrase
uses: docker://toumoro/ssh-commands-action:latest
env:
INPUT_HOST: ${{ secrets.HOST }}
INPUT_USERNAME: ${{ secrets.USERNAME }}
INPUT_KEY: ${{ secrets.KEY }}
INPUT_PORT: ${{ secrets.PORT }}
+ INPUT_PASSPHRASE: ${{ secrets.PASSPHRASE }}
INPUT_SCRIPT: |
whoami
ls -al
Content type
Image
Digest
Size
6.9 MB
Last updated
about 6 years ago
docker pull toumoro/ssh-commands-action