Skip to content

Commit

Permalink
ci: generate full release (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
duhow authored Dec 21, 2024
1 parent cfe11f5 commit 5b3170d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
48 changes: 29 additions & 19 deletions .github/workflows/build-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ jobs:
VERSION=`docker inspect ${IMAGE} | jq -r '.[0].Config.Labels["org.opencontainers.image.version"]'`
echo "version=${VERSION}" >> ${GITHUB_OUTPUT}
# download update file and extract
UPDATE=`docker inspect ${IMAGE} | jq -r '.[0].Config.Labels["org.opencontainers.image.source"]'`
echo "::add-mask::${UPDATE}"
echo "::add-mask::`basename ${UPDATE}`"
wget -q -O mico_${MODEL}.bin "${UPDATE}"
./tools/mico_firmware.py -d mico_${MODEL} -e mico_${MODEL}.bin -i
- name: Pull build packages
uses: actions/download-artifact@v4
with:
Expand All @@ -67,43 +75,45 @@ jobs:
mkdir -p ${TARGET}
sudo tar xzvf ${SOURCE_DIR}/bin-*.tar.gz -C ${TARGET}
- name: Build image
run: sudo make patch build MODEL=${MODEL}
- name: Build artifact
run: sudo make patch build release MODEL=${MODEL}

- name: Prepare image
id: image
- name: Prepare artifact
id: artifact
env:
FILE: release/${{ env.MODEL }}/latest
BASE_VERSION: ${{ steps.base.outputs.version }}
run: |
FILE=`ls -1 release/${MODEL}/mico_*.tar | head -n 1`
HASH=`md5sum ${FILE} | awk '{print $1}'`
echo "hash=${HASH}" | tee -a ${GITHUB_OUTPUT}
NAME="mico_firmware_${HASH: -5}_$(date +"%y%m%d")_${MODEL}.img"
NAME=`basename ${FILE}`
echo "name=${NAME}" | tee -a ${GITHUB_OUTPUT}
cp -vf ${FILE} ${NAME}
cp -vf ${FILE} .
SIZE=`stat -c%s ${NAME}`
echo "size=${SIZE}" | tee -a ${GITHUB_OUTPUT}
echo "```json" >> ${GITHUB_STEP_SUMMARY}
jq -nc \
--arg name "$NAME" \
--argjson size "$SIZE" \
--arg hash "$HASH" \
--arg version "$BASE_VERSION" \
'{"name": $name, "size": $size, "hash": $hash, "base_version": $version}' >> ${GITHUB_STEP_SUMMARY}
echo "```" >> ${GITHUB_STEP_SUMMARY}
- name: Upload image
{
echo '```json'
jq -nc \
--arg name "$NAME" \
--argjson size "$SIZE" \
--arg hash "$HASH" \
--arg version "$BASE_VERSION" \
'{"name": $name, "size": $size, "hash": $hash, "base_version": $version}'
echo '```'
} >> ${GITHUB_STEP_SUMMARY}
- name: Upload artifact to release
uses: softprops/action-gh-release@v2
with:
files: ${{ steps.image.outputs.name }}
files: ${{ steps.artifact.outputs.name }}
draft: true

- name: Attest Build Provenance
uses: actions/attest-build-provenance@v1
with:
subject-path: ${{ steps.image.outputs.name }}
subject-path: ${{ steps.artifact.outputs.name }}
show-summary: false

5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ endif

release: get_version release_set_config release_pack
@MD5TAR=$(shell md5sum $(RELEASE_FILE) | cut -d ' ' -f 1); \
mv $(RELEASE_FILE) $(DESTDIR)/mico_firmware_$${MD5TAR: -7}_$(DATE)_$(MODEL).tar ; \
echo "> Release created: mico_firmware_$${MD5TAR: -7}_$(DATE)_$(MODEL).tar - $${MD5TAR}"
MD5TAR_SHORT=$$(echo $$MD5TAR | rev | cut -c 1-7 | rev); \
mv $(RELEASE_FILE) $(DESTDIR)/mico_firmware_$${MD5TAR_SHORT}_$(DATE)_$(MODEL).tar ; \
echo "> Release created: mico_firmware_$${MD5TAR_SHORT}_$(DATE)_$(MODEL).tar - $$MD5TAR"
@rm -rf $(RELEASE_DIR)

build_squashfs:
Expand Down
24 changes: 17 additions & 7 deletions tools/mico_firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, path):
self.image_header = ImageHeader()
self.fd = open(self.path, 'rb')

def verify(self):
def verify(self, ignore_hash=False):
logging.info('[Jobs] Verifying firmware image...')
assert self.fd.readinto(self.image_header) == ctypes.sizeof(self.image_header)

Expand Down Expand Up @@ -68,15 +68,24 @@ def verify(self):
hash = m.hexdigest()
logging.info(f'computed md5 hash: {hash}')
filename_hash = os.path.basename(self.path).split('_')[-2]
assert hash[(len(filename_hash) * -1):] == filename_hash
try:
assert hash[(len(filename_hash) * -1):] == filename_hash
except AssertionError:
if ignore_hash:
logging.warning(f'Warning: hash "{filename_hash}" does not match expected!')
pass
else:
raise

self.fd.seek(0)
return True

def extract(self, dest):
def extract(self, dest: str = None):
logging.info('[Jobs] Extracting firmware...')
current_time = time.strftime("%Y%m%d_%H%M%S", time.localtime())
self.dest_dir = os.path.join(dest, f'{os.path.basename(self.path).replace(".bin", "")}_{current_time}')
if dest is not None:
self.dest_dir = dest
os.mkdir(self.dest_dir)
logging.info(f'create destination directory: {self.dest_dir}')

Expand All @@ -92,10 +101,10 @@ def extract(self, dest):
logging.info(f'extracting segment: {segment_header.segment_name.decode("ascii")}')


def run(path, extract=False, dest=None):
def run(path, extract=False, dest=None, ignore_hash=False):
logging.info(f'[MSG] Input file: {path}')
firmware = Firmware(path)
if firmware.verify():
if firmware.verify(ignore_hash):
logging.info('[Jobs] Verification success: it\'s a genuine firmware from Xiaomi.')

if extract:
Expand All @@ -108,6 +117,7 @@ def run(path, extract=False, dest=None):

parser = argparse.ArgumentParser()
parser.add_argument('-d', '--dest', help='Destination directory to store extracted files, default=current working directory', action='store')
parser.add_argument('-i', '--ignore-hash', help='Do not fail if md5 hash does not match', action='store_true')
parser.add_argument('-e', '--extract', help='Input your firmware', action='store')
parser.add_argument('-s', '--show', help='Show firmware info', action='store')
args = parser.parse_args()
Expand All @@ -117,9 +127,9 @@ def run(path, extract=False, dest=None):
dest_path = os.getcwd()
if args.dest:
dest_path = args.dest
run(firmware_path, extract=True, dest=dest_path)
run(firmware_path, extract=True, dest=dest_path, ignore_hash=args.ignore_hash)
elif args.show:
firmware_path = os.path.abspath(args.show)
run(firmware_path)
run(firmware_path, ignore_hash=args.ignore_hash)
else:
parser.print_help()

0 comments on commit 5b3170d

Please sign in to comment.