GitLab.com + GitLab-CI + Shared runners + gulp + rsyncで自動デプロイを試みる
この辺の知識全くなかったので苦戦したけどなんとかできました。
わたしがgulp使ってたのでタイトルにgulpって書いたけど、gruntでもwebpackでもなんでも大丈夫です。
事前準備
- デプロイ先のサーバで公開鍵を作成してauthorized_keysに登録しておく
- Project Settings -> Features -> Buildsにチェックを入れる(多分デフォルトで入っているはず)
- Variablesに以下を登録
SSH_PRIVATE_KEY
: 秘密鍵SERVER_HOST
: ホストSERVER_USER
: 接続ユーザSERVER_DEST
: デプロイ先のパス
.gitlab-ci.yml
.gitlab-ci.ymlというファイルをリポジトリ直下に以下の内容で作成します。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
image: node:latest | |
cache: | |
paths: | |
- node_modules/ | |
before_script: | |
- apt-get update -y | |
- apt-get install rsync openssh-client -y | |
- npm install | |
stages: | |
- deploy | |
job_deploy: | |
stage: deploy | |
environment: production | |
only: | |
- master | |
script: | |
- npm run build | |
- eval $(ssh-agent -s) | |
- ssh-add <(echo "$SSH_PRIVATE_KEY") | |
- mkdir -p ~/.ssh | |
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' | |
- rsync -avz --delete ./public "${SERVER_USER}@${SERVER_HOST}:${SERVER_DEST}" | |
artifacts: | |
paths: | |
- public |
ファイルをpushすれば自動でデプロイが行われるはず。