PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 27, 2022

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

 September 27, 2022     continuous-delivery, continuous-deployment, continuous-integration, github, travis-ci     No comments   

Issue

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?


Solution

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


Answered By - Hleb Kastseika
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing