-
Notifications
You must be signed in to change notification settings - Fork 4
205 lines (174 loc) · 8.46 KB
/
reproducible-build.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
name: Build contracts deterministically
on:
workflow_call:
inputs:
image_tag:
type: string
required: true
description: Image multiversx/sdk-rust-contract-builder
project_path:
type: string
default: '.'
required: false
description: A specific project path
contract_name:
type: string
required: false
description: A specific contract to be built
create_release:
type: boolean
description: Whether to create a new release
attach_to_existing_release:
type: boolean
description: Whether to attach output to existing release
skip_preliminary_checks:
type: boolean
description: Skip preliminary checks. Never set this in production!
package_whole_project_src:
type: boolean
description: Include all project files in the packaged source (*.source.json)
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Handle input
run: |
python3 - << "EOF"
import logging
import os
import sys
logging.basicConfig(level=logging.INFO)
image_tag = os.getenv("image_tag", "")
project_path = os.getenv("project_path", "")
contract_name = os.getenv("contract_name", "")
create_release = os.getenv("create_release", "").lower() == "true"
attach_to_existing_release = os.getenv("attach_to_existing_release", "").lower() == "true"
skip_preliminary_checks = os.getenv("skip_preliminary_checks", "").lower() == "true"
package_whole_project_src = os.getenv("package_whole_project_src", "").lower() == "true"
logging.info(f"Image tag: {image_tag}")
logging.info(f"Project path: {project_path}")
logging.info(f"Contract name: {contract_name or '*'}")
logging.info(f"Create new release: {create_release}")
logging.info(f"Attach output to existing release: {attach_to_existing_release}")
logging.info(f"Skip preliminary checks: {skip_preliminary_checks}")
logging.info(f"Package whole project src: {package_whole_project_src}")
if skip_preliminary_checks:
logging.warning("You should never set 'skip_preliminary_checks', unless you really know what you are doing!")
if image_tag == "next":
logging.warning("You've chosen to build the contracts with the image 'multiversx/sdk-rust-contract-builder:next', which is experimental and not recommended for production!")
if contract_name:
logging.info(f"One contract will be built: {contract_name}.")
if create_release and attach_to_existing_release:
sys.exit("ERROR: Choose either to create a new release, or to attach the build output to an existing release. Can't pick both. Feel free to pick none, though.")
EOF
env:
image_tag: ${{ inputs.image_tag }}
project_path: ${{ inputs.project_path }}
contract_name: ${{ inputs.contract_name }}
create_release: ${{ inputs.create_release }}
attach_to_existing_release: ${{ inputs.attach_to_existing_release }}
skip_preliminary_checks: ${{ inputs.skip_preliminary_checks }}
package_whole_project_src: ${{ inputs.package_whole_project_src }}
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ env.GITHUB_REF_NAME }}
fetch-depth: 0
repository: ${{ env.GITHUB_REPOSITORY }}
- name: Preliminary checks
if: ${{ inputs.skip_preliminary_checks == false }}
run: |
python3 - << "EOF"
import logging
import os
import sys
from pathlib import Path
logging.basicConfig(level=logging.INFO)
project_path = os.getenv("project_path", "")
logging.info(f"Project path: {project_path}")
logging.info("Check for missing 'wasm/Cargo.lock' files ...")
missing_cargo_lock = False
for cargo_toml in Path(project_path).rglob("wasm/Cargo.toml"):
cargo_lock = cargo_toml.with_suffix(".lock")
if cargo_lock.exists():
logging.info(f"Found wasm/Cargo.lock: {cargo_lock}")
else:
logging.error(f"wasm/Cargo.lock file not found: {cargo_lock}")
missing_cargo_lock = True
if missing_cargo_lock:
sys.exit(f"ERROR: One or more 'wasm/Cargo.lock' files are missing. They are essential for reproducible builds.")
EOF
env:
project_path: ${{ inputs.project_path }}
- name: Download build script
run: |
wget https://raw.githubusercontent.com/multiversx/mx-sdk-rust-contract-builder/${{ inputs.image_tag }}/build_with_docker.py
- name: Build contracts
run: |
flag_package_whole_project_src=""
if ${{ inputs.package_whole_project_src }}; then
flag_package_whole_project_src="--package-whole-project-src"
fi
python3 ./build_with_docker.py --no-docker-tty --image=multiversx/sdk-rust-contract-builder:${{ inputs.image_tag }} --project=${{ inputs.project_path }} --contract=${{ inputs.contract_name }} --output=/home/runner/work/output-from-docker ${flag_package_whole_project_src}
- name: Save artifacts
uses: actions/upload-artifact@v3
with:
name: build-output
path: |
/home/runner/work/output-from-docker/**/*.*
if-no-files-found: error
- name: Create new release (if applicable)
if: ${{ inputs.create_release == true }}
run: |
gh release create ${{ github.ref_name }} --target=$GITHUB_SHA --prerelease --title="New release: ${{ github.ref_name }}" --generate-notes
# Give some time for processing the request
sleep 10
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
image_tag: ${{ inputs.image_tag }}
- name: Create release notes
if: ${{ inputs.attach_to_existing_release == true || inputs.create_release == true }}
run: |
python3 - << "EOF"
import logging
from hashlib import blake2b
from pathlib import Path
import os
import json
import urllib.request
logging.basicConfig(level=logging.INFO)
repository = os.getenv("GITHUB_REPOSITORY")
ref_name = os.getenv("GITHUB_REF_NAME")
image_tag = os.getenv("image_tag", "")
try:
logging.info(f"Fetching existing release notes, if any, for repository = {repository}, tag = {ref_name} ...")
request = urllib.request.urlopen(f"https://api.github.com/repos/{repository}/releases/tags/{ref_name}")
data = json.loads(request.read())
existing_notes = data["body"]
except Exception as err:
logging.info("An error ocurred while fetching existing release notes (perhaps there is no release yet).")
existing_notes = ""
notes = existing_notes + "\n\n"
notes += "Built using Docker image: **multiversx/sdk-rust-contract-builder:${{ inputs.image_tag }}**.\n"
notes += "\n"
notes+= "## Codehashes (blake2b):\n"
for wasm_file in Path("/home/runner/work/output-from-docker").rglob("*.wasm"):
code = wasm_file.read_bytes()
h = blake2b(digest_size=32)
h.update(code)
notes += f"**{wasm_file.name}**: `{h.hexdigest()}`\n"
Path("release_notes.txt").write_text(notes)
EOF
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
image_tag: ${{ inputs.image_tag }}
- name: Upload artifacts to release, edit release notes
if: ${{ inputs.attach_to_existing_release == true || inputs.create_release == true }}
run: |
gh release edit ${{ github.ref_name }} --notes-file=release_notes.txt
gh release upload ${{ github.ref_name }} $(find /home/runner/work/output-from-docker/**/*.wasm -type f)
gh release upload ${{ github.ref_name }} $(find /home/runner/work/output-from-docker/**/*.source.json -type f)
gh release upload ${{ github.ref_name }} $(find /home/runner/work/output-from-docker/**/*.abi.json -type f)
gh release upload ${{ github.ref_name }} $(find /home/runner/work/output-from-docker/artifacts.json -type f)
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}