I wrote a bash script to run a branch cleanup for a repository, which will consider the branches which are not used in 3 months and send an email to the branch owner. In the script I wrote there is a small function which gets the branch name and the last committed date using git commands. Tis is how it looks like,
get_active_branches(){
declare -ag active_branches=()
echo -e "e[1;32m Scanning all active branches... e[0m"
for branch in `git branch -r | grep -v HEAD`; do
branch_data=$(git show --format=%cs "$branch" | head -n 1)
active_branches+=("$branch_data $branch")
done
echo -e "e[1;32m Done. e[0m"
}
I am running this in a docker image, created using osixia/openldap as the base image. I’m using that image because I need to have access to an active directory in a ldap server to get the email address by providing the username of a developer.
The issue is that when I run the above function in a bitbucket pipeline, for the command branch_data=$(git show --format=%cs "$branch" | head -n 1)
the branch_data
variable get the value as %cs
, for an example,
git show --format=%cs master
branch_data=%cs
The Dockerfile I’m using is lokks like this,
FROM osixia/openldap
RUN apt-get update && apt-get install -y tar xz-utils git openssl bash curl jq gcc libc-dev vim coreutils &&
rm -rf /var/lib/apt/lists/*
# Install kubectl
RUN apt-get update && apt-get install -y util-linux bash fortune sed findutils openssh-client git jq openssl curl &&
curl -L https://storage.googleapis.com/kubernetes-release/release/v1.21.0/bin/linux/amd64/kubectl > /usr/bin/kubectl &&
chmod +x /usr/bin/kubectl
# Add actual scripts to /bin
ADD bb_cleanup_new.sh /bin/
bb_cleanup_new.sh is the script file which has the function.
Any suggestions to fix this issue.
I tested the script locally and everything works fine.
I thought maybe it’s the issue with the branch name having the prefix of origin/
, so I tested after removing the prefix and running a bitbucket pipeline but it still gives the same result.
It only gives the issue when I’m using the image.
2
Answers
I found out what the issue was, it was
%cs
it doesn't give me a value when I'm running in the pipeline, and when I use something else like%ci, %cr, %ae
it gives me the respective value, so I used%ci
and did some string manipulation with AWK to get the date. Everything works fine now.git requires access to the .git directory of your repository. Otherwise it won’t be able to show you any information specific to your repository.
You can mount the git repo during the runtime of your container.
https://docs.docker.com/engine/reference/commandline/run/#volume
If you add the git repository to your Dockerimage, than it would also be possible to access the data from it.
Also be aware, that your docker context would also require all credentials that are to be used to access remote repositories.
(~/.ssh, global git config, git credential cache etc.)