name: Staging CI/CD
on:
  push:
    branches: [ release-* ]
jobs:
  build-and-deploy:
    strategy:
      fail-fast: false
    runs-on: [ ubuntu-18.04 ]
    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      - uses: abatilo/[email protected]
      - uses: actions/checkout@v2
      - uses: jsdaniell/[email protected]
        with :
          name: "staging_conf.json"
          json: ${{ secrets.STAGING_CONFIG }}
      - uses: jsdaniell/[email protected]
        with :
          name: "staging_secret.json"
          json: ${{ secrets.STAGING_SECRET_KEY }}
      - uses: google-github-actions/setup-gcloud@master
        with:
          version: '290.0.1'
          service_account_key:  ${{ secrets.GCR_KEY }}
          project_id:  ${{ secrets.GCP_PROJECT_ID }}
      - run: |
          gcloud --quiet auth configure-docker
      - name: Poetry Export
        run: |
          poetry export -f requirements.txt --output requirements.txt --without-hashes
          cat requirements.txt
      - name: Build
        run: |
          docker build -f Dockerfile_staging --tag gcr.io/${{ secrets.GCP_PROJECT_ID }}/staging:`poetry version | sed 's/ /-/g'` .
      - name: Publish Container to GCR
        run: |
          docker push gcr.io/${{ secrets.GCP_PROJECT_ID }}/staging:`poetry version | sed 's/ /-/g'`
      - name: Deploy to staging server
        run: |
          gcloud run deploy ${{ secrets.STAGING_API }} \\
          --image gcr.io/${{ secrets.GCP_PROJECT_ID }}/staging:`poetry version | sed 's/ /-/g'` \\
          --region ${{ secrets.GCP_PROJECT_REGION }} \\
          --platform "managed" \\
          --quiet
name: Develop CI/CD

on:
  push:
    branches: [ develop ]
jobs:
  linting:
    runs-on: ubuntu-latest
    steps:
      #----------------------------------------------
      #       check-out repo and set-up python
      #----------------------------------------------
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
      #----------------------------------------------
      #        load pip cache if cache exists
      #----------------------------------------------
      - uses: actions/cache@v2
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip
          restore-keys: ${{ runner.os }}-pip
      #----------------------------------------------
      #          install and run linters #black . --check
      #----------------------------------------------
      - run: python -m pip install black flake8 isort
      - run: |
          black . --check
  unit-test:
    needs: [ linting ]
    strategy:
      fail-fast: false
      matrix:
        python-version: [ 3.7 ]
        poetry-version: [ 1.1.6 ]
        os: [ ubuntu-18.04 ]
    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Run image
        uses: abatilo/[email protected]
        with:
          poetry-version: ${{ matrix.poetry-version }}
      - name: Poetry install
        run: |
            poetry install --no-interaction
      - name : Mysql Container Build and Deploy
        run: |
            docker-compose -f "compose-files/githubaction-conf.yml" up -d --build
      - name: Wait / Sleep
        uses: jakejarvis/[email protected]
        with:
          time: '30s'
      - name : Start Pytest
        run: |
           poetry run pytest  --create-db --ds=tac.settings.docker_test
      - name : Stop Mysql Containers
        if: always()
        run: docker-compose -f "compose-files/githubaction-conf.yml" down

  build-and-deploy:
    needs: [ unit-test ]
    strategy:
      fail-fast: false
    runs-on: [ ubuntu-18.04 ]
    steps:
      - uses: abatilo/[email protected]
      - uses: actions/checkout@v2
      - uses: google-github-actions/setup-gcloud@master
        with:
          version: '290.0.1'
          service_account_key:  ${{ secrets.GCR_KEY }}
          project_id: 'admin-center-329701'
      - run: |
          gcloud --quiet auth configure-docker
      - name: Poetry Export
        run: |
          poetry export -f requirements.txt --output requirements.txt --without-hashes
          cat requirements.txt
      - name: Build
        run: |
          docker build -f Dockerfile_develop --tag "gcr.io/admin-center-329701/develop" .
      - name: Publish Container to GCR
        run: |
          docker push "gcr.io/admin-center-329701/develop"
      - name: Deploy to Develop Server
        run: |
          gcloud compute scp compose-files/dev-conf.yml compose-files/docker-compose.yml "devsmile@develop-admin-center-api:~" --zone "asia-northeast3-a"
      - name: Deploy
        run: |
          gcloud compute ssh "devsmile@develop-admin-center-api" --zone "asia-northeast3-a" -- 'docker system prune && docker-compose -f dev-conf.yml pull &&  docker-compose -f dev-conf.yml up -d'
name: PR

# workflow run이 언제 triggered 될 것인지에 대한 설정.
on:
  # master랑 develop 브랜치에 풀리퀘하면 workflow를 돌리겠음.
  pull_request:
    branches: [ develop, release-* ]
  push:
    branches: [ release-* ]
# workflow 정의.
jobs:
  linting:
    runs-on: ubuntu-latest
    steps:
      #----------------------------------------------
      #       check-out repo and set-up python
      #----------------------------------------------
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
      #----------------------------------------------
      #        load pip cache if cache exists
      #----------------------------------------------
      - uses: actions/cache@v2
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip
          restore-keys: ${{ runner.os }}-pip
      #----------------------------------------------
      #          install and run linters #black . --check
      #----------------------------------------------
      - run: python -m pip install black flake8 isort
      - run: |
          black . --check
 
  unit-test:
    needs: [ linting ]
    strategy:
      fail-fast: false
      matrix:
        python-version: [ 3.7 ]
        poetry-version: [ 1.1.6 ]
        os: [ ubuntu-18.04 ]
    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Run image
        uses: abatilo/[email protected]
        with:
          poetry-version: ${{ matrix.poetry-version }}
      - name: Poetry install
        run: |
            poetry install --no-interaction
      - name : Mysql Container Build and Deploy
        run: |
            docker-compose -f "compose-files/githubaction-conf.yml" up -d --build
      - name: Wait / Sleep
        uses: jakejarvis/[email protected]
        with:
          time: '30s'
      - name : Start Pytest
        run: |
           poetry run pytest  --create-db --ds=tac.settings.docker_test
      - name : Stop Mysql Containers
        if: always()
        run: docker-compose -f "compose-files/githubaction-conf.yml" down
test-js:
    name: QUnit Tests
    runs-on: ubuntu-latest
    if: ${{ github.repository == 'WordPress/wordpress-develop' || github.event_name == 'pull_request' }}

    steps:
      - name: Checkout repository
        uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f # v2.3.4

      - name: Log debug information
        run: |
          npm --version
          node --version
          git --version
          svn --version
      - name: Install NodeJS
        uses: actions/setup-node@38d90ce44d5275ad62cc48384b3d8a58c500bb5f # v2.2.2
        with:
          node-version: 14
          cache: npm

      - name: Log debug information
        run: |
          npm --version
          node --version
      - name: Install Dependencies
        run: npm ci

      - name: Run QUnit tests
        run: npm run grunt qunit:compiled

      - name: Ensure version-controlled files are not modified or deleted
        run: git diff --exit-code

  slack-notifications:
    name: Slack Notifications
    uses: WordPress/wordpress-develop/.github/workflows/slack-notifications.yml@trunk
    needs: [ test-js ]
    if: ${{ github.repository == 'WordPress/wordpress-develop' && github.event_name != 'pull_request' && always() }}
    with:
      calling_status: ${{ needs.test-js.result == 'success' && 'success' || needs.test-js.result == 'cancelled' && 'cancelled' || 'failure' }}
    secrets:
      SLACK_GHA_SUCCESS_WEBHOOK: ${{ secrets.SLACK_GHA_SUCCESS_WEBHOOK }}
      SLACK_GHA_CANCELLED_WEBHOOK: ${{ secrets.SLACK_GHA_CANCELLED_WEBHOOK }}
      SLACK_GHA_FIXED_WEBHOOK: ${{ secrets.SLACK_GHA_FIXED_WEBHOOK }}
      SLACK_GHA_FAILURE_WEBHOOK: ${{ secrets.SLACK_GHA_FAILURE_WEBHOOK }}
name: React build
on: 
  push:                               # master, develop Branch에서 push 이벤트가 일어났을 때만 실행
    branches:
      - master
      - develop

jobs:
  build:
    runs-on: ubuntu-18.04
    steps:
      - name: Checkout source code.   # 레포지토리 체크아웃
        uses: actions/checkout@v1
      
      - name: Cache node modules      # node modules 캐싱
        uses: actions/cache@v1
        with:
          path: node_modules
          key: ${{ runner.OS }}-build-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.OS }}-build-
            ${{ runner.OS }}-

      - name: Install Dependencies    # 의존 파일 설치
        run: npm install

      - name: Build                   # React Build
        run: npm run build

      - name: Archive production artifacts   # upload file
        uses: actions/upload-artifact@v1
        with:
          name: build                        # artifact name
          path: build                        # upload path

  deploy_to_dev:
    needs: build
    runs-on: ubuntu-18.04
    steps:
      - name: Download production artifacts  # download file
        uses: actions/download-artifact@v1
        with:
          name: build                        # artifact name

      - name: Deploy                  # S3에 배포하기
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        run: |
          aws s3 cp \\
            --recursive \\
            --region ap-northeast-2 \\
            build s3://github-actions-dh/develop # develop branch 디렉토리로 배포
    if: contains(github.ref, 'develop')  # github branch가 develop일 때만 develop_to_dev를 실행한다.

  deploy_to_prod:
    needs: build
    runs-on: ubuntu-18.04
    steps:
      - name: Download production artifacts  # download file
        uses: actions/download-artifact@v1
        with:
          name: build                        # artifact name

      - name: Deploy                  # S3에 배포하기
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        run: |
          aws s3 cp \\
            --recursive \\
            --region ap-northeast-2 \\
            build s3://github-actions-dh/master # develop branch 디렉토리로 배포
    if: contains(github.ref, 'master')  # github branch가 master일 때만 develop_to_prod를 실행한다.