Skip to content

Commit d8ce216

Browse files
authored
Merge pull request #574 from input-output-hk/djo/543/ci-add-commit-hash-to-toml
Add commit hash to toml before build in CI
2 parents ce3d42d + 0757aef commit d8ce216

File tree

3 files changed

+80
-12
lines changed

3 files changed

+80
-12
lines changed

.github/workflows/actions/build-upload-mithril-artifact/action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ runs:
2323
uses: Swatinem/rust-cache@v2
2424
with:
2525
key: ${{ runner.os }}-cache-v${{ inputs.cache-version }}
26+
27+
- name: Add commit short sha to Cargo.tomls version
28+
shell: bash
29+
run: |
30+
pip3 install toml
31+
python3 ./.github/workflows/scripts/edit-cargo-toml-version.py -l $(echo ${{ github.sha }} | cut -c1-7)
2632
2733
- name: Cargo build
2834
uses: actions-rs/cargo@v1
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import argparse
2+
import glob
3+
import os
4+
5+
import toml
6+
7+
8+
def dir_path(path):
9+
if os.path.isdir(path):
10+
return path
11+
else:
12+
raise NotADirectoryError(path)
13+
14+
15+
def append_label_to_cargo_toml_version(cargo_toml_path, label: str, dry_run: bool):
16+
cargo_toml = toml.load(cargo_toml_path)
17+
print(f"Editing {cargo_toml_path} ...")
18+
19+
if 'package' not in cargo_toml:
20+
print("No package section (probably a workspace file), skipping this Cargo.toml")
21+
return
22+
23+
new_version = f"{cargo_toml['package']['version'].split('-', 1)[0]}-{label}"
24+
print(f"{cargo_toml_path} new version: {new_version}")
25+
26+
if not dry_run:
27+
cargo_toml['package']['version'] = new_version
28+
29+
with open(cargo_toml_path, "w") as f:
30+
toml.dump(cargo_toml, f)
31+
32+
33+
def main(args):
34+
workdir = os.path.relpath(args.working_dir)
35+
print(f"Searching for Cargo.toml(s) in '{workdir}' to edit their version label ...")
36+
37+
cargo_tomls = glob.glob(f"{workdir}/**/Cargo.toml", recursive=True)
38+
print(f"Cargo.toml(s) found: {cargo_tomls}")
39+
40+
for cargo_toml in cargo_tomls:
41+
append_label_to_cargo_toml_version(cargo_toml, args.semver_label, args.dry_run)
42+
43+
44+
if __name__ == '__main__':
45+
parser = argparse.ArgumentParser(
46+
prog="Cargo.toml semver label editor",
47+
description="Edit all Cargo.toml in the given path and subdirectories by replacing their label with the given"
48+
" '--semver-label'.")
49+
parser.add_argument("-l", "--semver-label", required=True,
50+
help="Label to suffix to the Cargo.toml(s) semver version")
51+
parser.add_argument("-d", "--working-dir", type=dir_path, default="./",
52+
help="Directory in which Cargo.toml will be searched")
53+
parser.add_argument("--dry-run", action="store_true")
54+
55+
main(parser.parse_args())

.github/workflows/scripts/package-distribution.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def dir_path(path):
3131

3232

3333
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}")
34+
input_files = os.listdir(input_dir)
35+
print(f"packing {archive_basename} with files: {input_files}")
3636

3737
if platform.system() == "Windows":
3838
import zipfile
@@ -53,10 +53,10 @@ def build_archive(input_dir, destination_dir, archive_basename):
5353
return archive_name
5454

5555

56-
def check_archive(archive, original_input_dir):
56+
def check_archive(archive_path, original_input_dir):
5757
print(f"checking archive ...")
5858
test_dir = "./unpack-test"
59-
shutil.unpack_archive(archive, test_dir)
59+
shutil.unpack_archive(archive_path, test_dir)
6060
original_checksum = sha256sum(original_input_dir)
6161
archive_content_checksum = sha256sum(test_dir)
6262
if original_checksum != archive_content_checksum:
@@ -69,23 +69,30 @@ def check_archive(archive, original_input_dir):
6969
shutil.rmtree(test_dir)
7070

7171

72-
def compute_sha256_checksum(archive):
72+
def compute_sha256_checksum(archive_path):
7373
print(f"computing archive checksum...")
74-
archive_checksum = sha256sum(archive)
75-
checksum_filename = f"{archive}.sha256"
74+
archive_checksum = sha256sum(archive_path)
75+
checksum_filename = f"{archive_path}.sha256"
7676
with open(checksum_filename, "x") as f:
7777
f.write(archive_checksum)
7878

7979

80+
def main(args):
81+
archive_path = build_archive(args.input, args.dest, archive_basename=f"mithril-{args.version}-{args.target}")
82+
check_archive(archive_path, args.input)
83+
compute_sha256_checksum(archive_path)
84+
85+
8086
if __name__ == '__main__':
81-
parser = argparse.ArgumentParser()
87+
parser = argparse.ArgumentParser(
88+
prog="Mithril distribution packager",
89+
description="Package the files in the given '--input' dir in a .tar.gz (linux, macOs) or .zip (windows)"
90+
" plus add a file with the value the sha256 of the generated package."
91+
)
8292
parser.add_argument("--input", type=dir_path, help="input folder which content will be archived", required=True)
8393
parser.add_argument("--dest", type=dir_path, help="destination folder for the archive, default to current folder",
8494
default="./")
8595
parser.add_argument("--version", help="version of the distribution to package", required=True)
8696
parser.add_argument("--target", help="target os & architecture of the package", required=True)
87-
args = parser.parse_args()
8897

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)
98+
main(parser.parse_args())

0 commit comments

Comments
 (0)