steps:

# Get the GitHub access token from Secret Manager.
# The token is used to talk to GitHub API, specifically to create a release.
- name: gcr.io/cloud-builders/gcloud
  id: 'github-token'
  entrypoint: 'bash'
  args: 
  - '-c'
  - 'gcloud secrets versions access latest --secret=kaggleteam-github-access-token > /root/github_token'
  volumes:
  - name: 'root'
    path: /root

# Get the pypi password from Secret Manager, and create the ~/.pypirc file.
- name: 'gcr.io/cloud-builders/gcloud'
  id: create-credentials-pypirc
  entrypoint: 'bash'
  args:
    - '-c'
    - |
      token_test=$(gcloud secrets versions access latest --secret=test-pypi-token)
      token=$(gcloud secrets versions access latest --secret=pypi-token)
      cat >~/.pypirc <<EOL
      [distutils]
      index-servers =
        pypi
        pypitest
      [pypi]
      repository: https://upload.pypi.org/legacy/
      username=__token__
      password=${token}
      [pypitest]
      repository: https://test.pypi.org/legacy/
      username=__token__
      password=${token_test}
      EOL
  volumes:
  - name: 'root'
    path: /root

# Pull the repository from GitHub.
- name: 'gcr.io/cloud-builders/git'
  id: clone-repo
  args:
  - clone
  - https://github.com/kaggle/$_GITHUB_REPOSITORY.git

# The hatch docker image is built with the tools/cicd/cloudbuild.yaml build.
- name: us-docker.pkg.dev/$PROJECT_ID/tools/hatch:${_PYTHON_VERSION}
  id: tests
  dir: $_GITHUB_REPOSITORY
  args:
  - test

- name: us-docker.pkg.dev/$PROJECT_ID/tools/hatch:${_PYTHON_VERSION}
  id: lint
  dir: $_GITHUB_REPOSITORY
  args:
  - run
  - lint:all

- name: us-docker.pkg.dev/$PROJECT_ID/tools/hatch:${_PYTHON_VERSION}
  id: build
  dir: $_GITHUB_REPOSITORY
  args:
  - build

# Create a release on GitHub.
# The `hub` image has been built previously in the project:
# $ git clone https://github.com/GoogleCloudPlatform/cloud-builders-community.git
# $ cd cloud-builders-community/hub
# $ gcloud builds submit
- name: 'us-docker.pkg.dev/$PROJECT_ID/tools/hub'
  id: 'release'
  dir: $_GITHUB_REPOSITORY
  entrypoint: 'bash'
  args:
  - '-c'
  - |
    export RELEASE_VERSION=`grep __version__ src/kagglehub/__init__.py | sed -E "s/.*['\"]([^'\"]+)['\"].*/\1/"`
    echo "Preparing release for version $$RELEASE_VERSION"
    export GITHUB_TOKEN=$(</root/github_token)
    hub release create -m "release v$$RELEASE_VERSION" "v$$RELEASE_VERSION"
  env:
  - GITHUB_USER=kaggle
  - GITHUB_REPOSITORY=$_GITHUB_REPOSITORY
  - HUB_PROTOCOL=https
  volumes:
  - name: 'root'
    path: /root

# Release package to pypi.
- name: us-docker.pkg.dev/$PROJECT_ID/tools/hatch:${_PYTHON_VERSION}
  id: release-pypi
  dir: $_GITHUB_REPOSITORY
  entrypoint: bash 
  args:
  - '-c'
  - |
    twine upload dist/* -r pypi
  volumes:
  - name: 'root'
    path: /root

substitutions:
  _GITHUB_REPOSITORY: kagglehub
  # Before changing this, ensure there's a matching tag for our bre-built hatch image:
  # https://pantheon.corp.google.com/artifacts/docker/kaggle-cicd/us/tools/hatch?e=13803378&mods=-ai_platform_fake_service&project=kaggle-cicd
  _PYTHON_VERSION: 3.10.13

