# This workflow will build and push a new container image to Alibaba Cloud Container Registry (ACR),
# and then will deploy it to Alibaba Cloud Container Service for Kubernetes (ACK), when there is a push to the "develop" branch.
#
# To use this workflow, you will need to complete the following set-up steps:
#
# 1. Create an ACR repository to store your container images.
#    You can use ACR EE instance for more security and better performance.
#    For instructions see https://www.alibabacloud.com/help/doc-detail/142168.htm
#
# 2. Create an ACK cluster to run your containerized application.
#    You can use ACK Pro cluster for more security and better performance.
#    For instructions see https://www.alibabacloud.com/help/doc-detail/95108.htm
#
# 3. Store your AccessKey pair in GitHub Actions secrets named `ACCESS_KEY_ID` and `ACCESS_KEY_SECRET`.
#    For instructions on setting up secrets see: https://developer.github.com/actions/managing-workflows/storing-secrets/
#
# 4. Change the values for the REGION_ID, REGISTRY, NAMESPACE, IMAGE, ACK_CLUSTER_ID, and ACK_DEPLOYMENT_NAME.
#

name: Build and Push to ACR

on:
  push:
    branches: [ "develop" ]

env:
  REGION_ID: cn-shenzhen
  REGISTRY: mmc-registry.cn-shenzhen.cr.aliyuncs.com
  NAMESPACE: sharefly
  IMAGE: pms
  TAG: ${{ github.sha }}
  #ACK_CLUSTER_ID: clusterID
  #ACK_DEPLOYMENT_NAME: nginx-deployment

  ACR_EE_REGISTRY: mmc-registry.cn-shenzhen.cr.aliyuncs.com
  ACR_EE_INSTANCE_ID: cri-yhk5zgfc2v1sia6l
  ACR_EE_NAMESPACE: sharefly
  ACR_EE_IMAGE: pms
  ACR_EE_TAG: ${{ github.sha }}
  JAVA_VERSION: '8'
  GITLAB_URL: https://oauth2:MjVJKxB7m4tCy7symBzn@git.mmcuav.cn/iuav/pms.git

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    environment: dev

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

      #- uses: actions/checkout@v3
      - name: Set up JDK 8
        uses: actions/setup-java@v1
        with:
          java-version: ${{env.JAVA_VERSION}}

      # 2.1 (Optional) Login to ACR EE
      - name: Login to ACR EE with the AccessKey pair
        uses: aliyun/acr-login@v1
        with:
          login-server: "https://${{ env.ACR_EE_REGISTRY }}"
          region-id: "${{ env.REGION_ID }}"
          username: "QD--KeBiTeHangKong@1354706964800968"
          password: "MMC@2023&ACR"
          #access-key-id: "${{ secrets.ACCESS_KEY_ID }}"
          #access-key-secret: "${{ secrets.ACCESS_KEY_SECRET }}"
          instance-id: "${{ env.ACR_EE_INSTANCE_ID }}"

        # 2.2 (Optional) Build and push image ACR EE
      - name: Build and push image to ACR EE
        run: |
          mvn clean package
          docker build -t "$ACR_EE_REGISTRY/$ACR_EE_NAMESPACE/$ACR_EE_IMAGE:$TAG" .
          docker push "$ACR_EE_REGISTRY/$ACR_EE_NAMESPACE/$ACR_EE_IMAGE:$TAG"

      # 3.2 Deploy the image to the ACK cluster
      - name: Kustomize Set Image
        run: |-
          cd kustomization/overlays/dev
          curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"  | bash /dev/stdin 3.8.6
          ./kustomize edit set image REGISTRY/NAMESPACE/IMAGE:TAG=$REGISTRY/$NAMESPACE/$IMAGE:$TAG

      - name: Commit and Push
        run: |
          git config user.name "Chuck"
          git config user.email "Chuck@users.noreply.github.com"
          git remote set-url origin "$GITLAB_URL"
          git commit -am "Generated Image New Tag"
          git push origin develop

      #- name: Commit files
      #  run: |
      #    git config --local user.email "Chuck@users.noreply.github.com"
      #    git config --local user.name "Chuck"
      #    git commit -am "Image Update"

      #- name: Push changes
      #  uses: ad-m/github-push-action@master
      #  with:
      #    github_token: ${{ secrets.GITHUB_TOKEN }}