Easy CI/CD

Share this post on:

Git servers, provide features commonly named ‘Actions’ to handle CI/CD operations. Parallel solutions to achieve the requisites are available.

Linux has a built-in functionality to support repeating scheduled tasks (also known as ‘CronJobs’ in some refs) called crontab.

The deamon responsible for crontab will send a mail to user who triggered the action which may cause storage and mail transfer issues. Before all let’s disable this in case of no reason to keep all audit logs by setting the environment variables. Also there are alternative ways so far.

MAILTO=""

To redirect echo outputs to stderr instead of stdout, there is a clean method for this:

echoerr() { echo "$@" 1>&2; }

It is optional to prepend the line above to the cicd.sh file containing scripts below:

SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"

declare -a ProjectsArray=("project1" "project2" "project3")
for project in ${ProjectsArray[@]}; do

cd $SCRIPTPATH/d/$project
git fetch
if [[ $(git diff origin/master) ]];
then
	git pull
	sudo rm -rf bin/ obj/ publish/
	docker compose -f docker-compose.mssql.yml up -d
	docker compose up -d --build | mail -s "CI/CD $(date) $(pwd)" ubuntu
else
    echoerr "no changes in $(pwd)"
fi;

done

This will check project folders mentioned in the SCRIPTPATH array and will check their git status. If there are pending incoming changes, it will pass the condition, pull changes and rebuild the containers. This will keep the project updated with a low overhead.

Finally, using the crontab -e an editor will prepare a safe method to place codes below in crontab configuration.

* * * * * sudo su <USERNAME_HERE> -c /home/<USERNAME_HERE>/cicd.sh | wall

Finally, if mails are not disabled, it’s possible to publish last 500 lines of logs over the net using netcat with a simple web server.

while true; do { echo -e “HTTP/1.1 200 OK\r\n”; tail -n 500 /var/mail/<USERNAME_HERE>; } | nc -N -l <PORT_NUMBER_HERE>; done

Share this post on:

Author: tayyebi

Tayyebi works in the role of Director at Gordarg where he is the founder. He is passionate about people, technology, and arts. Mohammad believes in communications, each of us has the power to empower their people with knowledge. He can be seen writing codes, playing music, biking, and reading.

View all posts by tayyebi >






www.Gordarg.com