name: Build and Upload CoreOS image
on:
  workflow_dispatch:
    inputs:
      coreOSConfigBranch:
        description: "Branch of CoreOS config repo to build from"
        default: "main"
        required: false
      debug:
        description: "Build debug image"
        type: boolean
        default: false
        required: false

jobs:
  build-coreos-image:
    name: "Build CoreOS using customized COSA"
    runs-on: [self-hosted, linux, nested-virt]
    permissions:
      contents: read
      packages: read
    env:
      SHELL: /bin/bash
    steps:
      - name: Checkout
        uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b
        with:
          submodules: recursive
          token: ${{ secrets.CI_GITHUB_REPOSITORY }}

      - name: Build bootstrapper
        if: ${{ inputs.debug == false }}
        uses: ./.github/actions/build_bootstrapper
        with:
          outputPath: ${{ github.workspace }}/build/bootstrapper

      - name: Build debugd
        if: ${{ inputs.debug == true }}
        uses: ./.github/actions/build_debugd
        with:
          outputPath: ${{ github.workspace }}/build/bootstrapper

      - name: Build disk-mapper
        uses: ./.github/actions/build_disk_mapper
        with:
          outputPath: ${{ github.workspace }}/build/disk-mapper

      - name: Determine version
        id: version
        uses: ./.github/actions/pseudo_version

      - name: Log in to the Container registry
        uses: docker/login-action@49ed152c8eca782a232dede0303416e8f356c37b
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Install Azure CLI
        shell: bash
        run: |
          curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
          wget -q https://aka.ms/downloadazcopy-v10-linux -O azcopy.tar.gz
          tar --strip-components 1 -xf azcopy.tar.gz
          rm azcopy.tar.gz
          echo "$(pwd)" >> $GITHUB_PATH

      - name: Login to Azure
        uses: azure/login@24848bc889cfc0a8313c2b3e378ac0d625b9bc16
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: Store GH token to be mounted by cosa
        shell: bash
        run: echo "machine github.com login api password ${{ secrets.CI_GITHUB_REPOSITORY }}" > /tmp/.netrc

      # Make sure to set valid names for GCP and Azure
      #  Azure
      #    gallery name may include alphanumeric characters, dots and underscores. Must end and begin with an alphanumeric character
      #    image definition may include alphanumeric characters, dots, dashes and underscores. Must end and begin with an alphanumeric character
      #    image version has to be semantic version in the form <uint>.<uint>.<uint> . uint may not be larger than 2,147,483,647
      #
      #  GCP
      #    image family and image name may include lowercase alphanumeric characters and dashes. Must not end or begin with a dash
      - name: Configure input variables
        shell: bash
        run: |
          timestamp=${{ steps.version.outputs.timestamp }}
          semver=${{ steps.version.outputs.semanticVersion }}
          releaseVersion=${{ steps.version.outputs.releaseVersion }}
          pseudover=${{ steps.version.outputs.pseudoVersion }}
          echo "azureImageName=constellation-${pseudover//./-}" >> $GITHUB_ENV
          if [ "${{ startsWith(github.ref, 'refs/heads/release/') && (inputs.debug == false) }}" = true ]
          then
            echo "gcpImageName=constellation-${releaseVersion//./-}" >> $GITHUB_ENV
            echo "gcpImageFamily=constellation" >> $GITHUB_ENV
            echo "azureGalleryName=Constellation" >> $GITHUB_ENV
            echo "azureImageDefinition=constellation" >> $GITHUB_ENV
            echo "azureImageVersion=${releaseVersion:1}" >> $GITHUB_ENV
          elif [ "${{ ((github.ref == 'refs/heads/main') || startsWith(github.ref, 'refs/heads/release/')) && (inputs.debug == true) }}" = true ]
          then
            echo "gcpImageName=constellation-${{ steps.version.outputs.timestamp }}" >> $GITHUB_ENV
            echo "gcpImageFamily=constellation-debug-${semver//./-}" >> $GITHUB_ENV
            echo "azureGalleryName=Constellation_Debug" >> $GITHUB_ENV
            echo "azureImageDefinition=${semver}" >> $GITHUB_ENV
            echo "azureImageVersion=${timestamp:0:4}.${timestamp:4:4}.${timestamp:8}" >> $GITHUB_ENV
          else
            echo "gcpImageName=constellation-${{ steps.version.outputs.timestamp }}" >> $GITHUB_ENV
            echo "gcpImageFamily=constellation-${{ steps.version.outputs.branchName }}" >> $GITHUB_ENV
            echo "azureGalleryName=Constellation_Testing" >> $GITHUB_ENV
            echo "azureImageDefinition=${{ steps.version.outputs.branchName }}" >> $GITHUB_ENV
            echo "azureImageVersion=${timestamp:0:4}.${timestamp:4:4}.${timestamp:8}" >> $GITHUB_ENV
          fi

      - name: Build and Upload
        id: build-and-upload
        shell: bash
        run: |
          make cosa-image
          make -j$(nproc) CONTAINER_ENGINE=docker NETRC=/tmp/.netrc \
            COSA_INIT_BRANCH="${{ inputs.coreOSConfigBranch }}" \
            GCP_IMAGE_NAME="${{ env.gcpImageName }}" GCP_IMAGE_FAMILY="${{ env.gcpImageFamily }}" \
            AZURE_IMAGE_DEFINITION="${{ env.azureImageDefinition }}" AZURE_IMAGE_VERSION="${{ env.azureImageVersion }}" AZURE_GALLERY_NAME="${{ env.azureGalleryName }}" AZURE_IMAGE_NAME="${{ env.azureImageName }}"\
            image-gcp image-azure upload-gcp upload-azure
        working-directory: ${{ github.workspace }}/image
