从Jenkins远程签入到GitLab

问题描述 投票:0回答:1

需求:我有一个Java项目,该项目读取excel并在验证后更新相同的excel。在构建过程中,这一切都会发生。现在,我必须使用GitLab和Jenkins。我的代码在我已配置了Webhook来运行构建的GitLab上。

问题:构建后,Excel在Jenkins工作区中进行了更新,但我也想将其推送到GitLab。如果我直接从shell推送,它将始终循环运行。因此,我不是为git命令编写完美的shell。您能帮忙编辑吗?

我曾尝试将git命令置于不同的条件下,但无济于事。下面是我的外壳

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

git config user.name "Tarun"
git config user.email [email protected]

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 ==========================

实际输出:

=================== 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:我想要顺利签入,一旦构建完成,它也会在GitLab中更新。而且,仅当Excel中存在更改时,才应在GitLab上签入。

git jenkins gitlab checkin
1个回答
0
投票

您的if陈述似乎被颠倒了。*"no changes added to commit"*意味着存储库中存在更改。请注意,在对存储库执行操作之前,请检查状态。我将通过反转if语句,更新字符串比较以检测预期的文件名并仅在检测到更改的情况下执行git操作来解决此问题。

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

git config user.name "Tarun"
git config user.email [email protected]

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

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"
    #git pull origin master
fi

echo =================== Shutting Job ==========================
© www.soinside.com 2019 - 2024. All rights reserved.