azure-devopsazure-pipelinesazure-pipelines-yaml

Azure pipeline - run shell script on the remote server


I have a task to run a shell script on an onprem linux server using Azure Devops pipeline . The shells script is in the same repo as pipeline yml.

Below is the template I am running but it gives me an error that the given key was not present in dictionary.

- stage: scripttest
  jobs:
  - job: scripttest
    steps:
     # Step 1: Checkout the repo (to get the script from the Azure repo)
     - task: Checkout@1
       displayName: "Checkout repository"
     # Step 2: SSH into the server and run the script downloaded from repo
     - task: SSH@0
       displayName: "Run Script on Server"
       inputs:
         sshEndpoint: $(ServiceConnection)
         runOptions: 'commands'
         commands: |
           echo "running command on the server"
           ssh user@$(serverhost) 'bash -s' < $(Build.SourcesDirectory)/file/test1/script.sh

I verified and the variable group values are correct. How do I fix this. the requirement is to run the script thru pipeline without copying the script to server.


Solution

  • Conceptually, the SSH@0 establishes a SSH terminal session on the remote computer and then executes commands or scripts on that remote machine.

    Reference Documentation:

    # SSH v0
    # Run shell commands or a script on a remote machine using SSH.
    - task: SSH@0
      inputs:
        sshEndpoint: # string. Required. SSH service connection. 
        runOptions: 'commands' # 'commands' | 'script' | 'inline'. Required. Run. Default: commands.
        commands: # string. Required when runOptions = commands. Commands. 
        #scriptPath: # string. Required when runOptions = script. Shell script path. 
        #inline: # string. Required when runOptions = inline. Inline Script. 
        #interpreterCommand: '/bin/bash' # string. Optional. Use when runOptions = inline. Interpreter command. Default: /bin/bash.
        #args: # string. Optional. Use when runOptions = script. Arguments. 
      # Advanced
        #failOnStdErr: true # boolean. Fail on STDERR. Default: true.
        #interactiveSession: false # boolean. Enable interactive session. Default: false.
        readyTimeout: '20000' # string. Required. SSH handshake timeout. Default: 20000.
        #interactiveKeyboardAuthentication: false # boolean. Use interactive-keyboard authentication. Default: false.
    

    The task supports three different types of runOptions:

    When you specify script or inline the task copies the contents of the script to remote machine and executes it on that machine. (When the runOption is inline, it copies the contents of the inline script to a temporary file and transfers that to the remote machine.) When the script completes (or fails), it removes the script from the remote machine.

    To execute a single script:

    - task: SSH@0
      inputs:
        sshEndpoint: 'MySSHConnection'
        runOptions: script
        scriptPath: $(Build.SourcesDirectory)/file/test1/script.sh
    

    This connects to the machine using the credentials as specified in the 'MySSHConnection' service connection.

    If you need to execute multiple scripts or include dependencies, you'll need to use the CopyFilesOverSSH@0 task before executing the script.