Search code examples
pythongitgoogle-cloud-platformgoogle-cloud-buildcloudbuild.yaml

How do I access a git tag in a google cloud build?


I have a Cloud Source Repository where I maintain the code of my python package. I have set up two triggers:

  • A trigger that runs on every commit on every branch (this one installs my python package and tests the code.
  • A trigger that runs on a pushed git tag (install the package, test, build artifacts, and deploy them to my private pypi repo).

During the second trigger, I want to verify that my Version number matches the git tag. In the setup.py file, I have added the code:

#!/usr/bin/env python
import sys
import os
from setuptools import setup
from setuptools.command.install import install

VERSION = "v0.1.5"


class VerifyVersionCommand(install):
    """Custom command to verify that the git tag matches our version"""
    description = 'verify that the git tag matches our version'

    def run(self):
        tag = os.getenv('TAG_NAME')

        if tag != VERSION:
            info = "Git tag: {0} does not match the version of this app: {1}".format(
                tag, VERSION
            )
            sys.exit(info)


setup(
    name="name",
    version=VERSION,
    classifiers=["Programming Language :: Python :: 3 :: Only"],
    py_modules=["name"],
    install_requires=[
        [...]
    ],
    packages=["name"],
    cmdclass={
        'verify': VerifyVersionCommand,
    }
)

The beginning of my cloudbuild.yaml looks like this:

steps:

  - name: 'docker.io/library/python:3.8.6'
    id: Install
    entrypoint: /bin/sh
    args:
      - -c
      - |
        python3 -m venv /workspace/venv &&
        . /workspace/venv/bin/activate &&
        pip install -e .

  - name: 'docker.io/library/python:3.8.6'
    id: Verify
    entrypoint: /bin/sh
    args:
      - -c
      - |
        . /workspace/venv/bin/activate &&
        python setup.py verify

This works flawlessly on CircleCi, but on Cloud Build I get the error message:

Finished Step #0 - "Install"
Starting Step #1 - "Verify"
Step #1 - "Verify": Already have image: docker.io/library/python:3.8.6
Step #1 - "Verify": running verify
Step #1 - "Verify": /workspace/venv/lib/python3.8/site-packages/setuptools/dist.py:458: UserWarning: Normalizing 'v0.1.5' to '0.1.5'
Step #1 - "Verify":   warnings.warn(tmpl.format(**locals()))
Step #1 - "Verify": Git tag: None does not match the version of this app: v0.1.5
Finished Step #1 - "Verify"
ERROR
ERROR: build step 1 "docker.io/library/python:3.8.6" failed: step exited with non-zero status: 1

Therefore, the TAG_NAME variable as specified in the Cloud Build documentation seems to not contain the git tag.

How can I access the git tag to verify it?


Solution

  • The TAG_NAME is set as substitution variables but not as environment variables

    You can do that

      - name: 'docker.io/library/python:3.8.6'
        id: Verify
        entrypoint: /bin/sh
        env:
        - "TAG_NAME=$TAG_NAME"
        args:
          - -c
          - |
            . /workspace/venv/bin/activate &&
            python setup.py verify