Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute Travis CI 'before_deploy' step only once for multi-deploy configuration?

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?

like image 928
Gleb Avatar asked Jun 28 '18 09:06

Gleb


1 Answers

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
like image 123
Gleb Avatar answered Nov 06 '22 15:11

Gleb