CircleCIで終了ステータスが0以外の時に失敗せずに終了させる

モチベーション

CircleCIのステップで git commit をした際に差分が無い場合は終了ステータスが1になりワークフローが失敗扱いとなる。これを成功扱いにして終了させたい。

version: 2.1

jobs:
  update-text:
    docker:
      - image: ubuntu:xenial-20210804
    steps:
      - checkout 
      - run: apt-get update
      - run: apt-get -y install git 
      - run: git config --global user.email xxx
      - run: git config --global user.name xxx
      - run: git add sample.txt
      - run: git commit -m '[CircleCI] update sample.txt'

workflows:
  update-text:
    jobs:
      - update-text

結論

- run: <command> || true

失敗する可能性があるステップのコマンドに対して true論理和を行う。

以下詳細。

シェルオプションのデフォルト値

ドキュメントやログを見ると分かるが、CircleCIの実行環境のLinuxで実行するステップのシェルオプションのデフォルト値は /bin/bash -eo pipefail となっている。

-o pipefail
pipefail を有効にすると、パイプラインの戻り値は、0 以外のステータスで終了した最後 (右端) のコマンドのステータス値か、すべてのコマンドが正しく終了した場合に 0 となります。 シェルは、パイプライン内のすべてのコマンドの終了を待って値を返します。
https://circleci.com/docs/ja/configuration-reference#default-shell-options

挙動的に後者の「すべてのコマンドが正しく終了した場合に 0 」が該当しそう。

参考