当没有提交时防止 Gitlab 管道失败

Prevent Gitlab pipeline fail when nothing to commit

这条管道我已经实现了

send:
  image: node:latest
  before_script:
    - git config --global user.email "gituser@example.com"
    - git config --global user.name "Gitlab CI"
  script:
    - npm install
    - npm run start $GMAIL_PW
    - echo "Committing updated files to git"

    - git add executions.txt
    - git commit -m "Updated executions with new running date"
    - git push "https://${GITLAB_USER_NAME}:${CI_ACCESS_TOKEN}@${CI_REPOSITORY_URL#*@}" "HEAD:${CI_COMMIT_REF_NAME}" -o skip-ci
  only:
    - schedules

有时没有要提交的内容并且管道失败并出现此错误

$ git add executions.txt
$ git commit -m "Updated executions with new running date"
HEAD detached at 0fa2f4d
nothing to commit, working tree clean
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit code 1

当没有什么可提交时,如何防止管道失败?

您可以检查 git 状态,只有在有内容要提交时才提交和推送:

send:
  image: node:latest
  before_script:
    - git config --global user.email "gituser@example.com"
    - git config --global user.name "Gitlab CI"
  script:
    - npm install
    - npm run start $GMAIL_PW
    - |     
      CHANGES=$(git status --porcelain | wc -l)
      if [[ "${CHANGES}" -gt 0 ]]; then
        echo "Committing updated files to git"
        git add executions.txt
        git commit -m "Updated executions with new running date"
        git push "https://${GITLAB_USER_NAME}:${CI_ACCESS_TOKEN}@${CI_REPOSITORY_URL#*@}" "HEAD:${CI_COMMIT_REF_NAME}" -o skip-ci
      else
        echo "Nothing to commit"
      fi
  only:
    - schedules