0

Requirement: I've an Java Project which reads excel and updates the same excel after validation. On the build run, it all happens. Now, I've to use the GitLab and Jenkins for it. My code is on GitLab on which I've configured the webhook to run the build.

Issue: After the build, the excel gets updated in Jenkins workspace but I want to push it to GitLab as well. If I do direct push from shell, it runs in loop always. So, I'm not to write the perfect shell for git commands. Can you please help in editing it.

I've tried putting the git commands in different conditions but nothing helped. Below is my shell

#!/bin/bash +x
echo =================== Starting Job =========================

git config user.name "Tarun"
git config user.email tarun.verma2710@gmail.com

state=`git status`
echo *******Status Start*********
echo ${state}
echo *******Status End*********

git pull origin master
git checkout master
git add .
git commit -m "Jenkins Checkin"

if [[ ${state} == *"no changes added to commit"* ]]; then
    echo "changes not present"
    #git pull origin master
else
    echo "changes present"
    git push -u --force origin master
fi

echo =================== Shutting Job ==========================

Actual output :

=================== Starting Job =========================
    2019-10-01 15:20:59 INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown initiated...
    *******Status Start*********
    # HEAD detached at a4c555f # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: spring-boot-automation-tool/excels/QuickActionImplExcel.xlsx # no changes added to commit (use "git add" and/or "git commit -a")
    *******Status End*********
    2019-10-01 15:20:59 INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown completed.
    From gitlab.corp.amdocs.com:TARUNVE/crm-bpt-automation-tool
     * branch            master     -> FETCH_HEAD
    Already up-to-date.
    error: Your local changes to the following files would be overwritten by checkout:
        spring-boot-automation-tool/excels/QuickActionImplExcel.xlsx
    Please, commit your changes or stash them before you can switch branches.
    Aborting
    [detached HEAD 85e27cd] Jenkins Checkin
     1 file changed, 0 insertions(+), 0 deletions(-)
     rewrite spring-boot-automation-tool/excels/QuickActionImplExcel.xlsx (99%)
    changes not present
=================== Shutting Job ==========================

Expected Output: I want to have smooth check-in , once the build is complete, it is updated in GitLab as well. And also check-in should happen to GitLab only if changes are present in Excel.

mnestorov
  • 4,116
  • 2
  • 14
  • 24
Tarun
  • 65
  • 4

1 Answers1

1

Your if statement seems to be reversed. *"no changes added to commit"* means there are changes present in repository. Note that you check status before performing operations on repository. I would solve this by reversing if statement, updating string comparison to detect expected file name and perform git operations only if changes were detected.

#!/bin/bash +x
state=$(git status)

if [[ ${state} == *"modified:"*"QuickActionImplExcel.xlsx"* ]]; then
    echo "changes present"
    git add .
    git commit -m "Jenkins Checkin"
    git push -u --force origin master
else
    echo "changes not present"
fi
makozaki
  • 3,772
  • 4
  • 23
  • 47