メインコンテンツへスキップ
見出し画像

【GitHub Actions】LaravelのCI環境を作ってみた

    PullRequestを出した時に、自動でtestを実行して成功可否通知をslackに流すようにしたくて設定してみました。

    GitHubActions

    そもそもどういう仕組みで動いているのか、ホストとコンテナの関係 / 使い方などドキュメントを読んでから作業した方が良かったです。postgresへの接続ができない部分でつまづいて、DB_HOST定義を localhostで上書いていなかったことが原因だったのですが、仕組みをきちんと理解すればなんてことない部分でした。

    .github/workflows/continuous-integration.yml

    name: 'Continuous Integration'
    
    on:
     pull_request:
    
    jobs:
     #phpUnit 
     phpUnit:
       runs-on: ubuntu-latest
       services:
         postgres:
           image: postgres:12
           env:
             POSTGRES_DB: 'xxxxxxxxxx'
             POSTGRES_USER: 'xxxxxxxxxx'
             POSTGRES_PASSWORD: 'xxxxxxxxxx'
           ## needed because the postgres container does not provide a health-check
           options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
           ports:
             - 5432:5432
       env:
         ROOT_DIRECTORY: ./
         LARAVEL_DIRECTORY: ./laravel
         INCOMING_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_FOR_GITHUB_ACTION_NOTIFICATION }}
       steps:
         - uses: actions/checkout@v2
         - name: copy .env for laravel
           run: cp .laravel.local.env ./laravel/.env
           working-directory: ${{env.ROOT_DIRECTORY}}
         - name: composer install
           run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
           working-directory: ${{env.LARAVEL_DIRECTORY}}
         - name: key:generate
           run: php artisan key:generate
           working-directory: ${{env.LARAVEL_DIRECTORY}}
         - name: Directory Permissions
           run: chmod -R 777 storage bootstrap/cache
           working-directory: ${{env.LARAVEL_DIRECTORY}}
         - name: migrate add seed
           env:
             DB_HOST: localhost
           run: php artisan migrate:fresh --seed
           working-directory: ${{env.LARAVEL_DIRECTORY}}
         - name: cache node_modules
           id: node_modules_cache_id
           uses: actions/cache@v2
           with:
             path: ${{env.LARAVEL_DIRECTORY}}/node_modules
             key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
             restore-keys: ${{ runner.os }}-node-
         - name: npm install
           if: steps.node_modules_cache_id.outputs.cache-hit != 'true'
           run: npm install
           working-directory: ${{env.LARAVEL_DIRECTORY}}
         - name: npm run prod
           run: npm run prod
           working-directory: ${{env.LARAVEL_DIRECTORY}}
         - name: exec unit test
           env:
             DB_HOST: localhost
           run: php artisan test
           working-directory: ${{env.LARAVEL_DIRECTORY}}
         - name: slack notification success
           uses: tokorom/action-slack-incoming-webhook@main
           if: success()
           with:
             text: "Successfully"
             attachments: |
               [
                 {
                   "color": "good",
                   "author_name": "${{ github.actor }}",
                   "author_icon": "${{ github.event.sender.avatar_url }}",
                   "fields": [
                     {
                       "title": "GitHub Action Url",
                       "value": "${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }}"
                     },
                     {
                       "title": "Pull Request Title",
                       "value": "${{ github.event.pull_request.title }}"
                     },
                     {
                       "title": "Pull Request Url",
                       "value": "${{ github.event.pull_request.html_url }}"
                     }
                   ]
                 }
               ]
         - name: slack notification failed
           uses: tokorom/action-slack-incoming-webhook@main
           if: failure()
           with:
             text: "Failure"
             attachments: |
               [
                 {
                   "color": "danger",
                   "author_name": "${{ github.actor }}",
                   "author_icon": "${{ github.event.sender.avatar_url }}",
                   "fields": [
                     {
                       "title": "GitHub Action Url",
                       "value": "${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }}"
                     },
                     {
                       "title": "Pull Request Title",
                       "value": "${{ github.event.pull_request.title }}"
                     },
                     {
                       "title": "Pull Request Url",
                       "value": "${{ github.event.pull_request.html_url }}"
                     }
                   ]
                 }
               ]

    GitHubActionsの結果をslackに通知する

    いくつ通知するためのリポジトリはあるのですが、使い方がシンプルなのが気に入って採用しています。記述が若干冗長になったのですが、そんなにコードを触ることもないので良しとしています。

    slack通知キャプチャ

    誰のどのPullRequestだったか一目でわかるように、
    シンプルな通知内容にしています。

    スクリーンショット 2021-07-04 12.41.04

    cache node_modules

    サードパティを利用しなくてもcacheできる機能が用意されています。composerは現時点で数秒でinstallが完了しているため、cacheを利用しているのは、node_modulesだけにしています。


    あなたへのおすすめ