Run a block step only if the build has failed

I’ve got a pipeline that deploys a cloudformation stack, runs tests against it, then cleans up that stack. I’d like to add a block step prior to the cleanup that, only when tests fail, prevents the cleanup from happening until I unblock it so that I can debug the stack in its failing state.

Unfortunately, it seems like if: build.state == 'failed' is evaluated at upload time, not when execution reaches that part of the dag. Is there anyway to do what I want using just pipeline syntax or do I need to add a dynamic pipeline upload step?

Here’s the relevant portion of my pipeline.yml:

  - label: ':jest: :cloudformation: Integration Tests'
    artifact_paths:
      - 'reports/**/*'
    command: |
      ./scripts/stack-as-env npm test -- --selectProjects 'Integration Tests'
    depends_on:
      - sam-deploy-test
    key: sam-jest-integration
    plugins:
      - ecr#v2.5.0: *plugins_ecr
      - docker-compose#v3.8.0: *plugins_docker_compose
      - check-run-reporter/check-run-reporter#main:
          report: 'reports/junit/**/*.xml'
          label: Integration Tests
          token: '$CHECK_RUN_REPORTER_TOKEN'
    retry: *retry

  - block: ':buildkite: Delay deletion due to build test failure'
    allow_dependency_failure: true
    prompt: |
      This build's cloud formation stack has been retained so you can debug it. Please click continue when finished to clean up the stack.
    depends_on:
      - sam-jest-integration
      - sam-contract
    if: build.state == 'failed'
    key: sam-block-delete

  - label: ':cloudformation: delete'
    command: |
      sam delete \
        --config-env=test \
        --no-prompts \
        --region=$${AWS_REGION:-$$AWS_DEFAULT_REGION} \
        --stack-name="$CI_STACK_NAME"
    depends_on:
      - sam-jest-integration
      - sam-contract
      - sam-block-delete
    key: sam-delete
    plugins: *plugins
    retry: *retry
    allow_dependency_failure: true

Hi @ianwremmel!

That is correct, the state of the overall build is not available when executing a job. If a job is executing, the state of the build can’t be failed or passed as well, because there’s still a job executing.

It’s not possible to evaluate build.state without using an API to fetch the build and look at the states of the finished jobs. If you’re within a job running within the build then the build is still running, too.

Best!

Hi @paula :wave:

Sorry to up this topic, but I’m facing the same need, annotate a build when it fails. Would be really nice if your dev team can provide a plugin to implement your workaround.

Hi @tetienne,

Firstly welcome to Buildkite community and thank you for reaching out with your question.

Please can you confirm your need is when a build fails you want to annotate the build with a message. Is this correct ?

Thanks,
Suma

Well, more specifically, if a step fail, I want to be able to detect within a dependency step that’s the parent failed, and annotate the build.

@tetienne in that case, we have an example here and here of how you could use conditionals in a dependent step to check the outcome of a previous step (in this case, the parent). You could use the same logic to create an annotation on your build in case the parent step or build fails.

Hopefully that helps you get started, and let us know if you have any further questions!

Hi @jeremy,

Thanks for the link. I missed this part.