Travis CI 跳过发布部署,因为这不是标记的提交

Travis CI skip release deployment because this is not a tagged commit

我正在尝试将自动化发布部署配置为使用 Travis CI 构建的 Github 发布。我的 .travis.yml 文件如下所示:

language: java
jdk: oraclejdk8

branches:
  only:
    - master

before_install: mvn package -DskipTests=true -DbuildNumber=$TRAVIS_BUILD_NUMBER

before_deploy:
  - git config --local user.name "$USER_NAME"
  - git config --local user.email "$USER_EMAIL"
  - export GIT_TAG=1.0.$TRAVIS_BUILD_NUMBER
  - git tag $GIT_TAG -a -m "Generated tag from TravisCI build $TRAVIS_BUILD_NUMBER"
  - git push origin $GIT_TAG

deploy:
  provider: releases
  api_key: $GITHUB_TOKEN
  file:
    - target/tweetsched-dto-1.0.$TRAVIS_BUILD_NUMBER.jar
  name: tweetsched-dto-1.0.$TRAVIS_BUILD_NUMBER
  skip-cleanup: true
  on:
    tags: true
    repo: Tweetsched/tweetsched-dto
    branches:
      only:
        - master

notifications:
  email:
    on_success: never
    on_failure: always

我想要的 - PR 合并到主分支 Travis CI 在 before_deploy 步骤中创建一个新标签,然后根据该标签创建新版本。但是当我测试它时,我总是在 Travis CI logs:

中收到一条消息

Skipping a deployment with the releases provider because this is not a tagged commit

没有任何关于它为什么不创建标签的消息。我究竟做错了什么?以及如何正确配置 Travis 以在从 Master 分支成功构建时发布新版本的工件?

要让 Travis 考虑标签,您需要设置 $TRAVIS_TAG env var。参见 https://docs.travis-ci.com/user/deployment/#conditional-releases-with-on

export TRAVIS_TAG=$GIT_TAG

我终于解决了问题,配置的最终版本如下所示:

language: java
jdk: oraclejdk8

branches:
  only:
    - master

before_install: mvn package -DskipTests=true -DbuildNumber=$TRAVIS_BUILD_NUMBER

before_deploy:
- export TRAVIS_TAG="1.0.$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/tweetsched-dto-1.0.$TRAVIS_BUILD_NUMBER.jar
  on:
    branch: master
    repo: Tweetsched/tweetsched-dto

notifications:
  email:
    on_success: never
    on_failure: always