

- 
對Linux、Docker以及CI/CD有基本的瞭解。 
- 
GitLab帳號(免費計劃即可)。 
- 
一臺具備SSH訪問許可權的Linux伺服器(非root使用者即可)。我使用的是帶有LAMP[1]技術棧的Ubuntu 16.04 LTS系統。 
- 
裝有SSH和LFTP[2]的輕量級Docker映象。 
- 
你已經登入GitLab 
- 
你是某個project/repository的擁有者 
- 
你能夠在本地機器透過Git訪問這個repo進行pull和push操作 
我用的是GitKraken[3],一個Git GUI工具,能夠較為方面的進行Git操作。


$ ssh-keygen rsa -b 2048
$ ssh-copy-id -i /path/to/key user@host
$ ssh -i /path/to/key user@host


image: jimmyadaro/gitlab-ci-cd:latest
Deploy:
 stage: deploy
 only:
 — ‘master’
 when: manual
 allow_failure: false
 before_script:
 #Create .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 .git/; exit’ sftp://$HOST
 — rm -f ~/.ssh/id_rsa
 — ‘echo Deploy done: $(date “+%F %H:%M:%S”)’
image: jimmyadaro/gitlab-ci-cd:latest
Deploy:
stage: deploy
only:
 — ‘master’
when: manual
allow_failure: false
before_script:
 #Create .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 .git/; exit’ sftp://$HOST
 — rm -f ~/.ssh/id_rsa
 — ‘echo Deploy done: $(date “+%F %H:%M:%S”)’
— 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/”— 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 .git/; exit’ sftp://$HOST
- 
-u設定了我們sftp://$HOST的SSH使用者名稱。 
- 
-e用於設定執行命令(使用單引號進行配置)。 
- 
-R用於設定reverse mirror。 
- 
-n表示只上傳新的檔案。 
- 
-e用於刪除在我們源中不存在的檔案。 
- 
-v用於配置verbose日誌。 
- 
ignore-time將在決定是否下載時忽略時間。 
- 
exclude-glob .git`將會排除任何目錄中匹配`.git的所有檔案(例如.gitignore以及.gitkeep)。你可以在這裡設定其他檔案匹配方式。 
- 
exclude .git/這個配置將會保證不上傳我們repo中的git檔案。 
- 
exit將會停止LFTP和SSH執行。 
— rm -f ~/.ssh/id_rsa
 — ‘echo Deploy done: $(date “+%F %H:%M:%S”)’



- 
https://en.wikipedia.org/wiki/LAMP_%28software_bundle%29 
- 
https://lftp.yar.ru/ 
- 
https://www.gitkraken.com/ 
- 
https://hub.docker.com/r/jimmyadaro/gitlab-ci-cd/ 
- 
https://docs.gitlab.com/ce/ci/yaml/ 

 知識星球
知識星球
