Automate your webhook-configuration
I thought about how to optimize my project-creation-progress and wrote a little script for that.
In my article about GitLab-Webhooks, I wrote about how to simplify your deployprocess with a few simple steps. If someone pushes to a GitLab-project, a webhook will be run which updates your stage- or production-server.
In my solution I use a PHP-script, which reads a config-file to determine if the project which runs the webhook is a known one and if so, which branch has to be deployed.
I still like that solution, but I do not like, that I
- have to create the base-directory for a new project and set needed permissions all the time
- have to modify the deploy-configuration by myself.
Because, in my case, the deploy-config is the same most of the time (of course the path to and the name of the project differs), I tried to automate these steps.
addProject Bash-Script
So I wrote a little Bash-script, which helps me.
- It creates the base-directory of the project in the document-root.
- It modifies the deploy-config with standard-parameters and adds the correct path and project-title.
To prevent doublications and make sure the script does not overwrite exisiting files, the script checks if the new project already exists.
#!/bin/bash
cd /var/www/
if [ ! -d "$1" ]; then
mkdir -p "$1/logs"
chown YOURUSERNAME:www-data $1-$2
chmod -R 775 $1
cd /var/www/DEPLOYSCRIPTDIRECTORX/
head -n -2 deploy.json > temp.txt ; mv temp.txt deploy.json
printf "\t},\n\t\"$1\": {\n\t\t\"path\":\t\t\"/var/www/$1\",\n\t\t\"limit\":\t\"master\"\n\t}\n}" >> deploy.json
else
echo Project already exists!
fi
exit
How to run
To execute the script, you type:
/path/to/script.sh projectTitle
In my script there is a second parameter, which is a running number of the job. I want that to be part of the directory-name. I didn't include it in my sample here, but you can add more parameters by using the vars $2, $3, $4 …
The command above will create the directory projectTitle in /var/www.
How it works
First the script changes the current directory to the apache-document-root, that's /var/www for me, it may also be /srv/http for example. You should modify that for your needs in line 3 and 10 (I modified it a bit, because my client-works are in a subdirectory).
After that the script checks if the project-directory does exist. If so it will exit with a warning. If not, it will continue.
The directory will be created and the needed permissions will be set. The group of the directory will be set to www-data and the group gets write-access, so the deploy-script can write into the directory later.
If the deploy-script or your webserver runs as another user, you may want to change the group-name.
Then the script changes the directory of your deploy-configuration to extend it. First it will strip off the last two lines, which are the closing brackets of the json and of the last project.
Then the last projects gets its bracket back appended by a comma (so the next entry can follow).
After that the new entry will be written, the projectname and the path will be set. As default, the master-branch will be set for automatic deployment. If you want all branches to be deployed, you can change master to null or whatever branch you want to by deployed.
Finaly the closing bracket of the json is written.
So, with that the project ist set up and can be used. I put my script in my GitHub-Repository, hope it helps. If you have tips our questions feel free to comment below.