Getting TravisCI to commit and push a modified file with Tags (releases)

孤街醉人 提交于 2019-12-03 16:20:09

I finally figured out my logic error and will explain it here in the hopes this will help someone in future. Thanks so much to Sir Athos for leading me in the right direction on all of this, big Thank You's Sir.

First I was doing the modification to a file and a commit in the before_deploy: section of travis.yml which led to TravisCI spinning into a continuous loop and just creating new builds all marked as untagged-randomnumbers

I solved this now by doing any modifications to files in my script: section of Travis.yml

So, considering this is just a testing container, I have a script called changefile.sh which look as follows:

#!/bin/bash
YEAR=$(date +"%Y")
MONTH=$(date +"%m")
git config --global user.email "${GIT_EMAIL}"
git config --global user.name "${GIT_NAME}"
git config --global push.default simple
export GIT_TAG=V2.$YEAR-$MONTH.$TRAVIS_BUILD_NUMBER
msg="Tag Generated from TravisCI for build $TRAVIS_BUILD_NUMBER"
echo "$msg" >> $TRAVIS_BUILD_DIR/build.txt
git add $TRAVIS_BUILD_DIR/build.txt
git commit -m "Update build version file with $TRAVIS_BUILD_NUMBER"

next I have a script called deploy.sh which looks as follows:

#!/bin/bash
YEAR=$(date +"%Y")
MONTH=$(date +"%m")
git config --global user.email "${GIT_EMAIL}"
git config --global user.name "${GIT_NAME}"
git config --global push.default simple
git remote add origin https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git
export GIT_TAG=V2.$YEAR-$MONTH.$TRAVIS_BUILD_NUMBER
git fetch --tags
msg="Tag Generated from TravisCI for build $TRAVIS_BUILD_NUMBER"
if git tag $GIT_TAG -a -m "$msg" 2>/dev/null; then
git tag $GIT_TAG -a -m "Tag Generated from TravisCI for build $TRAVIS_BUILD_NUMBER"
git push origin master && git push origin master --tags
ls -aR
else echo Tag already exists!; fi

And my travis.yml (shortened version) now looks like this:

language: php
sudo: required
dist: trusty
env:
  global:
    - secure: lotsofrandomnumbers
    - GIT_NAME: Travis CI
    - GIT_EMAIL: builds@travis-ci.org
    - TRAVIS_REPO_SLUG: mygitusername/myreposlug
    - GIT_BRANCH: master
matrix:
  fast_finish: true
php:
  - '5.6'
cache:
  - apt
install:
  # do some stuff here
script:
  # do some more stuff here
  - ./changefile.sh
before_deploy:
  - ./deploy.sh
deploy:
  provider: releases
  api_key:
    secure: ${GH_TOKEN}
  file:
  # add files to the release - specify them individually instead of a git add . or git add -A
  - "test.txt"
  skip_cleanup: true
  on:
    repo: mygitusername/myreposlug
    tags: false
    all_branches: true
notifications:
    email: false

This now achieves what I want 100% and does not lead to TravisCI spinning into a loop.

Travis now first does a build on my master branch, pushes the release out with tags and the modified files and then TravisCI will do a subsequent build (apparently very normal Travis behavior), in my case labelled in Travis with the V2.whatever version number, but relates to the very same commit number as the build on master.

What this does is run a second build test but not actually push any tags, commits or changes out so no more getting TravisCI into a continuous loop. You will see the second build end with a message "Skipping a deployment with the releases provider because this is not a tagged commit" whereas your first build will exit with a message saying "Deploying application"

You are adding the file in git, but not committing it before pushing:

msg="Tag...
echo "$msg" >> $TRAVIS_BUILD_DIR/build.txt
git add $TRAVIS_BUILD_DIR/build.txt
git commit -m "Update build version file"
if git tag ...

If you have multiple sub-builds that run this script, you should commit inside the if git tag, so you don't end up with multiple (unpushed) commits:

msg="Tag Generated from TravisCI for build $TRAVIS_BUILD_NUMBER"
if git tag $GIT_TAG -a -m "$msg" 2>/dev/null; then
    echo "$msg" >> $TRAVIS_BUILD_DIR/build.txt
    git add $TRAVIS_BUILD_DIR/build.txt
    git commit -m "Update build version file"
    git push ...
else ...

(Please also note that you are running git tag twice, once in the if statement and then again inside the if; there is no need to re-tag, the if condition also tags at the same time).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!