name: Build on Containers

on:
  workflow_call:

permissions:
  contents: read

env:
  NUGET_PACKAGES: ${{ github.workspace }}/packages
  DOTNET_CLI_TELEMETRY_OPTOUT: 1

jobs:
  build-container:
    strategy:
      fail-fast: false
      matrix:
        include:
          - machine: ubuntu-22.04
            base-image: alpine
            os-type: linux-musl
            architecture: x64
          - machine: ubuntu-22.04-arm
            base-image: alpine
            os-type: linux-musl
            architecture: arm64
    runs-on: ${{ matrix.machine }}
    timeout-minutes: 60
    steps:

    - name: Checkout
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # tag: v4.2.2
      with:
        fetch-depth: 0 # fetching all, needed to correctly calculate version

    - name: Build in Docker container
      run: |
        set -e
        docker build -t mybuildimage -f "./docker/${{ matrix.base-image }}.dockerfile" .
        docker run -e OS_TYPE=${{ matrix.os-type }} --rm --mount type=bind,source="${GITHUB_WORKSPACE}",target=/project mybuildimage \
          /bin/sh -c 'git config --global --add safe.directory /project && ./build.sh BuildWorkflow'

    - name: Test the Shell scripts from README.md in Docker container
      run: |
        set -e
        docker build -t mybuildimage -f "./docker/${{ matrix.base-image }}.dockerfile" .
        docker run --mount type=bind,source="${GITHUB_WORKSPACE}",target=/project --rm mybuildimage /bin/sh -c '
          set -e
          dotnet publish -f net9.0 -c Release ./test/test-applications/integrations/TestApplication.Smoke
          export OTEL_DOTNET_AUTO_HOME="${PWD}/bin/tracer-home"
          . ./instrument.sh
          ./test/test-applications/integrations/bin/TestApplication.Smoke/Release/net9.0/publish/TestApplication.Smoke
            test "$(ls -A /var/log/opentelemetry/dotnet )"
        '

    - name: Publish Linux build
      if: (${{ job.status }} != 'cancelled')
      uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # tag: v4.6.2
      with:
        name: bin-${{ matrix.base-image }}-${{ matrix.architecture }}
        path: bin/tracer-home
      continue-on-error: true
