CloudBees action: Execute commands on a remote server through SSH

2 minute read

Use this action to connect to a remote server and execute commands via the Secure Shell Protocol (SSH).

All CloudBees action repositories are listed at CloudBees, Inc. on GitHub.

Inputs

Table 1. Input details
Input name Data type Required? Description

host

String

Yes

SSH host address.

username

String

Yes

The SSH username.

port

String

No

SSH port number. Default is 22.

script

String

Yes

The commands to be executed.

key-path

String

Required only if password is not specified.

The path to the SSH private key file.

passphrase

String

No

The SSH key passphrase.

password

String

Required only if key-path is not specified.

The SSH password.

Usage examples

Execute remote commands using an SSH private key

In your YAML file, add:

- name: SSH with username and key uses: cloudbees-io/ssh-deploy-actions@v1 with: host: ${{ secrets.SSH_HOST }} username: user1 key_path: ${{ secrets.SSH_PRIVATE_KEY }} port: "22" script: whoami,pwd,echo "hello"

Execute remote commands using a password

In your YAML file, add:

- name: SSH with username and password uses: cloudbees-io/ssh-deploy-actions@v1 with: host: ${{ secrets.SSH_HOST }} username: testUser password: ${{ secrets.SSH_PASSWORD }} port: "22" script: whoami,pwd

Execute remote commands using a passphrase-protected key

In your YAML file, add:

- name: SSH with username and passphrase-protected key uses: cloudbees-io/ssh-deploy-actions@v1 with: host: ${{ secrets.SSH_HOST }} username: demoUser key_path: ${{ secrets.SSH_PP_KEY }} port: "22" passphrase: ${{ secrets.SSH_PP_KEY_PASSPHRASE }} script: pwd,echo "hello"

Execute chef cookbooks using the ssh-actions

Chef is an open-source configuration management tool that automates infrastructure provisioning and management by defining system configurations as code.

Chef works with three core components:

You require SSH access to the workstation to execute the knife commands and trigger the cookbooks on the nodes.

The platform user must provide a single workstation with pre-configured roles, knife bootstrap, and SSH configured. The communication between the workstation, server, and nodes must be verified and tested.

In your YAML file, add:

name: ssh-actions apiVersion: automation.cloudbees.io/v1alpha1 kind: workflow on: push: branches: - '**' jobs: sshaction: steps: - name: executing-remote-commands-using-username-and-ssh-key #uses: cloudbees-io/ssh-deploy-actions@v1 uses: cloudbees-io/ssh-deploy-actions-private@v1 with: host: ec2-54-83-82-190.compute-1.amazonaws.com username: ${{ secrets.CHEF_WS_USER }} key_path: ${{ secrets.CHEF_WS_KEY }} port: "22" script: cd chef-repo/ && sudo knife ssh "name:*" 'sudo chef-client -o "recipe[nano_install]"' -x ec2-user -p 22 -i chefnode.pem

By executing the following command on the workstation, it will log in to the node machines through SSH and execute the chef-client command, which executes the cookbook:

sudo knife ssh "name:*" 'sudo chef-client -o "recipe[nano_install]"' -x ec2-user -p 22 -i chefnode.pem

For more information, refer to knife-ssh.