use bash case statement in gitlab ci/cd - bash

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.

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?

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;
}

Related

Push nuget package only if the package version matches the Tag on Git's master branch

In our development environment, we have set up a NuGet local server (BaGet). We have adopted the Gitflow idea. When a library is ready to be released on Baget, the developer should first increase the Tag on the master branch (which needs to be approved first via a pull-request), then push the library to the Baget. We do this to keep the version of Git and Nuget in sync.
The process of keeping versions in sync (Git tag & NuGet version) is controlled manually by the developer and sometimes some team members forget to define the Git version tag and just push the library to Baget.
It would be a great help if the script could check the Current Git Tag before pushing the library to the Baget server, and only push it if the Tag and Version are the same. This can prevent pushing a version without matching Tag on git.
We use this script for pushing to Baget:
#!/bin/bash
clear
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR
ostype=`uname`
KEY=$NUGET_KEY
SOURCE=$NUGET_URL
while :
do
clear
echo "Input your package version: "
read version
Common="Dayan.Common/bin/Debug/Dayan.Common."$version".nupkg"
dotnet nuget push $Common -s $SOURCE -k $KEY
echo "press enter to continue ..."
read
done
Can I somehow check use git commands in the bash to get the Tag of the last commit on the master branch of the project, and check it with the user input for version?
One way to make that check would be to use the git command rev-list.
This command will output the commit SHA of the most recent commit:
$ git rev-list -n 1 HEAD
dfe4a9989b33e97f25645d79fd62900cc3209ec7
While this command will output the commit SHA of the tag 3.1.5:
$ git rev-list -n 1 "3.1.5"
a35117a201290b63b53ba6372dbf8bbfc68f28b9
The following example script should get you started:
#!/bin/bash
echo "Input your package version: "
read version
last_commit=$(git rev-list -n 1 HEAD 2>/dev/null)
last_commit_result=$?
if [ "$last_commit_result" != "0" ]; then
echo "Failed to get the SHA of the most recent commit"
exit 1
fi
version_commit=$(git rev-list -n 1 "$version" 2>/dev/null)
version_commit_result=$?
if [ "$version_commit_result" != "0" ]; then
echo "There is no commit with the tag: $version"
exit 1
fi
if [ "$last_commit" = "$version_commit" ]; then
echo "The most recent commit has the tag: $version"
else
echo "The most recent commit does NOT have the tag: $version"
fi
If you also want to make sure the script is only run from master then add this near the script's start:
active_branch=$(git branch --show-current 2>/dev/null)
active_branch_result=$?
if [ "$active_branch_result" != "0" ]; then
echo "Failed to get the active branch"
exit 1
elif [ "$active_branch" != "master" ]; then
echo "The active branch is not master"
exit 1
fi

Automating git push and commit in a shell script on a Centos server

I am implementing a shell script that will be backing up the database and then push the sql file to Github, I am using centos server the project is located at /opt/server-scripts/backup.sh. How do I automate this?
Here is my implementation so far:
#!/bin/bash/
var=$CURRENT_DATE=date +"%D %T"
docker exec 3856a8e52031 /usr/bin/mysqldump -u root --password=cvxxx django_mysql_docker > backup.sql
# Git Push
GIT=$(which git)
REPO_DIR=/opt/server-scripts/
cd ${REPO_DIR} || exit
${GIT} add --all .
${GIT} commit -m "backup:" + "'$CURRENT_DATE'"
${GIT} https://pmutua:xxxxx#github.com/pmutua/sqlbackup.git master
You can check if a command/executable is installed or it is with in your PATH, one way using type
if type -P git >/dev/null; then
echo 'git is installed.'
fi
If you want to negate the result add the !
if ! type -P git >/dev/null; then
echo 'git is not installed.'
fi
To add that to your script.
#!/usr/bin/env bash
docker exec 3856a8e52031 /usr/bin/mysqldump -u root --password=cvxxx django_mysql_docker > backup.sql
if ! type -P git >/dev/null; then ##: Check if git is not installed
echo 'git is not installed.' >&2 ##: print an error message to stderr
exit 1 ##: Exit with an error
fi
# Git Push
CURRENT_DATE=$(date +"%D %T") ##: Assign the output of date in a variable
REPO_DIR=/opt/server-scripts/
cd "${REPO_DIR}" || exit
git add --all .
git commit -m "backup: '$CURRENT_DATE'"
git push https://pmutua:xxxxx#github.com/pmutua/sqlbackup.git master
You can add the date directly git commit -m "backup: '$(date +"%D %T")'"
that way the date will be same with the output of git log
Other ways to check if a command exists is via command and hash see Howto check if a program exists in my PATH

Why does my command fail even if I use `|| echo ""`

I run my bash script with -e
set -e
git push --delete sometag || echo "git delete tag failed"
echo "some other commands"
However git push returns a fatal error, and the rest of the script is not executed (it does execute echo "git delete tag failed")
I thought that was the purpose of using ||. Is a "fatal" error any different?
Note: I have seen other questions but they say that || is the solution

Bash Script that uses Git to Update Only Itself

I have a bash script that gets updated fairly often that I would like to have self-update only itself using git, but not affect anything else.
I found an Example Script that updates itself, but it uses git pull --force which updates everything. Most of the time this should be fine, but I hesitate to automatically do something with the potential to have unintended consequences, safer to just affect only itself.
My attempts to modify that script to use checkout or cherry-pick have not been successful.
Does anyone have a function that updates only $0 or can write one?
Edit:
This is the messy code I wrote for my script.
#!/bin/bash
BRANCH="master"
SCRIPTNAME=$1
REPOSITORY="https://stash.xxx/projects/IT/repos/xxx/browse/$SCRIPTNAME"
self_update() {
git fetch
if [[ -n $(git diff --name-only origin/$BRANCH | grep $SCRIPTNAME) ]]
then
echo The version you have and the version on stash are different
echo
echo Do you want to:
echo
echo s. Show messy differences
echo c. Open repository in Chrome
echo
echo d. Download the stash version, overwrite your current version, and exit script
echo
echo q. return to the previous menu
read choice
case $choice in
s)
git diff origin/$BRANCH
echo
read -p "Enter to Return " enterkey
;;
c)
open -a "/Applications/Google Chrome.app" "$REPOSITORY"
;;
d)
git checkout -f origin/$BRANCH -- $SCRIPTNAME
#head -5 $SCRIPTNAME
exit
;;
q)
break
;;
*)
echo Please enter one of the choices.
;;
esac
else
echo You are using the current version of $SCRIPTNAME
break
fi
}
#testing code
head -5 $SCRIPTNAME
while :
do
self_update
done
head -5 $SCRIPTNAME
Checkout should work
git fetch the-remote
git checkout the-remote/the-branch -- the-file.sh
This better not run on windows because it will reject rewrite the script while it's running.

Git Bash Script Check Working Tree

Is there a way in Git Bash to check if the working tree is clean, that is no uncommitted changes or untracked files?
I'm working on a bash script for my group to automate the process of daily rebasing working branches. Unclean working trees is a common problem. I can manually correct the problem by executing git checkout .. This would have the desired result most of the time, but not always, so I need to be able to have my script programatically check that the working directory/tree is clean.
The git-sh-setup script included with git contains a number of useful functions for working with git repositories. Among them is require_clean_work_tree:
require_clean_work_tree () {
git rev-parse --verify HEAD >/dev/null || exit 1
git update-index -q --ignore-submodules --refresh
err=0
if ! git diff-files --quiet --ignore-submodules
then
echo >&2 "Cannot $1: You have unstaged changes."
err=1
fi
if ! git diff-index --cached --quiet --ignore-submodules HEAD --
then
if [ $err = 0 ]
then
echo >&2 "Cannot $1: Your index contains uncommitted changes."
else
echo >&2 "Additionally, your index contains uncommitted changes."
fi
err=1
fi
if [ $err = 1 ]
then
test -n "$2" && echo >&2 "$2"
exit 1
fi
}
This is in addition to being able to check the output from git status --porcelain and/or git status -z if you need to be more specific about what the state currently is.

Resources