Action Workflow

  1. Go to Github repository, select "Actions", click "New Workflow", then click "Set up a workflow yourself". By default, github will create main.yml file in .github/workflows/ folder.
  2. Add the following config for automated testing and deployment:
    name: CICD
    
    on:
      push:
        branches: [main]
      pull_request:
        branches: [main]
    
    jobs:
      testing_and_build:
        runs-on: ubuntu-latest
        container:
          image: kirschbaumdevelopment/laravel-test-runner:8.1
    
        services:
          mysql:
            image: mysql:5.7
            env:
              MYSQL_ROOT_PASSWORD: password
              MYSQL_DATABASE: test
            ports:
              - 33306:3306
            options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
    
        steps:
          - uses: actions/checkout@v1
            with:
              fetch-depth: 1
    
          - name: Install composer dependencies
            run: |
              composer install --no-ansi --no-interaction --no-scripts --prefer-dist
    
          - name: Prepare Laravel Application
            run: |
              cp .env.ci .env
              php artisan key:generate
    
          - name: Run Testsuite
            run: vendor/bin/phpunit tests/
    
          - name: Install npm depedencies
            run: npm install
    
          - name: Build frontend
            run: npm run build
    
      deployment:
        needs: testing_and_build
        runs-on: ubuntu-latest
        steps:
          - name: Deploy using ssh
            uses: appleboy/ssh-action@master
            with:
              host: ${{ secrets.HOST }}
              username: ${{ secrets.USERNAME }}
              key: ${{ secrets.SSHKEY }}
              passphrase: ${{secrets.PASSPHRASE}}
              port: 22
              script: |
                whoami
                cd /www/wwwroot/cicd.redcomm.co.id/public-html/
                git reset --hard
                git pull origin main
                composer install --no-ansi --no-interaction --no-scripts --prefer-dist
                npm install --silent
                npm run build
    

    This script runs the workflow when there's a push or pull request on the main branch. To create a workflow for pushes to branches other than main, use:
    on:
      push:
        if: github.ref != 'refs/heads/main'
        //or
        branches:
         - '!main'
    
  3. Try pushing a commit to the repository. If you see a Task running in github actions, the main.yml script was successfully registered.
  4. Wait for the CI/CD process to complete. A successful CI process should look like this:
    cicd success
✨ Well done! Github Action successfuly created