Skip to content

Instantly share code, notes, and snippets.

@samuelcolvin
Last active March 28, 2020 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelcolvin/3b662d40e28213fbcd046743cb7068d8 to your computer and use it in GitHub Desktop.
Save samuelcolvin/3b662d40e28213fbcd046743cb7068d8 to your computer and use it in GitHub Desktop.
check a package version matches the version from GITHUB_REF, used when deploying with github actions
#!/usr/bin/env python3
"""
see https://gist.github.com/samuelcolvin/3b662d40e28213fbcd046743cb7068d8 for details
"""
import os
import re
import sys
from importlib import import_module
def main(version_env_var='GITHUB_REF', package_env_var='PACKAGE') -> int:
package = os.getenv(package_env_var)
if not package:
print(f'✖ "{package_env_var}" env variable not found')
return 2
module = import_module(package)
raw_version = module.VERSION
if not isinstance(raw_version, str):
print(f'✖ version "{raw_version!r}" is not a string')
return 2
version = raw_version.lower()
git_ref = os.getenv(version_env_var, 'none')
tag = re.sub('^refs/tags/v*', '', git_ref.lower())
if tag == version:
print(f'✓ {version_env_var} env var {git_ref!r} matches package version: {tag!r} == {version!r}')
return 0
else:
print(f'✖ {version_env_var} env var {git_ref!r} does not match package version: {tag!r} != {version!r}')
return 1
if __name__ == '__main__':
sys.exit(main())

To use simply run

PACKAGE='<your package name>' python <(curl -Ls https://git.io/JvQsH)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment