# https://www.springcloud.io/post/2022-08/springcloud-github-actions
# good reference


name: Octopus Complete CI

on:
  # Triggers the workflow on push request events but only for the main branch
  push:
    branches: [ main ]
  #    tags: # tags 更新时触发 workflow
  #      - 'v*'
  # when merged from branches to master will triger the action
  pull_request:
    branches: [ main ]
  # manually run this workflow  from the Actions tab
  workflow_dispatch:

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Set up JDK 11
        uses: actions/setup-java@v3
        with:
          java-version: '11'
          distribution: 'temurin'
          server-id: github # Value of the distributionManagement/repository/id field of the pom.xml-back
          settings-path: ${{ github.workspace }} # location for the settings.xml file
          cache: maven

      - name: Docker Setup QEMU
        # You may pin to the exact commit or the version.
        # uses: docker/setup-qemu-action@e81a89b1732b9c48d79cd809d8d81d79c4647a18
        uses: docker/setup-qemu-action@v2.1.0
        with:
          # QEMU static binaries Docker image (e.g. tonistiigi/binfmt:latest)
          image: # optional, default is tonistiigi/binfmt:latest
          # Platforms to install (e.g. arm64,riscv64,arm)
          platforms: amd64,arm64

      - name: Docker Setup Buildx
        # You may pin to the exact commit or the version.
        # uses: docker/setup-buildx-action@8c0edbc76e98fa90f69d9a2c020dcb50019dc325
        uses: docker/setup-buildx-action@v2.2.1

      - name: Docker Login
        # You may pin to the exact commit or the version.
        # uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a
        uses: docker/login-action@v2.1.0
        with:
          # Username used to log against the Docker registry
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          # Password or personal access token used to log against the Docker registry
          password:  ${{ secrets.DOCKERHUB_TOKEN }}
          # optional, default is true
          logout: true

      - name: Docker Tags GENERATE Uniformly - [ BASE ]
        id: docker_tags_base  # you'll use this in the next step
        uses: docker/metadata-action@v4.1.1
        with:
          # list of Docker images to use as base name for tags
          images: |
            ${{ secrets.DOCKERHUB_USERNAME }}/eclipse-temurin-11-jre-focal
          # Docker tags based on the following events/attributes
          tags: |
            latest
            {{date 'YYYY-MM-DD-HH-mm'}}

      - name: Docker Tags GENERATE Uniformly - [ SERVER ]
        id: docker_tags_server # you'll use this in the next step
        uses: docker/metadata-action@v4.1.1
        with:
          # list of Docker images to use as base name for tags
          images: |
            ${{ secrets.DOCKERHUB_USERNAME }}/wdd-octopus-server
          # Docker tags based on the following events/attributes
          tags: |
            latest
            {{date 'YYYY-MM-DD-HH-mm'}}

      - name: Docker Tags GENERATE Uniformly - [ AGENT ]
        id: docker_tags_agent # you'll use this in the next step
        uses: docker/metadata-action@v4.1.1
        with:
          # list of Docker images to use as base name for tags
          images: |
            ${{ secrets.DOCKERHUB_USERNAME }}/wdd-octopus-agent
          # Docker tags based on the following events/attributes
          tags: |
            latest
            {{date 'YYYY-MM-DD-HH-mm'}}

#      - name: Build My Base Image
#        uses: docker/build-push-action@v3.2.0
#        with:
#          context: ./
#          # Path to the Dockerfile
#          file: ./source/src/main/java/io/wdd/source/build-my-own-base-image.dockerfile
#          platforms: linux/amd64,linux/arm64
#          push: true
#          tags: ${{ steps.docker_tags_base.outputs.tags }}

      - name: Cache
        uses: actions/cache@v3.2.0
        with:
          # A list of files, directories, and wildcard patterns to cache and restore
          path: .m2/repository
          # An explicit key for restoring and saving the cache
          key: octopus-springboot-2.3.6

      - name: Maven package project
        run: |
          mvn -B -DskipTests=true install --file pom.xml
          ls
          echo "--------------------------------------"
          pwd
          echo "--------------------------------------"
          cd ./server/target
          ls
          cd /home/runner/work/ProjectOctopus/ProjectOctopus
          echo "--------------------------------------"
          cd ./agent/target
          ls
          echo "start to change timezone to Asia/Shanghai"

          echo "current date is $(date)"
          echo "start to copy target jar" 
          
          export formatTime=$(date --rfc-3339=seconds | cut -d"+" -f1 | sed "s/ /-/g" | sed "s/:/-/g")
          echo "current format date is ${formatTime}"        
          echo "write the version info to env"
          echo "AGENT_VERSION=${formatTime}" >> $GITHUB_ENV
          
          echo ""
          echo "开始复制agent.jar"
          cp ./agent-*.jar ./octopus-agent-${formatTime}.jar
          
          cd /home/runner/work/ProjectOctopus/ProjectOctopus
          echo "--------------------------------------"
          cat /proc/cpuinfo
          echo "--------------------------------------"

      - name: copy jar to arm seoul 2
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: root
          password: ${{ secrets.SSH_PASSWORD }}
          port: ${{ secrets.SSH_PORT }}
          source: "./agent/target/octopus-agent-*.jar"
          target: "/data/github-action/octopus-agent/"
          rm: true
          strip_components: 2

      - name: copy shell scripts to arm seoul 2
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: root
          password: ${{ secrets.SSH_PASSWORD }}
          port: ${{ secrets.SSH_PORT }}
          source: "./source/src/main/java/io/wdd/source/shell/*"
          target: "/data/github-action/shell/"
          rm: true
          strip_components: 8

      - name: Send Agent Latest Version Info To RabbitMQ.OctopusToServer
        # You may pin to the exact commit or the version.
        # uses: eferraris/rabbit-sender@aca1775ecffe3bce07cf6503089008fa20412e9f
        uses: eferraris/rabbit-sender@v1.1
        with:
          # Rabbit Username
          RABBIT_USERNAME: ${{ secrets.RABBIT_USER }}
          # Rabbit Password
          RABBIT_PASSWORD: ${{ secrets.RABBIT_PASS }}
          # Rabbit Hostname
          RABBIT_HOST: 150.230.198.103
          # RabbitMQ Port
          RABBIT_PORT: 20672
          # Use SSL
          RABBIT_USE_SSL: false
          # Name for the queue
          RABBIT_QUEUE_NAME: "OctopusToServer"
          # Message to be sent
          MESSAGE: "{\"uuid\":\"Octopus-Server\",\"init_time\": null,\"type\":\"AGENT\",\"content\": \"${{ env.AGENT_VERSION }}\",\"result\": \"${{ env.AGENT_VERSION }}\",\"ac_time\": null}"
          # Durability for the queue
          DURABLE: true


      - name: Build and push Docker images - [ Server ]˚
        id: docker_build_server
        # You may pin to the exact commit or the version.
        # uses: docker/build-push-action@c56af957549030174b10d6867f20e78cfd7debc5
        uses: docker/build-push-action@v3.2.0
        with:
          context: ./
          # Path to the Dockerfile
          file: ./server/Dockerfile
          # List of target platforms for build
          platforms: linux/amd64,linux/arm64
          # Always attempt to pull all referenced images
          pull: false
          # Push is a shorthand for --output=type=registry
          push: true
          tags: ${{ steps.docker_tags_server.outputs.tags }}

#      - name: Build and push Docker images - [ Agent ]
#        id: docker_build_agent
#        # You may pin to the exact commit or the version.
#        # uses: docker/build-push-action@c56af957549030174b10d6867f20e78cfd7debc5
#        uses: docker/build-push-action@v3.2.0
#        with:
#          context: ./
#          # Path to the Dockerfile
#          file: ./agent/Dockerfile
#          # List of target platforms for build
#          platforms: linux/amd64,linux/arm64
#          # Always attempt to pull all referenced images
#          pull: false
#          # Push is a shorthand for --output=type=registry
#          push: true
#          tags: ${{ steps.docker_tags_agent.outputs.tags }}
    
