Save state between build


#1

Hi.

We have a step configuration that it fails it sends a message to Slack, but if it runs successfully, it not send anything.

but, once it failed, if next build is green, I would notify slack to say “hey, the build has been fixed! Good job”

Is there any way to keep any value somewhere and retrieve it? Something like “key/store” value.

thanks


#2

Great idea @bernat_borras!

I’d say the closest to this is the Build Cache (http://devcenter.bitrise.io/caching/about-caching/) . With that you can store info into a file and then retrieve it in the next build.

To expose env vars for subsequent steps you can use envman: http://devcenter.bitrise.io/tips-and-tricks/expose-environment-variable/


#3

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:


#4

Wow thanks, I will try this snippet!

I guess it could be a good base for a new step?


#5

If you have any questions just let us know! :wink:

Might be indeed, although it’s one of those which would require a guide actually as e.g. it relies on the Build Cache mechanism :slight_smile:
But if you’d have some time creating the step and would have any questions just let us know! We’re always happy to help / answer questions :wink:


#6

Hi.
Is Bitrise Cache dir a good place to store some custom variables?
It will be cleaned up after some time, won’t it?


#7

Hello @dima_ost!
I would suggest you use the Env var or the secrets tab on the workflow editor to store custom variables! :slight_smile:


#8

Thanks, but I’d like to increment it for each build in workflow. There was suggestion to use bitrise cache, so I asked about cache lifetime


#9

The Build Cache, related to a specific branch, expires/is auto-deleted after 7 days, if there’s no new build on that branch in the meantime. This means that if you do builds on a specific branch every day (more frequently than a week), it’ll never expire/won’t get deleted automatically. If you don’t start a build on that specific branch for more than 7 days, then the related cache will be removed, and your next build will run like the first time, when there was no cache for that branch yet.


#10

Thank you. Do you plan to add build number env per workflow in future?


#11

Hey it is $BITRISE_BUILD_NUMBER but it is on the app itself, not workflow specific, for you can kind of chain them like lego pieces so it would not make sense.


#12

I’d like to increment version number for release builds independently of pull request builds, to prevent release version code ‘jumping’ like 101 - 300 - 500. I’d like to have release version codes sequence like 101-102-103


#13

hm, you can do it like this in my opinion, in the deploy workflow add the set android manifest version code and name step. if you use android ofc. and only those builds will change the version . number, the test workflows and the development ones don’t.