如何为多部署配置只执行一次 Travis CI 'before_deploy' 步骤?

Gle*_*iko 5 continuous-integration github continuous-deployment continuous-delivery travis-ci

在我的项目中,我配置了 Travis CI 构建过程,该过程将新版本的工件发布到 Github 版本。我的.travis.yml文件:

language: java
jdk: oraclejdk8

branches:
  only:
    - master

before_install: mvn package

before_deploy:
  - export TRAVIS_TAG="1.$TRAVIS_BUILD_NUMBER"
  - echo "$TRAVIS_TAG" "$TRAVIS_COMMIT"
  - git config --local user.name "$USER_NAME"
  - git config --local user.email "$USER_EMAIL"
  - git tag "$TRAVIS_TAG" "$TRAVIS_COMMIT"
  
deploy:
  provider: releases
  tag_name: $TRAVIS_TAG
  target_commitish: $TRAVIS_COMMIT
  name: $TRAVIS_TAG
  overwrite: true
  skip_cleanup: true
  api_key: $GITHUB_TOKEN
  file_glob: true
  file:
    - target/my-artifact-$TRAVIS_TAG.jar
  on:
    branch: master

notifications:
  email:
    on_success: never
    on_failure: always
Run Code Online (Sandbox Code Playgroud)

我想添加将工件部署到 Heroku 的能力,为此我添加了第二个项目deploy,这个:

provider: heroku
api_key: $HEROKU_API_KEY
on:
  branch: master
Run Code Online (Sandbox Code Playgroud)

通过这些更改,Travis CI 配置的最终版本:

language: java
jdk: oraclejdk8

branches:
  only:
    - master

before_install: mvn package

before_deploy:
  - export TRAVIS_TAG="1.$TRAVIS_BUILD_NUMBER"
  - echo "$TRAVIS_TAG" "$TRAVIS_COMMIT"
  - git config --local user.name "$USER_NAME"
  - git config --local user.email "$USER_EMAIL"
  - git tag "$TRAVIS_TAG" "$TRAVIS_COMMIT"
  
deploy:
  - provider: releases
    tag_name: $TRAVIS_TAG
    target_commitish: $TRAVIS_COMMIT
    name: $TRAVIS_TAG
    overwrite: true
    skip_cleanup: true
    api_key: $GITHUB_TOKEN
    file_glob: true
    file:
      - target/my-artifact-$TRAVIS_TAG.jar
    on:
      branch: master
  - provider: heroku
    api_key: $HEROKU_API_KEY
    on:
      branch: master

notifications:
  email:
    on_success: never
    on_failure: always
Run Code Online (Sandbox Code Playgroud)

但是具有这种配置的构建失败并显示消息

致命:标签已经存在

命令 "git tag "$TRAVIS_TAG" "$TRAVIS_COMMIT"" 在执行过程中失败并以 128 退出

您的构建已停止。

结果 - 我看到新版本的工件已发布到 Github 版本,但部署到 Heroku 失败。我调查了这个问题,看起来 Travis CI 管道尝试before_deploy在每个之前执行步骤deploy,当它尝试执行它以部署到 Heroku 时,它失败了,因为具有此类名称的 Git 标记已经在before_deploy步骤中为deployGithub 版本创建。

如何解决问题并将 Travis CI 配置为before_deploy仅执行一次步骤?

Gle*_*iko 6

我能够使用if条件before_deploy逐步修复发布过程。如果TRAVIS_TAG变量已经存在,它将在执行第二次部署之前跳过标签的创建:

before_deploy:
  if ! [[ $TRAVIS_TAG ]]; then
    export TRAVIS_TAG="1.$TRAVIS_BUILD_NUMBER" &&
    git config --local user.name "$USER_NAME" &&
    git config --local user.email "$USER_EMAIL" &&
    git tag "$TRAVIS_TAG" "$TRAVIS_COMMIT";
  fi
Run Code Online (Sandbox Code Playgroud)