Save state between build

It’s a very, very rough base script, “work in progress” if you like (wasn’t written for this problem originally, just tweaked it a bit), but might be useful as a starter :wink: :

    steps:
    - cache-pull@2.0.1: {}
    - script@1.1.5:
        title: check if changed
        inputs:
        - content: |
            #!/usr/bin/env bash
            set -ex

            build_status_file_pth="$BITRISE_CACHE_DIR/build-status"
            if [[ -f "$build_status_file_pth" ]] ; then
              last_successful_build_number=$(cat "$build_status_file_pth")
              if [[ $(( $last_successful_build_number + 1)) == $BITRISE_BUILD_NUMBER ]] ; then
                # previous build was successful
              else
                # previous build was not successful
                envman add --key 'IS_PREV_BUILD_FAILED' --value 'indeed'
              fi
            fi
    - random-quote@3.0.2:
        is_always_run: false
        run_if: '{{ enveq "IS_PREV_BUILD_FAILED" "indeed" }}'
    - script@1.1.5:
        title: save current build's status
        inputs:
        - content: |-
            #!/usr/bin/env bash
            set -ex

            build_status_file_pth="$BITRISE_CACHE_DIR/build-status"

            # store current build number
            echo "$BITRISE_BUILD_NUMBER" > "$build_status_file_pth"
    - cache-push@2.0.3:
        inputs:
        - cache_paths: "$BITRISE_CACHE_DIR"

Couple of notes:

  • A Step by default does not run / gets executed if a previous step failed. is_always_run: false makes this explicit in the config, e.g. the Slack step is one of those steps which is marked with is_always_run: true so that it also runs when the build failed.
  • run_if can be used to mark a step to only run if a simple expression evaluates to true (http://devcenter.bitrise.io/tips-and-tricks/disable-a-step-by-condition/#run-a-step-only-if-the-build-failed)
  • In this example random-quote is the step which is meant to be only performed if this build is successful (is_always_run: false) AND only if the IS_PREV_BUILD_FAILED env var is set to the value indeed (which is set by a previous Script step, in case the prev build’s number is not the current -1)

Again, this is just a sample “layout”, unfortunately I did not have more time right now to make this an actual working example, so treat it as a “starter” script :wink:

1 Like