Skip to content

Commit

Permalink
Make versions_check more reliable in forks (#7785)
Browse files Browse the repository at this point in the history
* discover repo
only run in GH actions
use discovered api
this is usefull for GHES enviroments
style fixes

* Fix wrong env name
  • Loading branch information
matmair authored Aug 2, 2024
1 parent 32db71c commit 6f67fb2
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions .github/scripts/version_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import requests

REPO = os.getenv('GITHUB_REPOSITORY', 'inventree/inventree')
GITHUB_API_URL = os.getenv('GITHUB_API_URL', 'https://api.github.com')


def get_existing_release_tags():
"""Request information on existing releases via the GitHub API."""
Expand All @@ -28,9 +31,7 @@ def get_existing_release_tags():
if token:
headers = {'Authorization': f'Bearer {token}'}

response = requests.get(
'https://api.github.com/repos/inventree/inventree/releases', headers=headers
)
response = requests.get(f'{GITHUB_API_URL}/repos/{REPO}/releases', headers=headers)

if response.status_code != 200:
raise ValueError(
Expand Down Expand Up @@ -90,6 +91,11 @@ def check_version_number(version_string, allow_duplicate=False):


if __name__ == '__main__':
# Ensure that we are running in GH Actions
if os.environ.get('GITHUB_ACTIONS', '') != 'true':
print('This script is intended to be run within a GitHub Action!')
sys.exit(1)

if 'only_version' in sys.argv:
here = Path(__file__).parent.absolute()
version_file = here.joinpath(
Expand All @@ -102,14 +108,13 @@ def check_version_number(version_string, allow_duplicate=False):
results[0] = str(int(results[0]) - 1)
print(results[0])
exit(0)

# GITHUB_REF_TYPE may be either 'branch' or 'tag'
GITHUB_REF_TYPE = os.environ['GITHUB_REF_TYPE']

# GITHUB_REF may be either 'refs/heads/<branch>' or 'refs/heads/<tag>'
GITHUB_REF = os.environ['GITHUB_REF']

GITHUB_REF_NAME = os.environ['GITHUB_REF_NAME']

GITHUB_BASE_REF = os.environ['GITHUB_BASE_REF']

# Print out version information, makes debugging actions *much* easier!
Expand Down Expand Up @@ -193,7 +198,7 @@ def check_version_number(version_string, allow_duplicate=False):
# Ref: https://getridbug.com/python/how-to-set-environment-variables-in-github-actions-using-python/
with open(os.getenv('GITHUB_ENV'), 'a') as env_file:
# Construct tag string
tags = ','.join([f'inventree/inventree:{tag}' for tag in docker_tags])
tags = ','.join([f'{REPO}:{tag}' for tag in docker_tags])

env_file.write(f'docker_tags={tags}\n')

Expand Down

0 comments on commit 6f67fb2

Please sign in to comment.