LoginSignup
3
2

More than 1 year has passed since last update.

GitHubActionsを使ってLaravelの変更を自動デプロイする

Last updated at Posted at 2022-04-13

ファイルをアップロードするときに毎回FFFTP使ったり、 sshつなげてgit pull origin masterしたりする手間を省きたいときにGitHubActionsやCircleCIは便利です。
今回はGitHubActionsで自動でプロイする仕組みをお伝えします。

Sakuraのレンタルサーバにアクセス制限設定を無効にする

・コントロールパネルから変更する

GitHubActionsの設定

・Githubにシークレットの設定(サーバ名、ユーザー、パスワード、アップロードするディレクトリ先) ※ファイルに直書きは見れてしまうので。

GithubActionsに登録

・FTP-Deploy-Action4.0.0を使います。
・Actions -> set up a workflow yourself に進みます。
・ymlファイルが開きますので、以下を参考に書きかえてください。とはいえ、ほとんど自動生成されるのでポイント箇所を重点的に見て書き換えます。

# This is a basic workflow to help you get started with Actions

name: CI

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

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# 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:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3

      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!

      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          echo Add other actions to build,
          echo test, and deploy your project.
      - name: 📂 Sync files
        uses: SamKirkland/FTP-Deploy-Action@4.0.0
        with:
          server: ${{ secrets.FTP_SAKURA_SERVER }} ※2
          username: ${{ secrets.FTP_SAKURA_USERNAME }} ※3
          password: ${{ secrets.FTP_SAKURA_PASSWORD }} ※4
          local-dir: ./ ※5
          server-dir: ${{ secrets.FTP_SAKURA_DIR }}  ※6

※1 ワークフローを起動させるブランチ名。mainかmasterかと。
※2 シークレットの登録サーバー(FTP_SAKURA_SERVERという名前で登録してるのでsecrets.FTP_SAKURA_SERVER)
※3 シークレットの登録ユーザー
※4 シークレットの登録パスワード
※5 server-dirは./ でよいかと。
※6 シークレットの登録ディレクトリ

実際のymlファイル →https://github.com/yutoonodera/tenjin/blob/master/.github/workflows/main.yml

プッシュしてみる

・対象リポジトリのソースをローカルで変更してpushします。

 Actionsにこのように表示されます(失敗したら赤マーク)
image.png
ソースを見たら変更がされてました(エビデンス示してなくてすいません)

※参考:
https://zenn.dev/mamezou/articles/23a221c7287cf6
https://zenn.dev/moai_san/articles/81a10cabe6de00

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