name: Registry
on:
  push:
    branches:
      - master
    tags:
      - "v*"
  workflow_dispatch: {}

env:
  BUCKET: ${{ secrets.OSS_BUCKET }}
  ENDPOINT: ${{ secrets.OSS_ENDPOINT }}
  ACCESS_KEY: ${{ secrets.OSS_ACCESS_KEY }}
  ACCESS_KEY_SECRET: ${{ secrets.OSS_ACCESS_KEY_SECRET }}
  ARTIFACT_HUB_REPOSITORY_ID: ${{ secrets.ARTIFACT_HUB_REPOSITORY_ID }}

jobs:
  publish-images:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
      - name: Get the version
        id: get_version
        run: |
          VERSION=${GITHUB_REF#refs/tags/}
          if [[ ${GITHUB_REF} == "refs/heads/master" ]]; then
            VERSION=latest
          fi
          echo ::set-output name=VERSION::${VERSION}
      - name: Get git revision
        id: vars
        shell: bash
        run: |
          echo "::set-output name=git_revision::$(git rev-parse --short HEAD)"
      - name: Login ghcr.io
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Login docker.io
        uses: docker/login-action@v1
        with:
          registry: docker.io
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      - name: Login Alibaba Cloud ACR
        uses: docker/login-action@v1
        with:
          registry: kubevela-registry.cn-hangzhou.cr.aliyuncs.com
          username: ${{ secrets.ACR_USERNAME }}@aliyun-inner.com
          password: ${{ secrets.ACR_PASSWORD }}
      - uses: docker/setup-qemu-action@v1
      - uses: docker/setup-buildx-action@v1
        with:
          driver-opts: image=moby/buildkit:master

      - name: Build & Pushing vela-core for ACR
        run: |
          docker build --build-arg GOPROXY=https://proxy.golang.org --build-arg VERSION=${{ steps.get_version.outputs.VERSION }} --build-arg GITVERSION=git-${{ steps.vars.outputs.git_revision }}  -t kubevela-registry.cn-hangzhou.cr.aliyuncs.com/oamdev/vela-core:${{ steps.get_version.outputs.VERSION }} .
          docker push kubevela-registry.cn-hangzhou.cr.aliyuncs.com/oamdev/vela-core:${{ steps.get_version.outputs.VERSION }}
      - uses: docker/build-push-action@v2
        name: Build & Pushing vela-core for Dockerhub and GHCR
        with:
          context: .
          file: Dockerfile
          labels: |-
            org.opencontainers.image.source=https://github.com/${{ github.repository }}
            org.opencontainers.image.revision=${{ github.sha }}
          platforms: linux/amd64,linux/arm64
          push: ${{ github.event_name != 'pull_request' }}
          build-args: |
            GITVERSION=git-${{ steps.vars.outputs.git_revision }}
            VERSION=${{ steps.get_version.outputs.VERSION }}
            GOPROXY=https://proxy.golang.org
          tags: |-
            docker.io/oamdev/vela-core:${{ steps.get_version.outputs.VERSION }}
            ghcr.io/${{ github.repository }}/vela-core:${{ steps.get_version.outputs.VERSION }}

      - name: Build & Pushing vela-apiserver for ACR
        run: |
          docker build --build-arg GOPROXY=https://proxy.golang.org --build-arg VERSION=${{ steps.get_version.outputs.VERSION }} --build-arg GITVERSION=git-${{ steps.vars.outputs.git_revision }} -t kubevela-registry.cn-hangzhou.cr.aliyuncs.com/oamdev/vela-apiserver:${{ steps.get_version.outputs.VERSION }} -f Dockerfile.apiserver .
          docker push kubevela-registry.cn-hangzhou.cr.aliyuncs.com/oamdev/vela-apiserver:${{ steps.get_version.outputs.VERSION }}
      - uses: docker/build-push-action@v2
        name: Build & Pushing vela-apiserver for Dockerhub and GHCR
        with:
          context: .
          file: Dockerfile.apiserver
          labels: |-
            org.opencontainers.image.source=https://github.com/${{ github.repository }}
            org.opencontainers.image.revision=${{ github.sha }}
          platforms: linux/amd64,linux/arm64
          push: ${{ github.event_name != 'pull_request' }}
          build-args: |
            GITVERSION=git-${{ steps.vars.outputs.git_revision }}
            VERSION=${{ steps.get_version.outputs.VERSION }}
            GOPROXY=https://proxy.golang.org
          tags: |-
            docker.io/oamdev/vela-apiserver:${{ steps.get_version.outputs.VERSION }}
            ghcr.io/${{ github.repository }}/vela-apiserver:${{ steps.get_version.outputs.VERSION }}

      - name: Build & Pushing vela CLI for ACR
        run: |
          docker build --build-arg GOPROXY=https://proxy.golang.org --build-arg VERSION=${{ steps.get_version.outputs.VERSION }} --build-arg GITVERSION=git-${{ steps.vars.outputs.git_revision }}  -t kubevela-registry.cn-hangzhou.cr.aliyuncs.com/oamdev/vela-cli:${{ steps.get_version.outputs.VERSION }} -f Dockerfile.cli .
          docker push kubevela-registry.cn-hangzhou.cr.aliyuncs.com/oamdev/vela-cli:${{ steps.get_version.outputs.VERSION }}
      - uses: docker/build-push-action@v2
        name: Build & Pushing CLI for Dockerhub and GHCR
        with:
          context: .
          file: Dockerfile.cli
          labels: |-
            org.opencontainers.image.source=https://github.com/${{ github.repository }}
            org.opencontainers.image.revision=${{ github.sha }}
          platforms: linux/amd64,linux/arm64
          push: ${{ github.event_name != 'pull_request' }}
          build-args: |
            GITVERSION=git-${{ steps.vars.outputs.git_revision }}
            VERSION=${{ steps.get_version.outputs.VERSION }}
            GOPROXY=https://proxy.golang.org
          tags: |-
            docker.io/oamdev/vela-cli:${{ steps.get_version.outputs.VERSION }}
            ghcr.io/${{ github.repository }}/vela-cli:${{ steps.get_version.outputs.VERSION }}

      - uses: docker/build-push-action@v2
        name: Build & Pushing runtime rollout for Dockerhub and GHCR
        with:
          context: .
          file: runtime/rollout/Dockerfile
          labels: |-
            org.opencontainers.image.source=https://github.com/${{ github.repository }}
            org.opencontainers.image.revision=${{ github.sha }}
          platforms: linux/amd64,linux/arm64
          push: ${{ github.event_name != 'pull_request' }}
          build-args: |
            GITVERSION=git-${{ steps.vars.outputs.git_revision }}
            VERSION=${{ steps.get_version.outputs.VERSION }}
            GOPROXY=https://proxy.golang.org
          tags: |-
            docker.io/oamdev/vela-rollout:${{ steps.get_version.outputs.VERSION }}
            ghcr.io/${{ github.repository }}/vela-rollout:${{ steps.get_version.outputs.VERSION }}
            kubevela-registry.cn-hangzhou.cr.aliyuncs.com/oamdev/vela-rollout:${{ steps.get_version.outputs.VERSION }}

  publish-charts:
    env:
      HELM_CHARTS_DIR: charts
      HELM_CHART: charts/vela-core
      MINIMAL_HELM_CHART: charts/vela-minimal
      LEGACY_HELM_CHART: legacy/charts/vela-core-legacy
      VELA_ROLLOUT_HELM_CHART: runtime/rollout/charts
      LOCAL_OSS_DIRECTORY: .oss/
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@master
      - name: Get git revision
        id: vars
        shell: bash
        run: |
          echo "::set-output name=git_revision::$(git rev-parse --short HEAD)"
      - name: Install Helm
        uses: azure/setup-helm@v1
        with:
          version: v3.4.0
      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: '14'
      - name: Generate helm doc
        run: |
          make helm-doc-gen
      - name: Prepare legacy chart
        run: |
          rsync -r $LEGACY_HELM_CHART $HELM_CHARTS_DIR
          rsync -r $HELM_CHART/* $LEGACY_HELM_CHART --exclude=Chart.yaml --exclude=crds
      - name: Prepare vela chart
        run: |
          rsync -r $VELA_ROLLOUT_HELM_CHART $HELM_CHARTS_DIR
      - uses: oprypin/find-latest-tag@v1
        with:
          repository: oam-dev/kubevela
          releases-only: true
        id: latest_tag
      - name: Tag helm chart image
        run: |
          latest_repo_tag=${{ steps.latest_tag.outputs.tag }}
          sub="."
          major="$(cut -d"$sub" -f1 <<<"$latest_repo_tag")"
          minor="$(cut -d"$sub" -f2 <<<"$latest_repo_tag")"
          patch="0"
          current_repo_tag="$major.$minor.$patch"
          image_tag=${GITHUB_REF#refs/tags/}
          chart_version=$latest_repo_tag
          if [[ ${GITHUB_REF} == "refs/heads/master" ]]; then
            image_tag=latest
            chart_version=${current_repo_tag}-nightly-build
          fi
          sed -i "s/latest/${image_tag}/g" $HELM_CHART/values.yaml
          sed -i "s/latest/${image_tag}/g" $MINIMAL_HELM_CHART/values.yaml
          sed -i "s/latest/${image_tag}/g" $LEGACY_HELM_CHART/values.yaml
          sed -i "s/latest/${image_tag}/g" $VELA_ROLLOUT_HELM_CHART/values.yaml
          chart_smever=${chart_version#"v"}
          sed -i "s/0.1.0/$chart_smever/g" $HELM_CHART/Chart.yaml
          sed -i "s/0.1.0/$chart_smever/g" $MINIMAL_HELM_CHART/Chart.yaml
          sed -i "s/0.1.0/$chart_smever/g" $LEGACY_HELM_CHART/Chart.yaml
          sed -i "s/0.1.0/$chart_smever/g" $VELA_ROLLOUT_HELM_CHART/Chart.yaml
      - name: Install ossutil
        run: wget http://gosspublic.alicdn.com/ossutil/1.7.0/ossutil64 && chmod +x ossutil64 && mv ossutil64 ossutil
      - name: Configure Alibaba Cloud OSSUTIL
        run: ./ossutil --config-file .ossutilconfig config -i ${ACCESS_KEY} -k ${ACCESS_KEY_SECRET} -e ${ENDPOINT} -c .ossutilconfig
      - name: sync cloud to local
        run: ./ossutil --config-file .ossutilconfig sync oss://$BUCKET/core $LOCAL_OSS_DIRECTORY
      - name: add artifacthub stuff to the repo
        run: |
          rsync $HELM_CHART/README.md $LEGACY_HELM_CHART/README.md
          rsync $HELM_CHART/README.md $VELA_ROLLOUT_HELM_CHART/README.md
          sed -i "s/ARTIFACT_HUB_REPOSITORY_ID/$ARTIFACT_HUB_REPOSITORY_ID/g" hack/artifacthub/artifacthub-repo.yml
          rsync hack/artifacthub/artifacthub-repo.yml $LOCAL_OSS_DIRECTORY
      - name: Package helm charts
        run: |
          helm package $HELM_CHART --destination $LOCAL_OSS_DIRECTORY
          helm package $MINIMAL_HELM_CHART --destination $LOCAL_OSS_DIRECTORY
          helm package $LEGACY_HELM_CHART --destination $LOCAL_OSS_DIRECTORY
          helm package $VELA_ROLLOUT_HELM_CHART --destination $LOCAL_OSS_DIRECTORY
          helm repo index --url https://$BUCKET.$ENDPOINT/core $LOCAL_OSS_DIRECTORY
      - name: sync local to cloud
        run: ./ossutil --config-file .ossutilconfig sync $LOCAL_OSS_DIRECTORY oss://$BUCKET/core -f

  publish-capabilities:
    env:
      CAPABILITY_BUCKET: kubevela-registry
      CAPABILITY_DIR: capabilities
      CAPABILITY_ENDPOINT: oss-cn-beijing.aliyuncs.com
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@master
      - name: Install ossutil
        run: wget http://gosspublic.alicdn.com/ossutil/1.7.0/ossutil64 && chmod +x ossutil64 && mv ossutil64 ossutil
      - name: Configure Alibaba Cloud OSSUTIL
        run: ./ossutil --config-file .ossutilconfig config -i ${ACCESS_KEY} -k ${ACCESS_KEY_SECRET} -e ${CAPABILITY_ENDPOINT} -c .ossutilconfig
      - name: sync capabilities bucket to local
        run: ./ossutil --config-file .ossutilconfig sync oss://$CAPABILITY_BUCKET $CAPABILITY_DIR
      - name: rsync all capabilites
        run: rsync vela-templates/registry/auto-gen/* $CAPABILITY_DIR
      - name: sync local to cloud
        run: ./ossutil --config-file .ossutilconfig sync $CAPABILITY_DIR oss://$CAPABILITY_BUCKET -f