name: Update bazel cache and deploy artifacts

on:
  workflow_call:
    inputs:
      update-cache:
        required: true
        type: boolean
      deploy-channel:
        required: false
        type: string
    secrets:
      artifact-registry-uploader-json-creds:
        required: true

jobs:
  set-variables:
    runs-on: ubuntu-22.04
    outputs:
      debian-version: ${{ steps.define-version.outputs.debian_version }}
      image-version: ${{ steps.define-version.outputs.image_version }}
    steps:
    - name: Checkout repository
      uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8  # v6.0.1
    - name: Define version
      id: define-version
      run: |
        TIME=$(date -u +'%Y%m%d%H%M')
        case "${INPUTS_DEPLOY_CHANNEL}" in
          stable)
            # Debian package format                : X.Y.Z
            # Docker tag / os image version format : X.Y.Z
            SEMVER=$(echo ${GITHUB_REF_NAME} | grep -oE '[0-9]+.[0-9]+.[0-9]+')
            DEBIAN_VERSION="${SEMVER}"
            IMAGE_VERSION="${SEMVER}"
            ;;
          unstable)
            # Debian package format                : X.Y~gitYYYYMMDDHHMM-<Github SHA 8 digit>
            # Docker tag / os image version format : X.Y-<Github SHA 8 digit>
            SEMVER=$(echo ${GITHUB_REF_NAME} | grep -oE '[0-9]+.[0-9]+')
            SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-8)
            DEBIAN_VERSION="${SEMVER}~git${TIME}.${SHORT_SHA}"
            IMAGE_VERSION="${SEMVER}-${SHORT_SHA}"
            ;;
          nightly)
            # Debian package format                : X.Y.Z~gitYYYYMMDDHHMM-<Github SHA 8 digit>
            # Docker tag / os image version format : <Github SHA 8 digit>
            SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-8)
            BASE_VERSION=$(sed -n '1s/.*(\([0-9]\+\.[0-9]\+\.[0-9]\+\)).*/\1/p' base/debian/changelog)
            DEBIAN_VERSION="${BASE_VERSION}~git${TIME}.${SHORT_SHA}"
            IMAGE_VERSION="${SHORT_SHA}"
            ;;
          *)
            DEBIAN_VERSION=""
            IMAGE_VERSION=""
        esac
        echo "image_version=$IMAGE_VERSION" >> $GITHUB_OUTPUT
        echo "image_version is $IMAGE_VERSION"
        echo "debian_version=$DEBIAN_VERSION" >> $GITHUB_OUTPUT
        echo "debian_version is $DEBIAN_VERSION"
      env:
        INPUTS_DEPLOY_CHANNEL: ${{ inputs.deploy-channel }}
  update-cvd-test-bazel-cache:
    if: inputs.update-cache
    runs-on: ubuntu-24.04
    steps:
    - name: Free disk space
      uses: jlumbroso/free-disk-space@v1.3.1
      with:
        large-packages: false
        swap-storage: false
        tool-cache: true
    - name: Checkout repository
      uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8  # v6.0.1
    - name: Mount bazel cache
      uses: ./.github/actions/mount-bazel-cache
      with:
        action-name: run-cvd-unit-tests
        writable: true
    - name: Run cvd unit tests
      uses: ./.github/actions/run-cvd-unit-tests
  update-bazel-cache-and-deploy-debian-package-amd64:
    needs: [set-variables]
    environment: deployment
    runs-on: ubuntu-22.04
    steps:
    - name: Free disk space
      uses: jlumbroso/free-disk-space@54081f138730dfa15788a46383842cd2f914a1be # aka v1.3.1
      with:
        docker-images: false
        swap-storage: false
    - name: checkout repository
      uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8  # v6.0.1
    - name: Mount bazel cache
      uses: ./.github/actions/mount-bazel-cache
      with:
        action-name: build-debian-packages
        writable: '${{ inputs.update-cache }}'
    - name: Build CF host debian packages
      uses: ./.github/actions/build-debian-packages
      with:
        debian-version: ${{ needs.set-variables.outputs.debian-version }}
        image-version: ${{ needs.set-variables.outputs.image-version }}
    - name: Authentication on GCP project android-cuttlefish-artifacts
      if: inputs.deploy-channel != ''
      uses: 'google-github-actions/auth@v3'
      with:
        credentials_json: '${{ secrets.artifact-registry-uploader-json-creds }}'
    - name: Deploy CF host debian packages
      if: inputs.deploy-channel != ''
      uses: ./.github/actions/deploy-debian-packages
      with:
        deploy-channel: ${{ inputs.deploy-channel }}
        path: .
  update-bazel-cache-and-deploy-debian-package-arm64:
    needs: [set-variables]
    environment: deployment
    runs-on: ubuntu-22.04-arm
    steps:
    - name: Free disk space
      uses: jlumbroso/free-disk-space@54081f138730dfa15788a46383842cd2f914a1be # aka v1.3.1
      with:
        docker-images: false
        swap-storage: false
    - name: checkout repository
      uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8  # v6.0.1
    - name: Mount bazel cache
      uses: ./.github/actions/mount-bazel-cache
      with:
        action-name: build-debian-packages
        writable: '${{ inputs.update-cache }}'
    - name: Build CF host debian packages
      uses: ./.github/actions/build-debian-packages
      with:
        debian-version: ${{ needs.set-variables.outputs.debian-version }}
        image-version: ${{ needs.set-variables.outputs.image-version }}
    - name: Authentication on GCP project android-cuttlefish-artifacts
      if: inputs.deploy-channel != ''
      uses: 'google-github-actions/auth@v3'
      with:
        credentials_json: '${{ secrets.artifact-registry-uploader-json-creds }}'
    - name: Deploy CF host debian packages
      if: inputs.deploy-channel != ''
      uses: ./.github/actions/deploy-debian-packages
      with:
        deploy-channel: ${{ inputs.deploy-channel }}
        path: .
  deploy-gigabyte-ampere-installer-arm64:
    if: inputs.deploy-channel != ''
    needs: [set-variables, update-bazel-cache-and-deploy-debian-package-arm64]
    environment: deployment
    runs-on: ubuntu-22.04
    container:
      image: debian@sha256:13f29b6806e531c3ff3b565bb6eed73f2132506c8c9d41bb996065ca20fb27f2 # debian:trixie-20260223 (amd64)
    steps:
    - name: checkout repository
      uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8  # v6.0.1
    - name: Build Gigabyte Ampere installer
      uses: ./.github/actions/build-gigabyte-ampere-cuttlefish-installer
      with:
        deploy-channel: ${{ inputs.deploy-channel }}
        debian-version: ${{ needs.set-variables.outputs.debian-version }}
    - name: Authentication on GCP project android-cuttlefish-artifacts
      uses: 'google-github-actions/auth@v3'
      with:
        credentials_json: '${{ secrets.artifact-registry-uploader-json-creds }}'
    - name: Deploy Gigabyte Ampere server iso installer image.
      uses: ./.github/actions/deploy-gigabyte-ampere-cuttlefish-installer
      with:
        deploy-channel: ${{ inputs.deploy-channel }}
        image-version: ${{ needs.set-variables.outputs.image-version }}
        path: ./gigabyte-ampere-cuttlefish-installer

  deploy-docker-image-amd64:
    if: inputs.deploy-channel != ''
    needs: [set-variables, update-bazel-cache-and-deploy-debian-package-amd64]
    environment: deployment
    runs-on: ubuntu-22.04
    steps:
    - name: Checkout repository
      uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8  # v6.0.1
    - name: Build docker image
      run: container/image/image-builder.sh -m ${INPUTS_DEPLOY_CHANNEL}
      env:
        INPUTS_DEPLOY_CHANNEL: ${{ inputs.deploy-channel }}
    - name: Authentication on GCP project android-cuttlefish-artifacts
      uses: 'google-github-actions/auth@v3'
      with:
        credentials_json: '${{ secrets.artifact-registry-uploader-json-creds }}'
    - name: Login to Artifact Registry
      uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef  # v3.6.0
      with:
        registry: us-docker.pkg.dev
        username: _json_key
        password: '${{ secrets.artifact-registry-uploader-json-creds }}'
    - name: Deploy docker image
      uses: ./.github/actions/deploy-docker-image
      with:
        arch: amd64
        image-version: ${{ needs.set-variables.outputs.image-version }}
  deploy-docker-image-arm64:
    if: inputs.deploy-channel != ''
    needs: [set-variables, update-bazel-cache-and-deploy-debian-package-arm64]
    environment: deployment
    runs-on: ubuntu-22.04-arm
    steps:
    - name: Checkout repository
      uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8  # v6.0.1
    - name: Build docker image
      run: container/image/image-builder.sh -m ${INPUTS_DEPLOY_CHANNEL}
      env:
        INPUTS_DEPLOY_CHANNEL: ${{ inputs.deploy-channel }}
    - name: Authentication on GCP project android-cuttlefish-artifacts
      uses: 'google-github-actions/auth@v3'
      with:
        credentials_json: '${{ secrets.artifact-registry-uploader-json-creds }}'
    - name: Login to Artifact Registry
      uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef  # v3.6.0
      with:
        registry: us-docker.pkg.dev
        username: _json_key
        password: '${{ secrets.artifact-registry-uploader-json-creds }}'
    - name: Deploy docker image
      uses: ./.github/actions/deploy-docker-image
      with:
        arch: arm64
        image-version: ${{ needs.set-variables.outputs.image-version }}
  deploy-docker-manifest:
    if: inputs.deploy-channel != ''
    needs: [set-variables, deploy-docker-image-amd64, deploy-docker-image-arm64]
    environment: deployment
    runs-on: ubuntu-22.04
    steps:
    - name: Checkout repository
      uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8  # v6.0.1
    - name: Authentication on GCP project android-cuttlefish-artifacts
      uses: 'google-github-actions/auth@v3'
      with:
        credentials_json: '${{ secrets.artifact-registry-uploader-json-creds }}'
    - name: Login to Artifact Registry
      uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef  # v3.6.0
      with:
        registry: us-docker.pkg.dev
        username: _json_key
        password: '${{ secrets.artifact-registry-uploader-json-creds }}'
    - name: Deploy manifests
      run: |
        IMAGE=us-docker.pkg.dev/android-cuttlefish-artifacts/cuttlefish-orchestration/cuttlefish-orchestration
        TAG=${NEEDS_SET_VARIABLES_OUTPUTS_IMAGE_VERSION}

        for MANIFEST_TAG in ${TAG} ${INPUTS_DEPLOY_CHANNEL}; do
          docker manifest create ${IMAGE}:${MANIFEST_TAG} \
            --amend ${IMAGE}:${TAG}-amd64 \
            --amend ${IMAGE}:${TAG}-arm64
          docker manifest push ${IMAGE}:${MANIFEST_TAG}
        done
      env:
        INPUTS_DEPLOY_CHANNEL: ${{ inputs.deploy-channel }}
        NEEDS_SET_VARIABLES_OUTPUTS_IMAGE_VERSION: ${{ needs.set-variables.outputs.image-version }}
