Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I was helped by Sir Athos (thank you greatly) earlier today on a separate question which helped me greatly.

I am now stuck at a point where I want to modify a simple text file and include it in my push. So basically I want to add the modified text file to a commit and push it as a commit with it's build number tag as a release. Hope I am making sense here.

My code to do try and do this so far is

#!/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
git fetch --tags
msg="Tag Generated from TravisCI for build $TRAVIS_BUILD_NUMBER"
echo "$msg" >> $TRAVIS_BUILD_DIR/build.txt
git add -A
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 https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG} HEAD:master && git push https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG} --tags HEAD:master
ls -aR
else echo Tag already exists!; fi

The if ... else ... code given to me by Sir Athos earlier works 100% with it picking up if the tag exists and then either pushing if it does not exist or not pushing it it does exist.

I'm just stuck with now getting Travis to include the simple build.txt file which I echo $TRAVIS_BUILD_DIR into with an append.

What's interesting is if I add to my travis.yml deploy option like this:

file: - build.txt

Travis pushes the build.txt file with the release but is not committing the file. I know I'm doing and missing something really stupid here but go easy I'm new to Travis.

Update:

Now I have modified my bash script as follows, removing adding the extra tag within the if statement. This now works and pushes the modified build.txt file to the repo. But once complete Travis then starts other builds with untagged-cc6ebe6dbcbb13bc599c and now it's throwing Travis into a loop just continuing to build and build and build. I think Travis will drive me mad yet, I know I have some logic wrong somwehere.

    #!/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
    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
    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"
$TRAVIS_BUILD_NUMBER"
    git push https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG} HEAD:master && git push https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG} --tags HEAD:master
    ls -aR
    else echo Tag already exists!; fi

I think I must re-look at this with my travis.yml in the morning and work on doing things before_deploy and then use deploy to do the final push. Think I have figured out why I am causing Travis to loop non-stop. Have disabled it for tonight so Travis doesn't go mental all night long.

like image 703
MitchellK Avatar asked Dec 14 '22 00:12

MitchellK


1 Answers

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: [email protected]
    - 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"

enter image description here

like image 184
MitchellK Avatar answered Jan 13 '23 12:01

MitchellK