#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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 Plugin Agent"
description: "Build Plugin Agent before running a plugin test"
inputs:
  base_image_java:
    description: "The Java Docker image under which this test case will run"
    required: false
    default: "eclipse-temurin:8-jdk"
  base_image_tomcat:
    description: "The Tomcat Docker image under which this test case will run"
    required: false
    default: "tomcat:8.5-jdk8-openjdk"
runs:
  using: "composite"
  steps:
    - name: Check License
      uses: apache/skywalking-eyes@a63f4afcc287dfb3727ecc45a4afc55a5e69c15f
    - name: Add checkstyle-plugin
      shell: bash
      run: |
        echo "::group::Add checkstyle plugin to the pom.xml"
        sed -i "/<\/sourceDirectories>/i <sourceDirectory>scenarios\/""${{ inputs.test_case }}""<\/sourceDirectory>" test/plugin/pom.xml
        echo "::endgroup::"
    - name: Cache local Maven repository
      uses: actions/cache@v4
      with:
        path: ~/.m2/repository
        key: ${{ runner.os }}-maven-agent-test-${{ hashFiles('**/pom.xml') }}
        restore-keys: ${{ runner.os }}-maven-agent-test-
    - uses: actions/setup-java@v2
      with:
        distribution: temurin
        java-version: 17
    - name: Build SkyWalking Agent
      shell: bash
      run: |
        echo "::group::Build SkyWalking Agent"
        # Retry one more time due to frequent "maven connection reset"
        ./mvnw -q --batch-mode clean package -Dmaven.test.skip || \
        ./mvnw -q --batch-mode clean package -Dmaven.test.skip
        echo "::endgroup::"
    - uses: actions/upload-artifact@v4
      name: Upload Agent
      with:
        name: skywalking-agent
        path: skywalking-agent
    - uses: actions/setup-java@v2
      with:
        distribution: temurin
        java-version: 8
    - name: Build Docker Image
      shell: bash
      run: |
        echo "::group::Build Docker Image"
        # Retry one more time due to frequent "maven connection reset"
        ./mvnw --batch-mode -f test/plugin/pom.xml \
          -Dmaven.test.skip \
          -Dbase_image_java=${{ inputs.base_image_java }} \
          -Dbase_image_tomcat=${{ inputs.base_image_tomcat }} \
          -Dcontainer_image_version=1.0.0 \
          clean package || \
        ./mvnw --batch-mode -f test/plugin/pom.xml \
          -Dmaven.test.skip \
          -Dbase_image_java=${{ inputs.base_image_java }} \
          -Dbase_image_tomcat=${{ inputs.base_image_tomcat }} \
          -Dcontainer_image_version=1.0.0 \
          clean package
        echo "::endgroup::"

        echo "::group::Save Docker Image"
        mkdir -p test-containers
        docker save -o test-containers/skywalking-agent-test-jvm-1.0.0.tgz skywalking/agent-test-jvm:1.0.0
        docker save -o test-containers/skywalking-agent-test-tomcat-1.0.0.tgz skywalking/agent-test-tomcat:1.0.0
        echo "::endgroup::"
    - uses: actions/upload-artifact@v4
      name: Upload Test Containers
      with:
        name: test-tools
        path: |
          test-containers
          test/plugin/dist/plugin-runner-helper.jar
