Poor man’s continuous integration

I’m coming from the past, where FTP was enough for regular deployment. Those times are long gone, and we need to keep in shape with the younger developers who mostly work on the 13-inch notebooks from smoky cafe shops and train stations. It would be a shame for us that someone with those working habits be more productive than we. In our Star Trek offices, on our Thinkpads and 27-inch monitors

For this setup you’ll need the next ingredients:

  • Linux
  • Ansible on your host
  • Git (on both ends)
  • Gitlab / Github / Gitea or something similar

In this case, I’m deploying a .Net app so I made a service script that should be restarted after the files are replaced from Gitlab

The first step would be to add your public key to the remote host so you don’t need to enter a password to login. (as described here: LINK

Now create a folder where you will keep all the files:

mkdir /home/myuser/CICD && cd /home/myuser/CICD

Create file inventory.txt and add:

NAME ansible_host=HOST ansible_connection=ssh ansible_port=22 ansible_user=root

(replace HOST with actual host and NAME with your server name. Also if your ssh is not on the default port, replace the port 22 )

Create in the same dir the MYSERVICE.yaml file (for Ansible)

- hosts: all
  tasks:
 
    - name: Restart MYSERVICE
      service: name=MYSERVICE state=restarted

Replace the MYSERVICE with your service name

The finally create the shell script (MYSERVICE.sh)

#!/bin/bash
 
PWD="/home/myuser/WebSites/MyAppBuild"
 
echo " ====================== running command from" $PWD
cd $PWD
 
git add .
 
echo -n " ====================== commit message: "
read message
 
git commit -m "$message"
 
git push -u origin master
 
ssh MYREMOTE@HOST "cd /somedir/MyAppBuild && git pull origin master && exit"
 
ansible-playbook /home/myuser/CICD/MYSERVICE.yaml -i /home/myuser/CICD/inventory.txt

Of course, replace the path(s), save the file and chmod +x MYSERVICE.sh

When you build your project inside the /home/myuser/WebSites/MyAppBuild all you need to do is to run /home/myuser/CICD/MYSERVICE.sh

I hope this will help someone

Leave a Reply

Your email address will not be published. Required fields are marked *