yamlgoogle-cloud-rungoogle-workflowsgoogle-cloud-run-jobs

How to check for success status of Cloud Run Job within Cloud Workflows?


I'm running several Cloud Run Jobs in sequence using Cloud Workflow. After completing Job #1, I want to check for successful completion before moving to Job #2. In testing, I see that Job #1 returns the following results JSON for status:

"status": {
"completionTime": "2024-11-08T17:09:23.947071Z",
"conditions": [
  {
    "lastTransitionTime": "2024-11-08T17:09:23.947071Z",
    "status": "True",
    "type": "Completed"
  },
  {
    "lastTransitionTime": "2024-11-08T17:08:32.322671Z",
    "status": "True",
    "type": "ResourcesAvailable"
  },
  {
    "lastTransitionTime": "2024-11-08T17:08:35.183828Z",
    "status": "True",
    "type": "Started"
  },
  {
    "lastTransitionTime": "2024-11-08T17:08:32.219908Z",
    "status": "True",
    "type": "ContainerReady"
  }
],

So, to test for success, I think I want something like:

- check_step1:
    switch:
      - condition: ${step1_result.status.conditions.type == "Completed"}
        next: step2

But, this doesn't work in Workflows. How, then, do I write a proper test condition?


Solution

  • NOT a Workflows user but you piqued my curiosity ;-)

    Updated

    I tested this using a Cloud Run job (that I had lying around ;-))

    main:
      steps:
      - init:
          assign:
          - project: ${sys.get_env("PROJECT")}
          - location: ${sys.get_env("LOCATION")}
          - job: ${sys.get_env("JOB")}
          - result: false
      - run_job:
          call: googleapis.run.v1.namespaces.jobs.run
          args:
            name: ${"namespaces/" + project + "/jobs/" + job}
            location: ${location}
          result: execution
      - step-for-loop:
          for:
            value: condition
            in: ${execution.status.conditions} 
            steps:
            - step-loop-assign:
                assign:
                  # True if type=="Completed" and status=="True"
                  # Once any `result` is true, then `result` is always true
                  # Was unsure how to break from the loop once a result is true
                - result: ${result or (condition["type"] == "Completed" and condition["status"] == "True")}
      - done:
          return: ${result}
    

    This works for me:

    gcloud workflows deploy ${NAME} \
    --location=${LOCATION} \
    --project=${PROJECT} \
    --source=${PWD}/workflow.yml \
    --service-account=${EMAIL} \
    --set-env-vars=\
    PROJECT=${PROJECT},\
    LOCATION=${LOCATION},\
    JOB=${NAME}
    
    gcloud workflows execute ${NAME} \
    --location=${LOCATION} \
    --project=${PROJECT}
    
    gcloud workflows executions describe ${EXECUTION} \
    --workflow=${NAME} \
    --location=${LOCATION} \
    --project=${PROJECT} \
    --format=value(result)
    
    result: 'true'
    
    Original
    main:
      steps:
      - step-global-assign:
          assign:
            # Replace with `ExecutionStatus`
            # i.e. `step1_result.status.conditions`
          - conditions: [
                {"status":"True","type":"ResourcesAvailable"},
                {"status":"True","type":"Started"},
                {"status":"False","type":"Completed"},
                {"status":"True","type":"ContainerReady"},
            ]
            # Assume false
          - result: false
      - step-for-loop:
          for:
            value: condition
            in: ${conditions} 
            steps:
            - step-loop-assign:
                assign:
                  # True if type=="Completed" and status=="True"
                  # Once any `result` is true, then `result` is always true
                  # Was unsure how to break from the loop once a result is true
                - result: ${result or (condition["type"] == "Completed" and condition["status"] == "True")}
      - done:
          return: ${result}