Search code examples
rundeck

Rundeck - API import job response in JSON


I tried returning the response from Rundeck import endpoint in JSON but no of the mentioned content-type(application/json) worked out for me.

Please let me know where I am making issues with current curl command:

curl -kSsv --header "X-Rundeck-Auth-Token:$RUNDECK_JOB_IMPORT_TOKEN"  -F xmlBatch=@"$rdeck_yaml_file"  "$RUNDECK_HOST:4443/api/33/project/$RUNDECK_PROJECT_NAME/jobs/import?dupeOption=update&fileformat=yaml"

I got the following response, I know what is the error I have.

It is really hard for me to catch this kind of exception on my CI/CD pipeline, I am thinking if I will get JSON response instead of XML that would be easily parsed and the automated pipeline will fail as any error occurs.

<result success='true' apiversion='33'>
  <succeeded count='1'>
    <job index='3' href='https:/RUNDECK_HOST/:4443/api/33/job/8b357ba5-14df-4e20-bb6c-c6ea089ecf70'>
      <id>8b357ba5-14df-4e20-bb6c-c6ea089ecf70</id>
      <name>ssr_id</name>
      <group></group>
      <project>project-name</project>
      <permalink>https://RUNDECK_HOST:4443/project/project-name/job/show/8b357ba5-14df-4e20-bb6c-c6ea089ecf70</permalink>
    </job>
  </succeeded>
  <failed count='2'>
    <job index='1'>
      <name>job-name</name>
      <group>group-name</group>
      <project>project-name</project>
      <error>Workflow must have at least one step
Invalid Option definition: flag: Workflow must have at least one step
Validation errors: Workflow must have at least one step; Invalid Option definition: flag: Workflow must have at least one step</error>
    </job>
    <job index='2'>
      <name>my_group</name>
      <group></group>
      <project>project-name</project>
      <error>Workflow must have at least one step
Validation errors: Workflow must have at least one step</error>
    </job>
  </failed>
  <skipped count='0' />
</result>*

Let me know where I need to improve on.


Solution

  • Works in this way (my script is a little bit different but works as an example):

    #!/bin/sh
    
    # protocol
    protocol="http"
    
    # basic rundeck info
    rdeck_host="pop-os"
    rdeck_port="4440"
    rdeck_api="38"
    rdeck_token="MbBwnnddLO2hhFJahwJ2pVARVTpG949j"
    
    # specific api call info
    rdeck_project="ProjectEXAMPLE"
    rdeck_yaml_file="myfile.yaml"
    
    # api call
    curl -s --location --request POST "$protocol://$rdeck_host:$rdeck_port/api/$rdeck_api/project/$rdeck_project/jobs/import?dupeOption=update&fileformat=yaml" \
      --header "Accept: application/json" \
      --header "X-Rundeck-Auth-Token: $rdeck_token" \
      -F xmlBatch=@"$rdeck_yaml_file"
    

    Output (using jq to "beautify"):

    {
      "succeeded": [
        {
          "index": 1,
          "href": "http://pop-os:4440/api/38/job/0aeaa0f4-d090-4083-b0a5-2878c5f558d1",
          "id": "0aeaa0f4-d090-4083-b0a5-2878c5f558d1",
          "name": "ChildJob",
          "group": "",
          "project": "ProjectEXAMPLE",
          "permalink": "http://pop-os:4440/project/ProjectEXAMPLE/job/show/0aeaa0f4-d090-4083-b0a5-2878c5f558d1"
        }
      ],
      "failed": [],
      "skipped": []
    }