2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

hetemlというレンタルサーバーにLaravelをDeployerを使ってGitHubActionsでCI/CDしたときの設定

Posted at

テストを実施した後にDeployerを使ってデプロイを実施する。

.github/workflows/main.yml
name: UnitTest

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  laravel-tests:
    name: phpunit test
    runs-on: ubuntu-latest
    
    services:
      mysql: 
        image: mysql:8.0.19
        ports:
          - 3306:3306
        env:  
          MYSQL_ROOT_PASSWORD: "password"
          MYSQL_DATABASE: test_db
          MYSQL_USER: user
          MYSQL_PASSWORD: "password"
        options: --health-cmd "mysqladmin ping -h localhost" --health-interval 20s --health-timeout 10s --health-retries 10
   
    env:
      DB_CONNECTION: mysql
      DB_HOST: db
      DB_PORT: 3306
      DB_DATABASE: test_db
      DB_USERNAME: user
      DB_PASSWORD: "password"
    
    steps:
      - uses: actions/checkout@v2
      - name: Setup PHP 7.4
        run: sudo update-alternatives --set php /usr/bin/php7.4
        
      - name: cache vendor 
        id: cache
        uses: actions/cache@v1
        with:
          path: ./vendor
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-
            
      - name: composer install
        shell: bash -xe {0}
        if: steps.cache.outputs.cache-hit != 'true'
        run: composer install -n --prefer-dist
        
      - name: composer dump autoload
        shell: bash -xe {0} 
        run: composer dump-autoload
        
      - name: generate key
        shell: bash -xe {0} 
        run: php artisan key:generate --env=testing
        
      - name: migrate
        shell: bash -xe {0} 
        env:
          DB_HOST: 127.0.0.1
        run: php artisan migrate --force
        
      - name: unit test
        shell: bash -xe {0} 
        env:
          DB_HOST: 127.0.0.1
        run: ./vendor/bin/phpunit

 
  deploy:
    name: deploy
    runs-on: ubuntu-latest
    needs: laravel-tests
    if: github.ref == 'refs/heads/main'

    steps:
      - uses: actions/checkout@v2
      - name: Setup PHP 7.4 
        run: sudo update-alternatives --set php /usr/bin/php7.4
        
      - name: cache vendor
        id: cache
        uses: actions/cache@v1
        with:
          path: ./vendor
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-
            
      - name: composer install
        if: steps.cache.outputs.cache-hit != 'true'
        run: composer install -n --prefer-dist
          
      - name: setup ssh
        run: | 
          mkdir -p /home/runner/.ssh
          touch /home/runner/.ssh/id_rsa
          echo "${{ secrets.SSH_DEPLOY_KEY }}" > /home/runner/.ssh/id_rsa
          chmod 600 /home/runner/.ssh/id_rsa
        
      - name: composer require deployer/deployer
        run: composer require deployer/deployer --dev
        
      - name: deploy
        run: ./vendor/bin/dep deploy production -vvv
2
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?