Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix missing nox output under CI #261

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: python -m pip install --upgrade nox pdm
- name: Build the distribution
id: build
run: nox -vs build >> $GITHUB_OUTPUT
run: nox -vs build
- name: Read the Changelog
id: read-changelog
uses: mindsers/changelog-reader-action@v2
Expand Down Expand Up @@ -68,10 +68,10 @@ jobs:
git config --global --add safe.directory '*'
- name: Bundle the distribution
id: bundle
run: nox -vs bundle >> $GITHUB_OUTPUT
run: nox -vs bundle
- name: Sign the bundle
id: sign
run: nox -vs sign >> $GITHUB_OUTPUT
run: nox -vs sign
- name: Generate hashes
id: hashes
run: nox -vs make_dist_digest
Expand Down Expand Up @@ -101,7 +101,7 @@ jobs:
- name: Bundle the distribution
id: bundle
shell: bash
run: nox -vs bundle >> $GITHUB_OUTPUT
run: nox -vs bundle
- name: Import certificate
id: windows_import_cert
uses: timheuer/base64-to-file@v1
Expand All @@ -111,7 +111,7 @@ jobs:
- name: Sign the bundle
id: sign
shell: bash
run: nox -vs sign -- '${{ steps.windows_import_cert.outputs.filePath }}' '${{ env.B2_WINDOWS_CODE_SIGNING_CERTIFICATE_PASSWORD }}' >> $GITHUB_OUTPUT
run: nox -vs sign -- '${{ steps.windows_import_cert.outputs.filePath }}' '${{ env.B2_WINDOWS_CODE_SIGNING_CERTIFICATE_PASSWORD }}'
- name: Generate hashes
id: hashes
run: nox -vs make_dist_digest
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
- name: Install dependencies
run: python -m pip install --upgrade nox pdm
- name: Build the distribution
run: nox -vs build >> $GITHUB_OUTPUT
run: nox -vs build
cleanup_buckets:
needs: lint
env:
Expand Down Expand Up @@ -159,7 +159,7 @@ jobs:
git config --global --add safe.directory '*'
- name: Bundle the distribution
id: bundle
run: nox -vs bundle >> $GITHUB_OUTPUT
run: nox -vs bundle
- name: Generate hashes
id: hashes
run: nox -vs make_dist_digest
Expand Down Expand Up @@ -198,7 +198,7 @@ jobs:
- name: Bundle the distribution
id: bundle
shell: bash
run: nox -vs bundle >> $GITHUB_OUTPUT
run: nox -vs bundle
- name: Generate hashes
id: hashes
run: nox -vs make_dist_digest
Expand Down
1 change: 1 addition & 0 deletions changelog.d/+fix_missing_ci_logs.infrastructure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix missing command output when running `nox` under CI.
110 changes: 51 additions & 59 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,28 @@ def _detect_python_nox_id() -> str:
]


def pdm_install(session: nox.Session, *args: str, dev: bool = True, editable: bool = False) -> None:
# dev dependencies are installed by default
prod_args = [] if dev else ['--prod']
editable_args = [] if editable else ['--no-editable']
group_args = []
for group in args:
group_args.extend(['--group', group])
session.run(
'pdm', 'install', *editable_args, *prod_args, *group_args, external=True, **run_kwargs
)


run_kwargs = {}

if CI:
# Inside the CI we need to silence most of the outputs to be able to use GITHUB_OUTPUT properly.
# Nox passes `stderr` and `stdout` directly to subprocess.Popen.
run_kwargs = dict(
stderr=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
)
def pdm_install(
session: nox.Session, *groups: str, dev: bool = True, editable: bool = False
) -> None:
args = []
if not dev:
args.append('--prod')
if not editable:
args.append('--no-editable')
for group in groups:
args.extend(['--group', group])
session.run('pdm', 'install', *args, external=True)


def github_output(name, value, *, secret=False):
gh_output_path = os.environ.get('GITHUB_OUTPUT')
if secret:
print(f"::add-mask::{value}")
if gh_output_path:
with open(gh_output_path, "a") as file:
file.write(f"{name}={value}\n")
else:
print(f"github_output {name}={'******' if secret else value}")


def get_version_key(path: pathlib.Path) -> int:
Expand Down Expand Up @@ -261,17 +262,15 @@ def cover(session):
@nox.session(python=PYTHON_DEFAULT_VERSION)
def build(session):
"""Build the distribution."""
session.run('nox', '-s', 'dump_license', '-fb', 'venv', external=True, **run_kwargs)
session.run('pdm', 'build', external=True, **run_kwargs)
session.run('nox', '-s', 'dump_license', '-fb', 'venv', external=True)
session.run('pdm', 'build', external=True)

# Set outputs for GitHub Actions
if CI:
# Path have to be specified with unix style slashes even for windows,
# otherwise glob won't find files on windows in action-gh-release.
print('asset_path=dist/*')
# Path have to be specified with unix style slashes even for windows,
# otherwise glob won't find files on windows in action-gh-release.
github_output('asset_path', 'dist/*')

version = os.environ['GITHUB_REF'].replace('refs/tags/v', '')
print(f'version={version}')
version = os.environ['GITHUB_REF'].replace('refs/tags/v', '')
github_output('version', version)


@nox.session(python=PYTHON_DEFAULT_VERSION)
Expand All @@ -288,7 +287,7 @@ def bundle(session: nox.Session):
# 1. `b2 license --dump` dumps the licence where the module is installed.
# 2. We don't want to install b2 as editable module in the current session
# because that would make `b2 versions` show the versions as editable.
session.run('nox', '-s', 'dump_license', '-fb', 'venv', external=True, **run_kwargs)
session.run('nox', '-s', 'dump_license', '-fb', 'venv', external=True)
pdm_install(session, 'bundle', 'full')

template_spec = string.Template(pathlib.Path('b2.spec.template').read_text())
Expand All @@ -302,45 +301,42 @@ def bundle(session: nox.Session):
})
pathlib.Path(f'{binary_name}.spec').write_text(spec)

session.run('pyinstaller', *session.posargs, f'{binary_name}.spec', **run_kwargs)
session.run('pyinstaller', *session.posargs, f'{binary_name}.spec')

if SYSTEM == 'linux' and not NO_STATICX:
session.run(
'staticx', '--no-compress', '--strip', '--loglevel', 'INFO', f'dist/{binary_name}',
f'dist/{binary_name}-static', **run_kwargs
f'dist/{binary_name}-static'
)
session.run(
'mv',
'-f',
f'dist/{binary_name}-static',
f'dist/{binary_name}',
external=True,
**run_kwargs
)

# Set outputs for GitHub Actions
if CI:
# Path have to be specified with unix style slashes even for windows,
# otherwise glob won't find files on windows in action-gh-release.
print('asset_path=dist/*')

# Note: this should pick the shortest named executable from the directory.
# But, for yet unknown reason, the `./dist/b2` doesn't play well with `--sut` and the autocomplete.
# For this reason, we're returning here the "latest, stable version" instead.
# This current implementation works fine up until version 10, when it will break.
# By that time, we should have come back to picking the shortest named binary (`b2`) up.
executable = max(
str(path) for path in pathlib.Path('dist').glob('*') if not path.name.startswith('_')
)
print(f'sut_path={executable}')
# Path have to be specified with unix style slashes even for windows,
# otherwise glob won't find files on windows in action-gh-release.
github_output('asset_path', 'dist/*')

# Note: this should pick the shortest named executable from the directory.
# But, for yet unknown reason, the `./dist/b2` doesn't play well with `--sut` and the autocomplete.
# For this reason, we're returning here the "latest, stable version" instead.
# This current implementation works fine up until version 10, when it will break.
# By that time, we should have come back to picking the shortest named binary (`b2`) up.
executable = max(
str(path) for path in pathlib.Path('dist').glob('*') if not path.name.startswith('_')
)
github_output('sut_path', executable)


@nox.session(python=False)
def sign(session):
"""Sign the bundled distribution (macOS and Windows only)."""

def sign_windows(cert_file, cert_password):
session.run('certutil', '-f', '-p', cert_password, '-importpfx', cert_file, **run_kwargs)
session.run('certutil', '-f', '-p', cert_password, '-importpfx', cert_file)
for binary_name in ['b2'] + get_versions():
session.run(
WINDOWS_SIGNTOOL_PATH,
Expand All @@ -357,7 +353,6 @@ def sign_windows(cert_file, cert_password):
'sha256',
f'dist/{binary_name}.exe',
external=True,
**run_kwargs
)
session.run(
WINDOWS_SIGNTOOL_PATH,
Expand All @@ -366,7 +361,6 @@ def sign_windows(cert_file, cert_password):
'/all',
f'dist/{binary_name}.exe',
external=True,
**run_kwargs
)

if SYSTEM == 'windows':
Expand All @@ -387,13 +381,11 @@ def sign_windows(cert_file, cert_password):
name = asset.stem
ext = asset.suffix
asset_path = f'dist/{name}-{SYSTEM}{ext}'
session.run('mv', '-f', asset, asset_path, external=True, **run_kwargs)
session.run('mv', '-f', asset, asset_path, external=True)

# Set outputs for GitHub Actions
if CI:
# Path have to be specified with unix style slashes even for windows,
# otherwise glob won't find files on windows in action-gh-release.
print('asset_path=dist/*')
# Path have to be specified with unix style slashes even for windows,
# otherwise glob won't find files on windows in action-gh-release.
github_output('asset_path', 'dist/*')


def _calculate_hashes(
Expand All @@ -416,7 +408,7 @@ def _calculate_hashes(


def _save_hashes(output_file: pathlib.Path, hashes: list[hashlib._Hash]) -> None: # noqa
longest_algo_name = max([len(elem.name) for elem in hashes])
longest_algo_name = max(len(elem.name) for elem in hashes)
line_format = '{algo:<%s} {hash_value}' % longest_algo_name

output_lines = []
Expand Down
Loading