Add new server to GitLab AutoDeploy

From Apoyar Wiki
Revision as of 09:26, 1 May 2017 by 84.47.33.253 (talk) (Installing autostarting Git auto Deploy on Windows.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

If not mentioned otherwise, the following values are variables and will change from project to project (or server to server):

  • dev-server - Git branch name, usually following the name of the server which the files will be deployed to (can be uat-server or live-server, etc)
  • the /var/www/html path is just an example, this will be the root folder of the website we want to deploy to (from nginx/apache sites-enabled)
  • the https://gitlab.apoyar.eu/somepath will be the url to the project in question - to be obtained from GitLab


And now the steps:[edit]

Linux servers:[edit]

  • Install Git AutoDeploy
    • apt-get install software-properties-common
    • add-apt-repository ppa:olipo186/git-auto-deploy
    • apt-get update
    • apt-get install git-auto-deploy
  • copy the settings file from Dev: /etc/git-auto-deploy.conf.json
  • amend the file contents: url to access (GitLab project), active branch required
  • open the /etc/init.d/git-auto-deploy file and make the daemon running as www-data:
    • DAEMON_UID="www-data"
    • DAEMON_GID="www-data"
  • Run systemctl daemon-reload after you’ve made the changes above
  • A reboot may be required here
  • Make sure the Deploy Bot account has access to the project and is made a Master
  • Backup the current website contents: cp -R /var/www/html/ /home/neal/backup/
  • Delete the website contents, including the git part (if present):
    • cd /var/www/html
    • rm –rf *
    • rm –rf .git .gitignore .htaccess
  • Before you do the below process, you may need to check the owner of the /var/log/git-auto-deploy.log file. It should be www-data. Also check the /var/run/git-auto-deploy/git-auto-deploy.pid If not, change them:
    • chown www-data:www-data /var/log/git-auto-deploy.log
    • chown www-data:www-data /var/run/git-auto-deploy/git-auto-deploy.pid
  • Start the daemon in foreground, but as the user www-data: sudo -H -u www-data bash -c 'git-auto-deploy --pid-file /var/run/git-auto-deploy/git-auto-deploy.pid --log-file /var/log/git-auto-deploy.log --config /etc/git-auto-deploy.conf.json'
  • Once static, Ctrl-C the process
  • It should create the tree
  • Check the contents of the /var/www/html/.gitignore file
  • If not present, create one, own it by www-data and push back to the server. If present, leave out the 3 next steps
    • vi .gitignore
    • chown www-data:www-data .gitignore
    • git push origin dev-server
  • Copy the ignored files listed above from backup
  • Clear the cache and push changes to GitLab:
    • cd /var/www/html
    • git rm –rf –cached .
    • git add .
    • git status
    • if the above command says nothing to commit, you can leave out the 2 next steps
    • git commit
    • git push origin dev-server
  • Make sure all objects in the .git/objects folder are owned by www-data: chown –R www-data:www-data *
  • Change the Deploy Bot to be a Reporter in GitLab
  • Start the service: service git-auto-deploy start
  • Observe the log file: tail –f /var/log/git-auto-deploy.log
  • If you don’t see anything in the log and the foreground app runs fine, reboot the server
  • Add the http://hostname:8001 as webhook to GitLab project in question: https://gitlab.apoyar.eu/root/bleckmann/settings/integrations
  • Reboot the server and check whether it’s listening on port 8001: netstat -anp

Windows servers:[edit]

  • All installation files and prerequisites are in the \\ncc1701d\SourceFiles\Git network folder
  • Install the Windows Git client (Git-2.11.1-64-bit)
  • Install Python for Windows (pywin32-220.win-amd64-py2.7)
  • Reboot
  • Make sure you can run Python from command line without specifying the installation path (open CMD, then type python -v, you should get the python command prompt. Close it by typing 'quit()')
  • Make sure you can run Git from command line. Just type git in the CMD window
  • If the above two are not working, just check the Windows PATH environmental variable in the computer's properties
  • Now, installing GitAutoDeploy:
    • Open the Command Prompt as admin
    • cd to 'C:\inetpub\wwwroot'
    • run 'git clone https://github.com/olipo186/Git-Auto-Deploy.git'
    • cd to newly created subfolder: 'cd Git-Auto-Deploy'
    • run the Python requirements checker 'pip install -r requirements.txt' It will check and install all required Python files, if needed.
  • From the \\ncc1701d\SourceFiles\Git network path copy these files to the C:\inetpub\wwwroot\Git-Auto-Deploy folder:
    • gitautodeploy.bat
    • GitAutoDeploy.py
    • invisible.vbs
    • config.json
  • Please note that if the 'Hide extensions for known file types' option is ticked, the above 4 files will be visible without their extension!
  • Create a folder which will contain the IIS files. In this example I'll be using 'C:\inetpub\wwwroot\BleckmannAPItest'
  • You will point your new IIS website to the above folder.
  • Edit the config.json file according to your needs. Particularly change the:
    • GitLab url
    • Branch to manage (uat-server)
    • Local path for the files ('C:/inetpub/wwwroot/BleckmannAPItest')
      • Please note I'm using forwardslash instead of backslash. This is the way it needs to be entered in the config.json file!
  • Should you need to serve more than one project, please refer to the complex-config.json file in the same network folder.
  • Now get the initial copy of the website from GitLab:
    • start CMD and browse to your new empty folder: 'cd C:\inetpub\wwwroot\BleckmannAPItest'
    • run git clone https://root@gitlab.apoyar.eu/root/BleckmannAPI.git .
    • don't forget the ending dot after the url as this makes sure the files will be saved to the current folder
  • Check the contents of the folder
  • Add the http://hostname:8001 as webhook to GitLab project in question: https://gitlab.apoyar.eu/root/BleckmannAPI/settings/integrations
  • Again, the above url is just an example
  • As for the hostname, it's always good to check that the computer name (for example bleckmannapi.apoyar-networks.com) is pingable from Linuxweb, which is the machine GitLab runs on
  • Time for the console test:
    • Open CMD
    • copy and paste 'python C:\inetpub\wwwroot\Git-Auto-Deploy\GitAutoDeploy.py --config C:\inetpub\wwwroot\Git-Auto-Deploy\config.json'
  • You'll get the initialization messages and the script should stop at 'listening on'
  • Go to GitLab, project in question, settings, Integrations
  • Find the entry you have just added recently and hit Test
  • You should get the 'Hook executed succesfully' blue bar at the top.
  • Now the startup:
    • Open the registry editor: start, run, 'regedit'
    • Browse to the: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
    • Add a new 'Expandable String Value'
    • Name it 'GitAutoDeploy'
    • Copy and paste this value: "%SystemRoot%\System32\WScript.exe" "C:\inetpub\wwwroot\Git-Auto-Deploy\invisible.vbs" "C:\inetpub\wwwroot\Git-Auto-Deploy\gitautodeploy.bat"
    • Reboot the server and log on as administrator
    • The above is required as the python script won't otherwise run in the background
  • Test