In my project I has configured Travis CI build process which releases new versions of artifact to Github releases. My .travis.yml
file:
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
I wanted to add ability to deploy artifact to Heroku and for that I added second item to deploy
step, this one:
provider: heroku
api_key: $HEROKU_API_KEY
on:
branch: master
With these changes the final version of Travis CI config:
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
But builds with such configuration are failing with message
fatal: tag already exists
The command "git tag "$TRAVIS_TAG" "$TRAVIS_COMMIT"" failed and exited with 128 during
Your build has been stopped.
As result - I see that new version of artifact was released to Github releases, but deployment to Heroku was failed. I investigated the issue and it looks like Travis CI pipeline try to execute step before_deploy
before each deploy
, and when it try to execute it for deployment to Heroku it fail due to Git tag with such name was already created in before_deploy
step for deploy
to Github releases.
How can I fix the issue and configure Travis CI to execute before_deploy
step only once?
I was able to fix release process using if
condition in before_deploy
step. It will skip creation of tag before execution of second deploy if TRAVIS_TAG
variable is already exist:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With