LoginSignup
2
2

More than 3 years have passed since last update.

GitHub ActionsでSSH経由でmasterブランチを本番サーバに反映したかった話

Posted at

初カキコ…ども…(ってもう古いですかね?see. 【追記あり】「初カキコ…ども…」10周年に投稿者が名乗り出る 「『りぼん』読み切りの台詞をパクった」→読んで真相を調べてみた - ねとらぼ

やりたかったこと

GitHubのリポジトリのmasterブランチにプッシュしたときに、masterブランチのデータを本番サーバに反映したかった…

サイトの概要

hokkaidosm.net プリキュアデータ昨日(2020/5/5)公開しました
このサイトの内容については実際にご覧いただければと思うのですが、内部的にはJSONでデータを管理して、GitHubのプライベートリポジトリでソースと合わせて管理しています。
サイトを動かしているサーバ(OSはCentos 7で、Apacheを使用)にはSSHの公開鍵認証で接続できるように設定済みです。ただし、セキュリティ上の理由から、標準の22番ポートではなく、別のポート番号を使用しています。

当初の計画

当初はmasterブランチの内容をSCPで送ろうとしていました。


当初の .github/workflows/deploy.yml
.github/workflows/deploy.yml
# This is a basic workflow to help you get started with Actions

name: deploy to main server

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [ master ]
  pull_request: # メモ:これがあるとmasterへのプルリクエスト作成時にも走ってしまったので、後で消しています
    branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    - uses: actions/checkout@master
    - name: copy file via ssh password
      uses: appleboy/scp-action@master
      with:
        host: ${{ secrets.HOST }}
        port: ${{ secrets.PORT }}
        username: ${{ secrets.USERNAME }}
        passphrase: ${{ secrets.PASSPHRASE }}
        key: ${{ secrets.KEY }}
        target: ${{ secrets.TARGET }}

ログ(一部)
Run appleboy/scp-action@master
  with:
    host: ***
    port: ***
    username: ***
    passphrase: ***
    key: ***
    target: ***
    timeout: 30s
    command_timeout: 10m
    rm: false
    strip_components: 0
    overwrite: false
    proxy_port: 22
    proxy_timeout: 30s
/usr/bin/docker run --name c27d31810048cf0d4d40eba4f86bc3868d5004_75e5c8 --label c27d31 --workdir /github/workspace --rm -e INPUT_HOST -e INPUT_PORT -e INPUT_USERNAME -e INPUT_PASSPHRASE -e INPUT_KEY -e INPUT_TARGET -e INPUT_PASSWORD -e INPUT_TIMEOUT -e INPUT_COMMAND_TIMEOUT -e INPUT_KEY_PATH -e INPUT_SOURCE -e INPUT_RM -e INPUT_STRIP_COMPONENTS -e INPUT_OVERWRITE -e INPUT_TAR_TMP_PATH -e INPUT_PROXY_HOST -e INPUT_PROXY_PORT -e INPUT_PROXY_USERNAME -e INPUT_PROXY_PASSWORD -e INPUT_PROXY_PASSPHRASE -e INPUT_PROXY_TIMEOUT -e INPUT_PROXY_KEY -e INPUT_PROXY_KEY_PATH -e HOME -e GITHUB_JOB -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_REPOSITORY_OWNER -e GITHUB_RUN_ID -e GITHUB_RUN_NUMBER -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e RUNNER_OS -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/precure/precure":"/github/workspace" c27d31:810048cf0d4d40eba4f86bc3868d5004
tar: empty archive
tar all files into /tmp/218750683/hkFvGwpi8Q.tar
exit status 1

方針転換1

にっちもさっちもいかなくなったのでサーバにSSH接続して git pull origin master するようにしてみましたが…。

このときの .github/workflows/deploy.yml
.github/workflows/deploy.yml
# This is a basic workflow to help you get started with Actions

name: deploy to main server

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    - name: Install SSH key
      uses: shimataro/ssh-key-action@v2
      with:
        key: ${{ secrets.KEY }}
        name: id_rsa # optional
        known_hosts: ${{ secrets.KNOWN_HOSTS }}
        config: ${{ secrets.CONFIG }}
    - name: Git pull on server
      run: ssh server "cd ${{ secrets.TARGET }} && git pull origin master"

このときのログ(一部)
Run ssh server "cd *** && git pull origin master"
  ssh server "cd *** && git pull origin master"
  shell: /bin/bash -e {0}
Warning: Permanently added the RSA host key for IP address '52.192.72.89' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
##[error]Process completed with exit code 1.

ララ「なんでルン!?」
ひかる「まあまあ」
→cf. 映画 スター☆トゥインクルプリキュア 星のうたに想いをこめて

現在の運用

サーバ側で git pull origin master をするスクリプトをたたくようにしました。
※下記では、SSH接続するユーザ名を hoge にしていますが、実際のユーザ名ではありません。
※下記では、HTMLルートフォルダを /var/www/hoge にしていますが、実際のパスではありません。また、このフォルダにはgit originの設定をしてあります。

.github/workflows/deploy.yml
# This is a basic workflow to help you get started with Actions

name: deploy to main server

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    - name: Install SSH key
      uses: shimataro/ssh-key-action@v2
      with:
        key: ${{ secrets.KEY }}
        name: id_rsa # optional
        known_hosts: ${{ secrets.KNOWN_HOSTS }}
        config: ${{ secrets.CONFIG }}
    - name: Git pull on server
      run: ssh server "/home/hoge/deploy_scripts/precure.sh"
/home/hoge/deploy_scripts/precure.sh
#!/bin/bash
eval `ssh-agent`
ssh-add ~/.ssh/id_rsa_github
cd /var/www/hoge
git pull origin master
echo "success"

ララ「これでプッシュしてみるルン!」
ひかる・ララ「…」

##[debug]Evaluating condition for step: 'Git pull on server'
##[debug]Evaluating: success()
##[debug]Evaluating success:
##[debug]=> true
##[debug]Result: true
##[debug]Starting: Git pull on server
##[debug]Loading inputs
##[debug]Loading env
Run ssh server "/home/***/deploy_scripts/precure.sh"
##[debug]/bin/bash -e /home/runner/work/_temp/059b3fee-62a7-4bb9-830f-df899c199f72.sh
Agent pid 28500
Identity added: /home/***/.ssh/id_rsa_github (/home/***/.ssh/id_rsa_github)
From github.com:Hokkaidosm/precure
 * branch            master     -> FETCH_HEAD
Updating 088d011..ab26902
Fast-forward
 sitepolicy/index.php | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
success
##[debug]Finishing: Git pull on server

ララ「できたルン!」
ひかる「キラやば~っ☆」

まとめ

ということでなんとかなったわけですが、ちょっと謎な挙動ばかりでしたので書いてみました。
誰かの役に立てればと思っています。

参考文献

いろいろさまよっていたため大量の記事を読みあさっていました。

2
2
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
2
2