A requirement

  • Basic understanding of Linux, Docker, and CI/CD.
  • GitLab account (free plan is ok).
  • A Linux server (not the root user) that has SSH access permission. I use Ubuntu 16.04 LTS with LAMP technology stack.
  • Lightweight Docker image with SSH and LFTP.





  • You have logged into GitLab
  • You are the owner of a project/repository
  • You can pull and push the repo from your local machine through Git



GitKraken


About GitLab CI/CD

You can build up to 2,000 minutes per month





“GitLab.com
Shared RunnersIn order to
Auto zoom modeRun, powered by DigitalOcean. Automatic scaling means minimizing the wait time to start builds and creating isolated virtual machines for each project to maximize security.”


— description from GitLab documentation

Create SSH keys for GitLab runner

Pay attention to






password-less
The RSA key is 2048 bytes


$ ssh-keygen rsa -b 2048
Copy the code



man ssh-key


$ ssh-copy-id -i /path/to/key user@host
Copy the code





$ ssh -i /path/to/key user@host
Copy the code





Choose Dockerfile

Alpine
Lightweight image (about 8Mb)
Dockerfile


Pipleline configuration

GitLab website








Image: jimmyadaro/gitlab-ci-cd:latest Deploy: stage: Deploy only: - 'master' when: manual allow_failure: false before_script: SSH directory -mkdir -p ~/. SSH #Save the SSH private key -echo "$SSH_PRIVATE_KEY" > ~/. SSH /id_rsa -chmod 700 SSH -chmod 600 ~/. SSH /id_rsa -eval $(ssh-agent-s) -ssh-add ~/. SSH /id_rsa script: #Backup everything in /var/www/html/ -- SSH -o StrictHostKeyChecking=no -i ~/. SSH /id_rsa $USERNAME@$HOST "zip -q -r /var/backups/ WWW /01-Deploy-$(date +%F_%H-%M-%S).zip /var/www/ HTML/" #Deploy new files to /var/www/ HTML -lftp -d -u $USERNAME, -e 'set SFTP :auto-confirm true; Set SFTP :connect-program "SSH -a x -i ~/. SSH /id_rsa"; Mirror-rnev./ /var/ WWW/HTML -- ignore-time -- exclude -- glob. git* -- exclude. Exit 'SFTP: / / $HOST - rm - f ~ /. SSH/id_rsa -' echo the Deploy done: $(date "H + % f % : % M: % S") 'Copy the code





image: jimmyadaro/gitlab-ci-cd:latest
Copy the code



Don’t forget to install OpenSSH and LFTP for the image


Deploy:
Copy the code





stage: deploy
Copy the code





Only: - 'master'Copy the code



master
development
wip
master


when: manual
Copy the code





allow_failure: false
Copy the code





before_script: SSH directory -mkdir -p ~/. SSH #Save the SSH private key -echo "$SSH_PRIVATE_KEY" > ~/. SSH /id_rsa -chmod 700 SSH -chmod 600 ~/. SSH /id_rsa -eval $(ssh-agent-s) -ssh-add ~/. SSH /id_rsaCopy the code



before_script












script: #Backup everything in /var/www/html/ -- SSH -o StrictHostKeyChecking=no -i ~/. SSH /id_rsa $USERNAME@$HOST "zip -q -r /var/backups/ WWW /01-Deploy-$(date +%F_%H-%M-%S).zip /var/www/ HTML/" #Deploy new files to /var/www/ HTML -lftp -d -u $USERNAME, -e 'set SFTP :auto-confirm true; Set SFTP :connect-program "SSH -a x -i ~/. SSH /id_rsa"; Mirror-rnev./ /var/ WWW/HTML -- ignore-time -- exclude -- glob. git* -- exclude. Exit 'SFTP: / / $HOST - rm - f ~ /. SSH/id_rsa -' echo the Deploy done: $(date "H + % f % : % M: % S") 'Copy the code





- SSH -o StrictHostKeyChecking=no -i ~/. SSH /id_rsa $USERNAME@$HOST "zip -q -r /var/backups/www/01-Deploy-$(date) H - F_ + % % % M - % S) zip/var/WWW/HTML/"Copy the code



Note: You need to install ZIP CLI on your server.






/var/www/html


- LFTP -d -u $USERNAME, -e 'set SFTP :auto-confirm true; Set SFTP :connect-program "SSH -a x -i ~/. SSH /id_rsa"; Mirror-rnev./ /var/ WWW/HTML -- ignore-time -- exclude -- glob. git* -- exclude. The exit 'SFTP: / / $HOSTCopy the code



mirror -Rnev ./ /var/www/html
. /
/var/www/html


  • -uSet us upsftp://$HOSTSSH user name of the
  • -eThis command is used to set the execution command (using single quotation marks).
  • -RUsed to set the Reverse mirror.
  • -nIndicates that only new files are uploaded.
  • -eUsed to delete files that do not exist in our source.
  • -vThis command is used to configure verbose logs.
  • ignore-timeTime will be ignored when deciding whether to download.
  • exclude-glob .git*Any directory matches will be excluded.git*All files (e.g.gitignoreAs well as.gitkeep). You can set other file matching methods here.
  • exclude .git/This configuration will ensure that no Git files in our repo are uploaded.
  • exitLFTP and SSH execution will be stopped.



Note: all files on our service that are not in our Repository will be deleted. Remember that the ‘source’ mentioned above refers to our Repository.








- the rm - f ~ /. SSH/id_rsa - 'echo the Deploy done: $(date "H + % f % : % M: % S")'Copy the code










Run the Docker image










Final state of Pipeline


conclusion






Original link:Build a CI/CD Pipeline with Docker and GitLab(Translated by Xiao Yuanhao)