name: Django CI - Develop 

on:
  pull_request:
    branches: [ test ]
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
      #----------------------------------------------
      - run: python -m pip install black flake8 isort
      - run: |
          black . --check

  #   strategy:
  #     fail-fast: false
  #     matrix:
  #       python-version: [ 3.7 ]
  #       poetry-version: [ 1.0, 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
  #           poetry run python manage.py test

  
name: PR

# workflow run이 언제 triggered 될 것인지에 대한 설정.
on:
  # master랑 develop 브랜치에 풀리퀘하면 workflow를 돌리겠음.
  pull_request:
    branches: [ master, develop ]
    
# workflow 정의.
jobs:
  build-and-test:
    
    runs-on: macos-latest
    
    steps:
    # step 1
    - name: Checkout source code
      uses: actions/checkout@v2
      
    # step 2
    - name: Select Xcode
      run: sudo xcode-select -switch /Applications/Xcode.app

    # step 3
    - name: Build and test
      run: xcodebuild test -workspace GithubActionTest.xcworkspace -scheme 'GithubActionTest' -destination 'platform=iOS Simulator,name=iPad Pro (9.7-inch)'
      
    # step 4 if step 3 failed
    # github.ref는 예를 들어 refs/pull/4/merge 이런 값임.
    # await은 자바스크립트 비동기 패턴이라고 함. 
    - name: pr close if test failed
      uses: actions/[email protected]
      with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            const ref = "${{github.ref}}"
            const pull_number = Number(ref.split("/")[2])
            
            await github.pulls.createReview({
              ...context.repo,
              pull_number,
              body: "테스트코드를 다시 확인해주세요.",
              event: "REQUEST_CHANGES"
            })
            
            await github.pulls.update({
              ...context.repo,
              pull_number,
              state: "closed"
            })
            
      # 이전 step이 실패했을 경우에만 이 step을 실행한다
      if: failure()
name: Build and Push to GCR

on:
  push:
    tags:
      - v*

# Environment variables available to all jobs and steps in this workflow
#  GKE_EMAIL: ${{ secrets.GKE_EMAIL }} 
#  GKE_KEY: ${{ secrets.GKE_KEY }} 
env:
  GITHUB_SHA: ${{ github.sha }} 
  GITHUB_REF: ${{ github.ref }} 
  IMAGE: [IMAGE_NAME]
  REGISTRY_HOSTNAME: gcr.io

jobs:
  setup-build-publish-deploy:
    name: Setup, Build, and Publish
    runs-on: ubuntu-latest
    steps:

    - name: Checkout
      uses: actions/checkout@v2

    # Setup gcloud CLI
    - uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
      with:
        version: '270.0.0'
        service_account_key: ${{ secrets.GCR_KEY }} 

    # Configure docker to use the gcloud command-line tool as a credential helper
    - run: |
        # Set up docker to authenticate
        # via gcloud command-line tool.
        gcloud auth configure-docker

    # Build the Docker image
    - name: Build
      run: |
        export TAG=`echo $GITHUB_REF | awk -F/ '{print $NF}'`
        echo $TAG
        docker build -t "$REGISTRY_HOSTNAME"/"$IMAGE":"$TAG" \\
          --build-arg GITHUB_SHA="$GITHUB_SHA" \\
          --build-arg GITHUB_REF="$GITHUB_REF" .

    # Push the Docker image to Google Container Registry
    - name: Publish
      run: |
        export TAG=`echo $GITHUB_REF | awk -F/ '{print $NF}'`
        echo $TAG
        docker push "$REGISTRY_HOSTNAME"/"$IMAGE":"$TAG"
        docker tag "$REGISTRY_HOSTNAME"/"$IMAGE":"$TAG" "$REGISTRY_HOSTNAME"/"$IMAGE":latest
        docker push "$REGISTRY_HOSTNAME"/"$IMAGE":latest

# Copyright 2020 Google, LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    <http://www.apache.org/licenses/LICENSE-2.0>
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: Build and Deploy to Google Compute Engine

on:
  push:
    branches:
      - master

env:
  PROJECT_ID: $
  GCE_INSTANCE: my-githubactions-vm # TODO: update to instance name
  GCE_INSTANCE_ZONE: us-central1-a # TODO: update to instance zone

jobs:
  setup-build-publish-deploy:
    name: Setup, Build, Publish, and Deploy
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      # Setup gcloud CLI
      - uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
        with:
          version: '290.0.1'
          service_account_key: $
          project_id: $

      # Configure Docker to use the gcloud command-line tool as a credential
      # helper for authentication
      - run: |-
          gcloud --quiet auth configure-docker
      # Build the Docker image
      - name: Build
        run: |-
          docker build --tag "gcr.io/$PROJECT_ID/$GCE_INSTANCE-image:$GITHUB_SHA" .
      # Push the Docker image to Google Container Registry
      - name: Publish
        run: |-
          docker push "gcr.io/$PROJECT_ID/$GCE_INSTANCE-image:$GITHUB_SHA"
      - name: Deploy
        run: |-
          gcloud compute instances update-container "$GCE_INSTANCE" \\
            --zone "$GCE_INSTANCE_ZONE" \\
            --container-image "gcr.io/$PROJECT_ID/$GCE_INSTANCE-image:$GITHUB_SHA"
SERVICE_ACCOUNT_NAME=test-service-account
gcloud iam service-accounts create "$SERVICE_ACCOUNT_NAME"
KEY_FILE="${HOME}/key.json"
gcloud iam service-accounts keys create "$KEY_FILE" \\
  --iam-account "${SERVICE_ACCOUNT_NAME}@${project-id}.iam.gserviceaccount.com"
gcloud projects add-iam-policy-binding ${project-id} \\
  --member "serviceAccount:${SERVICE_ACCOUNT_NAME}@total-admin-center.iam.gserviceaccount.com" \\
  --role roles/owner
gcloud auth activate-service-account --key-file="${HOME}/key.json"