0

so i have a pipeline that checks 2 repos every 24 hours and pushes changes to a 3rd repo. the pipeline looks like this:

- git  fetch $CHEATSHEETS
- git  fetch $BASH_ONELINER
- git add -A
- git commit -m "Update has been successful" || echo "error"
- git push $TOKEN_AND_REPO HEAD:master

my problem with that is i'm never going to see any error in the pipeline if there's an error like a merge conflict, for example. so i was thinking to use a case for comitting, something like this:

TEST=2; case "${TEST}" in "1") echo "one";; "2") echo "The commit has failed";; *) echo "Error";; esac

my question is would something like this work in gitlab ci/cd pipeline and if so - how should it be implemented?

thanks in advance.

kvelev
  • 53
  • 10
  • 1
    I don't understand the purpose of your code. If you always set `TEST` to 2, it would always produce an error message. – user1934428 Jan 04 '22 at 08:04
  • please, allow me to clarify. if you use git commit in a pipeline and there's nothing to be committed the pipeline would return an error: ERROR: Job failed: exit code 1 so, one way to resolve this is to use "OR" operator like in my example, however, this way you'd never really see what kind of error you get (should you get any). that's why I'm thinking about using the case statement and the code i've provided is just an example – kvelev Jan 04 '22 at 08:20
  • 1
    `to resolve this is to use "OR" operator like in my example, however, this way you'd never really see what kind of error you get` Using `||` does not hide the output, you still see the error that you got. – KamilCuk Jan 04 '22 at 08:23
  • Do you want to "see" what kind of error you are getting, or is your intention to execute a specific action depending on the kind of error you are getting? Could you clarify it? Why would you care what kind of error is that, and why do you use `||` then? Just remove the `||`, or just check if there are any changes before committing - if there aren't, then don't do it. https://stackoverflow.com/questions/3878624/how-do-i-programmatically-determine-if-there-are-uncommitted-changes – KamilCuk Jan 04 '22 at 08:27
  • 1
    @kvelev : Put such clarifications into the question, not in a comment. I also don't understand what you mean by _you'd never really see what kind of error you get_. `git` writes its error messages to stderr, so unless you hide stderr, you can see it. – user1934428 Jan 04 '22 at 08:44
  • i want the pipeline to fail only if there's an error with the commit such as a merge conflict, i don't want the pipeline to fail if there's nothing to be comitted which is the case now. the | | operator is used so i don't get a false-positive error. – kvelev Jan 04 '22 at 09:02

2 Answers2

1

You seem to want to do:

- git  fetch $CHEATSHEETS
- git  fetch $BASH_ONELINER
- git add -A
- if git diff-index --cached --quiet HEAD; then
     echo "Nothing to commit... exiting" &&
     exit 0
  fi
- git commit -m "Update has been successful"
- git push $TOKEN_AND_REPO HEAD:master

which is unrelated to using case. You can use case normally in gitlab-ci pipeline, just like in shell. See How do I programmatically determine if there are uncommitted changes?

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
0

You can try something like this, using a subshell or command groups:

git commit -m "Update has been successful" || {
  case $? in
    1) echo "one" ;;
    2) echo "commit failed" ;;
    *) echo "other error" ;;
  esac;
}
knittl
  • 246,190
  • 53
  • 318
  • 364