Skip to content

Commit d40f493

Browse files
committed
Package built artifacts in a archive before making a gh release
1 parent 6c56417 commit d40f493

File tree

3 files changed

+171
-16
lines changed

3 files changed

+171
-16
lines changed

.github/workflows/ci.yml

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,32 +296,66 @@ jobs:
296296
- run-test-lab
297297
- check
298298
steps:
299-
- name: Download built artifacts (${{ runner.os }}-${{ runner.arch }})
299+
- name: Checkout sources
300+
uses: actions/checkout@v3
301+
302+
- name: Prepare packaging
303+
run: mkdir package
304+
305+
- name: Get short SHA
306+
id: slug
307+
run: echo "sha8=$(echo ${{ github.sha }} | cut -c1-7)" >> $GITHUB_OUTPUT
308+
309+
- name: Download built artifacts (Linux-X64)
300310
uses: actions/download-artifact@v3
301311
with:
302-
name: mithril-distribution-${{ runner.os }}-${{ runner.arch }}
303-
path: ./build
312+
name: mithril-distribution-Linux-X64
313+
path: ./package-Linux-X64
304314

305315
- name: Download built artifacts (macOS-X64)
306316
uses: actions/download-artifact@v3
307317
with:
308318
name: mithril-distribution-macOS-X64
309-
path: ./build
319+
path: ./package-macOS-X64
310320

311321
- name: Download built artifacts (Windows-X64)
312322
uses: actions/download-artifact@v3
313323
with:
314324
name: mithril-distribution-Windows-X64
315-
path: ./build
325+
path: ./package-Windows-X64
316326

327+
- name: Package distribution (Linux-X64)
328+
run: |
329+
python3 ./.github/workflows/scripts/package-distribution.py \
330+
--input package-Linux-X64/ \
331+
--dest package/ \
332+
--version "unstable-${{ steps.slug.outputs.sha8 }}" \
333+
--target "linux-x64"
334+
335+
- name: Package distribution (macOS-X64)
336+
run: |
337+
python3 ./.github/workflows/scripts/package-distribution.py \
338+
--input package-macOS-X64/ \
339+
--dest package/ \
340+
--version "unstable-${{ steps.slug.outputs.sha8 }}" \
341+
--target "macos-x64"
342+
343+
- name: Package distribution (Windows-X64)
344+
run: |
345+
python3 ./.github/workflows/scripts/package-distribution.py \
346+
--input package-Windows-X64/ \
347+
--dest package/ \
348+
--version "unstable-${{ steps.slug.outputs.sha8 }}" \
349+
--target "windows-x64"
350+
317351
- name: Update unstable release
318352
uses: marvinpinto/action-automatic-releases@latest
319353
with:
320354
repo_token: ${{ secrets.GITHUB_TOKEN }}
321355
automatic_release_tag: unstable
322356
prerelease: true
323357
title: Unstable Development Builds
324-
files: build/*
358+
files: package/*
325359

326360
deploy-testing:
327361
runs-on: ubuntu-22.04

.github/workflows/pre-release.yml

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,66 @@ jobs:
1010
create-pre-release:
1111
runs-on: ubuntu-22.04
1212
steps:
13+
- name: Checkout sources
14+
uses: actions/checkout@v3
15+
16+
- name: Prepare packaging
17+
run: mkdir package
18+
1319
- name: Download built artifacts (Linux-x64)
1420
uses: dawidd6/action-download-artifact@v2
1521
with:
1622
name: mithril-distribution-Linux-x64
17-
path: ./build
23+
path: ./package-Linux-X64
1824
commit: ${{ github.sha }}
1925
workflow: ci.yml
2026
workflow_conclusion: success
21-
22-
- name: Download built artifacts (Windows-x64)
27+
28+
- name: Download built artifacts (macOS-x64)
2329
uses: dawidd6/action-download-artifact@v2
2430
with:
25-
name: mithril-distribution-Windows-x64
26-
path: ./build
31+
name: mithril-distribution-macOS-x64
32+
path: ./package-macOS-X64
2733
commit: ${{ github.sha }}
2834
workflow: ci.yml
2935
workflow_conclusion: success
3036

31-
- name: Download built artifacts (macOS-x64)
37+
- name: Download built artifacts (Windows-x64)
3238
uses: dawidd6/action-download-artifact@v2
3339
with:
34-
name: mithril-distribution-macOS-x64
35-
path: ./build
40+
name: mithril-distribution-Windows-x64
41+
path: ./package-Windows-X64
3642
commit: ${{ github.sha }}
3743
workflow: ci.yml
3844
workflow_conclusion: success
45+
46+
- name: Package distribution (Linux-X64)
47+
run: |
48+
python3 ./.github/workflows/scripts/package-distribution.py \
49+
--input package-Linux-X64/ \
50+
--dest package/ \
51+
--version "${{ github.ref_name }}" \
52+
--target "linux-x64"
53+
54+
- name: Package distribution (macOS-X64)
55+
run: |
56+
python3 ./.github/workflows/scripts/package-distribution.py \
57+
--input package-macOS-X64/ \
58+
--dest package/ \
59+
--version "${{ github.ref_name }}" \
60+
--target "macos-x64"
61+
62+
- name: Package distribution (Windows-X64)
63+
run: |
64+
python3 ./.github/workflows/scripts/package-distribution.py \
65+
--input package-Windows-X64/ \
66+
--dest package/ \
67+
--version "${{ github.ref_name }}" \
68+
--target "windows-x64"
3969
4070
- name: Append VERSION file
4171
run: |
42-
echo ${{ github.ref_name }} >> ./build/VERSION
72+
echo ${{ github.ref_name }} >> ./package/VERSION
4373
4474
- name: Create pre-release ${{ github.ref_name }}
4575
uses: marvinpinto/action-automatic-releases@latest
@@ -48,7 +78,7 @@ jobs:
4878
automatic_release_tag: ${{ github.ref_name }}
4979
prerelease: true
5080
title: Mithril v${{ github.ref_name }}
51-
files: build/**/*
81+
files: package/*
5282

5383
build-push-docker:
5484
runs-on: ubuntu-22.04
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import hashlib
2+
import argparse
3+
import os
4+
import platform
5+
import shutil
6+
7+
8+
def sha256sum(path: str):
9+
h = hashlib.sha256()
10+
paths = []
11+
12+
# Note: this won't works if the given path is a directory with a subdirectory
13+
if os.path.isdir(path):
14+
for file in os.listdir(path):
15+
paths.append(os.path.join(path, file))
16+
else:
17+
paths.append(path)
18+
19+
for path in paths:
20+
with open(path, "rb") as f:
21+
data = f.read()
22+
h.update(data)
23+
return h.hexdigest()
24+
25+
26+
def dir_path(path):
27+
if os.path.isdir(path):
28+
return path
29+
else:
30+
raise NotADirectoryError(path)
31+
32+
33+
def build_archive(input_dir, destination_dir, archive_basename):
34+
input_files = os.listdir(args.input)
35+
print(f"packing mithril distribution {args.version}-{args.target} with files: {input_files}")
36+
37+
if platform.system() == "Windows":
38+
import zipfile
39+
40+
archive_name = os.path.join(destination_dir, f"{archive_basename}.zip")
41+
with zipfile.ZipFile(archive_name, mode="x") as archive:
42+
for filename in input_files:
43+
archive.write(os.path.join(input_dir, filename), arcname=filename)
44+
else:
45+
import tarfile
46+
47+
archive_name = os.path.join(destination_dir, f"{archive_basename}.tar.gz")
48+
with tarfile.open(archive_name, "x:gz") as archive:
49+
for filename in input_files:
50+
archive.add(os.path.join(input_dir, filename), arcname=filename)
51+
52+
print(f"successfully packed mithril distribution into: {archive_name}")
53+
return archive_name
54+
55+
56+
def check_archive(archive, original_input_dir):
57+
print(f"checking archive ...")
58+
test_dir = "./unpack-test"
59+
shutil.unpack_archive(archive, test_dir)
60+
original_checksum = sha256sum(original_input_dir)
61+
archive_content_checksum = sha256sum(test_dir)
62+
if original_checksum != archive_content_checksum:
63+
print(
64+
f"mithril distribution checksum mismatch: before {original_checksum} != after {archive_content_checksum}"
65+
)
66+
exit(1)
67+
print("OK ! Checksum of the archive files matches original files")
68+
69+
shutil.rmtree(test_dir)
70+
71+
72+
def compute_sha256_checksum(archive):
73+
print(f"computing archive checksum...")
74+
archive_checksum = sha256sum(archive)
75+
checksum_filename = f"{archive}.sha256"
76+
with open(checksum_filename, "x") as f:
77+
f.write(archive_checksum)
78+
79+
80+
if __name__ == '__main__':
81+
parser = argparse.ArgumentParser()
82+
parser.add_argument("--input", type=dir_path, help="input folder which content will be archived", required=True)
83+
parser.add_argument("--dest", type=dir_path, help="destination folder for the archive, default to current folder",
84+
default="./")
85+
parser.add_argument("--version", help="version of the distribution to package", required=True)
86+
parser.add_argument("--target", help="target os & architecture of the package", required=True)
87+
args = parser.parse_args()
88+
89+
archive_path = build_archive(args.input, args.dest, archive_basename=f"mithril-{args.version}-{args.target}")
90+
check_archive(archive_path, args.input)
91+
compute_sha256_checksum(archive_path)

0 commit comments

Comments
 (0)