Skip to content

Commit

Permalink
change: release GitHub workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyDynamic committed Dec 17, 2023
1 parent 4255ddc commit 97eb2a3
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 39 deletions.
1 change: 0 additions & 1 deletion .github/workflows/matrix_includes.json

This file was deleted.

20 changes: 5 additions & 15 deletions .github/workflows/matrix_prep.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
workflow_call:
inputs:
target_subproject:
description: The subproject name of the specified Minecraft version for generating matrix entry
description: see release.yml, for generating matrix entries
type: string
required: false
default: ''
Expand All @@ -16,24 +16,14 @@ on:

jobs:
matrix_prep:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3

- name: Display context
run: |
echo ref_name = ${{ github.ref_name }}
echo target_subproject = ${{ github.event.inputs.target_subproject }}
echo target_release_tag = ${{ github.event.inputs.target_release_tag }}
- id: setmatrix
uses: JoshuaTheMiller/conditional-build-matrix@v1.0.1
with:
# inputFile: '.github/workflows/matrix_includes.json' # Default input file path
filter: '[? `${{ github.event_name }}` == `release` || `${{ github.event.inputs.target_subproject }}` == `` || `"${{ github.event.inputs.target_subproject }}"` == subproject_dir ]'

- name: Print matrix
run: echo ${{ steps.setmatrix.outputs.matrix }}
run: python3 .github/workflows/scripts/matrix.py # ubuntu-22.04 uses Python 3.10.6
env:
TARGET_SUBPROJECT: ${{ inputs.target_subproject }}

outputs:
matrix: ${{ steps.setmatrix.outputs.matrix }}
46 changes: 46 additions & 0 deletions .github/workflows/scripts/matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
A script to scan through the versions directory and collect all folder names as the subproject list,
then output a json as the github action include matrix
"""
__author__ = 'Fallen_Breath'

import json
import os
import sys


def main():
target_subproject_env = os.environ.get('TARGET_SUBPROJECT', '')
target_subprojects = list(filter(None, target_subproject_env.split(',') if target_subproject_env != '' else []))
print('target_subprojects: {}'.format(target_subprojects))

with open('settings.json') as f:
settings: dict = json.load(f)

if len(target_subprojects) == 0:
subprojects = settings['versions']
else:
subprojects = []
for subproject in settings['versions']:
if subproject in target_subprojects:
subprojects.append(subproject)
target_subprojects.remove(subproject)
if len(target_subprojects) > 0:
print('Unexpected subprojects: {}'.format(target_subprojects), file=sys.stderr)
sys.exit(1)

matrix_entries = []
for subproject in subprojects:
matrix_entries.append({
'subproject': subproject,
})
matrix = {'include': matrix_entries}
with open(os.environ['GITHUB_OUTPUT'], 'w') as f:
f.write('matrix={}\n'.format(json.dumps(matrix)))

print('matrix:')
print(json.dumps(matrix, indent=2))


if __name__ == '__main__':
main()
78 changes: 55 additions & 23 deletions .github/workflows/scripts/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
A script to scan through all valid mod jars in build-artifacts.zip/$version/build/libs,
and generate an artifact summary table for that to GitHub action step summary
"""
__author__ = ['Fallen_Breath', 'Sky_Dynamic']
__author__ = 'Fallen_Breath'

import functools
import glob
import hashlib
import json
import os

Expand All @@ -17,25 +19,55 @@ def read_prop(file_name: str, key: str) -> str:
)).split('=', 1)[1].lstrip()


target_subproject = os.environ.get('TARGET_SUBPROJECT', '')
with open('.github/workflows/matrix_includes.json') as f:
matrix: list[str] = json.load(f)

with open(os.environ['GITHUB_STEP_SUMMARY'], 'w') as f:
f.write('## Build Artifacts Summary\n\n')
f.write('| Subproject | for Minecraft | Files |\n')
f.write('| --- | --- | --- |\n')

for m in matrix:
subproject = m
if target_subproject != '' and subproject != target_subproject:
continue
game_versions = read_prop('versions/{}/gradle.properties'.format(subproject), 'game_versions')
game_versions = game_versions.strip().replace('\\n', ', ')
file_names = glob.glob('build-artifacts/{}/build/libs/*.jar'.format(subproject))
file_names = ', '.join(map(
lambda fn: '`{}`'.format(os.path.basename(fn)),
filter(lambda fn: not fn.endswith('-sources.jar') and not fn.endswith('-dev.jar'), file_names)
))
f.write('| {} | {} | {} |\n'.format(subproject, game_versions, file_names))

def get_sha256_hash(file_path: str) -> str:
sha256_hash = hashlib.sha256()

with open(file_path, 'rb') as f:
for buf in iter(functools.partial(f.read, 4096), b''):
sha256_hash.update(buf)

return sha256_hash.hexdigest()


def main():
target_subproject_env = os.environ.get('TARGET_SUBPROJECT', '')
target_subprojects = list(filter(None, target_subproject_env.split(',') if target_subproject_env != '' else []))
print('target_subprojects: {}'.format(target_subprojects))

with open('settings.json') as f:
settings: dict = json.load(f)

with open(os.environ['GITHUB_STEP_SUMMARY'], 'w') as f:
f.write('## Build Artifacts Summary\n\n')
f.write('| Subproject | for Minecraft | File | Size | SHA-256 |\n')
f.write('| --- | --- | --- | --- | --- |\n')

warnings = []
for subproject in settings['versions']:
if len(target_subprojects) > 0 and subproject not in target_subprojects:
print('skipping {}'.format(subproject))
continue
game_versions = read_prop('versions/{}/gradle.properties'.format(subproject), 'game_versions')
game_versions = game_versions.strip().replace('\\n', ', ')
file_paths = glob.glob('build-artifacts/{}/build/libs/*.jar'.format(subproject))
file_paths = list(filter(lambda fp: not fp.endswith('-sources.jar') and not fp.endswith('-dev.jar') and not fp.endswith('-shadow.jar'), file_paths))
if len(file_paths) == 0:
file_name = '*not found*'
sha256 = '*N/A*'
else:
file_name = '`{}`'.format(os.path.basename(file_paths[0]))
file_size = '{} B'.format(os.path.getsize(file_paths[0]))
sha256 = '`{}`'.format(get_sha256_hash(file_paths[0]))
if len(file_paths) > 1:
warnings.append('Found too many build files in subproject {}: {}'.format(subproject, ', '.join(file_paths)))

f.write('| {} | {} | {} | {} | {} |\n'.format(subproject, game_versions, file_name, file_size, sha256))

if len(warnings) > 0:
f.write('\n### Warnings\n\n')
for warning in warnings:
f.write('- {}\n'.format(warning))


if __name__ == '__main__':
main()

0 comments on commit 97eb2a3

Please sign in to comment.