# Copyright (C) 2019 Intel Corporation.  All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

name: Create and publish Docker images

on:
  workflow_call:
    inputs:
      upload_url:
        description: upload binary assets to the URL of release
        type: string
        required: true
      ver_num:
        description: a semantic version number.
        type: string
        required: true

jobs:
  build-and-push-images:
    runs-on: ubuntu-22.04

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Build and save Docker image(wasm-debug-server:${{ inputs.ver_num }}) to tar file
        run: |
          docker build -t wasm-debug-server:${{ inputs.ver_num }} .
          docker save -o wasm-debug-server.tar wasm-debug-server:${{ inputs.ver_num }}
        working-directory: test-tools/wamr-ide/WASM-Debug-Server/Docker

      - name: compress the tar file
        run: |
          tar czf wasm-debug-server-${{ inputs.ver_num }}.tar.gz wasm-debug-server.tar
          zip wasm-debug-server-${{ inputs.ver_num }}.zip wasm-debug-server.tar
        working-directory: test-tools/wamr-ide/WASM-Debug-Server/Docker

      - name: upload release tar.gz
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ inputs.upload_url }}
          asset_path: test-tools/wamr-ide/WASM-Debug-Server/Docker/wasm-debug-server-${{ inputs.ver_num }}.tar.gz
          asset_name: wasm-debug-server-${{ inputs.ver_num }}.tar.gz
          asset_content_type: application/x-gzip

      - name: upload release zip
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ inputs.upload_url }}
          asset_path: test-tools/wamr-ide/WASM-Debug-Server/Docker/wasm-debug-server-${{ inputs.ver_num }}.zip
          asset_name: wasm-debug-server-${{ inputs.ver_num }}.zip
          asset_content_type: application/zip

      - name: Build and save Docker image(wasm-toolchain:${{ inputs.ver_num }}) to tar file
        run: |
          docker build -t wasm-toolchain:${{ inputs.ver_num }} .
          docker save -o wasm-toolchain.tar wasm-toolchain:${{ inputs.ver_num }}
        working-directory: test-tools/wamr-ide/WASM-Toolchain/Docker

      - name: compress the tar file
        run: |
          tar czf wasm-toolchain-${{ inputs.ver_num }}.tar.gz wasm-toolchain.tar
          zip wasm-toolchain-${{ inputs.ver_num }}.zip wasm-toolchain.tar
        working-directory: test-tools/wamr-ide/WASM-Toolchain/Docker

      - name: upload release tar.gz
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ inputs.upload_url }}
          asset_path: test-tools/wamr-ide/WASM-Toolchain/Docker/wasm-toolchain-${{ inputs.ver_num }}.tar.gz
          asset_name: wasm-toolchain-${{ inputs.ver_num }}.tar.gz
          asset_content_type: application/x-gzip

      - name: upload release zip
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ inputs.upload_url }}
          asset_path: test-tools/wamr-ide/WASM-Toolchain/Docker/wasm-toolchain-${{ inputs.ver_num }}.zip
          asset_name: wasm-toolchain-${{ inputs.ver_num }}.zip
          asset_content_type: application/zip

