0
0

githubにpushしたらCircleCiでサーバーに「git pull」させる

Last updated at Posted at 2024-06-10

背景

githubに上げた後自動更新したかった。
リモートリポジトリにPush後自動デプロイをCircleCIを使ってやってみた
が...めちゃ時間かかったので備忘録として...

前提

  • githubのリポジトリとアクセストークン作成済み
  • CircleCIアカウント作成し、githubリポジトリと連携済み
  • サーバーにssh接続して「git pull」ができる

手順

サーバーに「git pull」するシェルスクリプト作成

「.git」と同階層に「deploy.sh」
中身↓

cd /"リポジトリがある場所" && sudo git pull https://"githubアカウント名":"githubアクセストークン"@github.com/"githubアカウント名"/"リモートリポジトリ名.git"

動かすことしか考えていないので今回はこの構成

シェルスクリプト動かしてみる

source deploy.sh

CircleCIでSSHキーの設定

CircleCIの「Project Settings」→「SSH Keys」→「Additional SSH Keys」で設定を追加してください。
ここはUIが変更されたりするのでこちらを参考にしてください。

config.ymlの設定

version: 2.1
jobs:
  deploy:
    machine:
      enabled: true
    steps:
      - add_ssh_keys:
          fingerprints:
            - ""
      - run: ssh -p "ポート" "ユーザー名"@"host" "さっき作成したdeploy.shがある場所"

workflows:
  version: 2
  deploy:
    jobs:
      - deploy:
          filters:
            branches:
              only: main # 人によってはmasterかな?

下記はapatchのデフォルトのhtml直下に sh ファイルを置いた際の例

- run: ssh -p "ポート" "ユーザー名"@"host" "/var/www/html/deploy.sh"

ymlの概要はこちら

動かしてみる

githubにプッシュしてみてCircleCIのstatusが「success」なら成功してるはず

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0