diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b6a562c..36412e4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,44 +2,86 @@ name: build on: push: - branches: [ "main" ] + branches: + - main + tags: + - '*' pull_request: - branches: [ "main" ] jobs: build: - runs-on: ubuntu-latest - steps: - - name: checkout - uses: actions/checkout@v4 - with: - submodules: false - - - name: submodules - run: git submodule update --init --recursive --depth 1 --jobs $(nproc) - - - name: arm-none-eabi-gcc - uses: ryanwinter/arm-none-eabi-gcc@master - with: - release: '10-2021.10' - - - name: protoc - uses: arduino/setup-protoc@v2 - - - name: pip - run: pip install protobuf grpcio-tools - - - name: cmake - run: cd build && cmake -DCMAKE_BUILD_TYPE=Release .. - - - name: compile - run: cd build && make -j$(nproc) - - - name: upload artifacts - uses: actions/upload-artifact@v4 - with: - name: vgm-fw - path: | - build/app/vgm-fw* + - name: 'Wipe workspace' + run: find ./ -mount -maxdepth 1 -exec rm -rf {} \; + + - name: 'Checkout code' + uses: actions/checkout@v4 + with: + submodules: false + ref: ${{ github.event.pull_request.head.sha }} + + - name: 'Checkout submodules' + run: git submodule update --init --recursive --depth 1 --jobs "$(getconf _NPROCESSORS_ONLN)"; + + - name: 'Setup python' + uses: actions/setup-python@v4 + with: + python-version: '3.11' + + - name: 'Get commit details' + id: names + run: | + if [[ ${{ github.event_name }} == 'pull_request' ]]; then + TYPE="pull" + elif [[ "${{ github.ref }}" == "refs/tags/"* ]]; then + TYPE="tag" + else + TYPE="other" + fi + python3 scripts/get_env.py \ + "--event_file=${{ github.event_path }}" \ + "--type=$TYPE" \ + "--github_auth_token=${{ secrets.GITHUB_TOKEN }}"; + echo "event_type=$TYPE" >> $GITHUB_OUTPUT + + - name: 'Install arm-none-eabi-gcc' + uses: ryanwinter/arm-none-eabi-gcc@master + with: + release: '10-2021.10' + + - name: 'Install protoc' + uses: arduino/setup-protoc@v2 + + - name: 'Install python tools' + run: python3 -m pip install protobuf grpcio-tools + + - name: 'Build firmware' + run: | + pushd build; + cmake -DCMAKE_BUILD_TYPE=Release ../; + make -j"$(getconf _NPROCESSORS_ONLN)"; + popd; + + - name: 'Make artifacts directory' + if: ${{ !github.event.pull_request.head.repo.fork }} + run: | + rm -rf artifacts; + mkdir artifacts; + + - name: 'Move upload files' + if: ${{ !github.event.pull_request.head.repo.fork }} + run: | + mv build/app/firmware.elf artifacts/vgm-rp2040-firmware-${SUFFIX}.elf; + mv build/app/firmware.bin artifacts/vgm-rp2040-firmware-${SUFFIX}.bin; + mv build/app/firmware.uf2 artifacts/vgm-rp2040-firmware-${SUFFIX}.uf2; + mv build/app/firmware.elf.map artifacts/vgm-rp2040-firmware-${SUFFIX}.map; + + - name: 'Upload artifacts to update server' + if: ${{ !github.event.pull_request.head.repo.fork }} + run: | + FILES=$(for CUR in $(ls artifacts/); do echo "-F files=@artifacts/$CUR"; done) + curl --fail -L -H "Token: ${{ secrets.INDEXER_TOKEN }}" \ + -F "branch=${BRANCH_NAME}" \ + ${FILES[@]} \ + "${{ secrets.INDEXER_URL }}"/vgm-firmware/uploadfiles; diff --git a/.gitignore b/.gitignore index 0b1bd37..9f32231 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,6 @@ build/ # KDE stuff .directory .kateproject + +CMakeCache.txt +CMakeFiles/ diff --git a/scripts/get_env.py b/scripts/get_env.py new file mode 100755 index 0000000..21eba18 --- /dev/null +++ b/scripts/get_env.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python3 + +import argparse +import datetime +import json +import os +import random +import re +import shlex +import ssl +import string +import urllib.request + + +def id_gen(size=5, chars=string.ascii_uppercase + string.digits): + return "".join(random.choice(chars) for _ in range(size)) + + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument("--event_file", help="Current GitHub event file", required=True) + parser.add_argument( + "--type", + help="Event file type", + required=True, + choices=["pull", "tag", "other"], + ) + parser.add_argument( + "--github_auth_token", + default=None, + help="Auth token for private repositories", + ) + args = parser.parse_args() + return args + + +def get_commit_json(event, github_auth_token): + context = ssl._create_unverified_context() + commit_url = event["pull_request"]["base"]["repo"]["commits_url"].replace( + "{/sha}", f"/{event['pull_request']['head']['sha']}" + ) + req = urllib.request.Request(commit_url) + if github_auth_token: + req.add_header("Authorization", f"Bearer {github_auth_token}") + with urllib.request.urlopen(req, context=context) as commit_file: + commit_json = json.loads(commit_file.read().decode("utf-8")) + return commit_json + + +def get_details(event, args): + data = {} + current_time = datetime.datetime.utcnow().date() + if args.type == "pull": + commit_json = get_commit_json(event, args.github_auth_token) + data["commit_comment"] = shlex.quote(commit_json["commit"]["message"]) + data["commit_hash"] = commit_json["sha"] + ref = event["pull_request"]["head"]["ref"] + data["pull_id"] = event["pull_request"]["number"] + data["pull_name"] = shlex.quote(event["pull_request"]["title"]) + elif args.type == "tag": + data["commit_comment"] = shlex.quote(event["head_commit"]["message"]) + data["commit_hash"] = event["head_commit"]["id"] + ref = event["ref"] + else: + data["commit_comment"] = shlex.quote(event["commits"][-1]["message"]) + data["commit_hash"] = event["commits"][-1]["id"] + ref = event["ref"] + data["commit_sha"] = data["commit_hash"][:8] + data["branch_name"] = re.sub("refs/\w+/", "", ref) + data["suffix"] = ( + data["branch_name"].replace("/", "_") + + "-" + + current_time.strftime("%d%m%Y") + + "-" + + data["commit_sha"] + ) + if ref.startswith("refs/tags/"): + data["suffix"] = data["branch_name"].replace("/", "_") + return data + + +def add_env(name, value, file): + delimeter = id_gen() + print(f"{name}<<{delimeter}", file=file) + print(f"{value}", file=file) + print(f"{delimeter}", file=file) + + +def add_set_output_var(name, value, file): + print(f"{name}={value}", file=file) + + +def add_envs(data, gh_env_file, gh_out_file, args): + add_env("COMMIT_MSG", data["commit_comment"], gh_env_file) + add_env("COMMIT_HASH", data["commit_hash"], gh_env_file) + add_env("COMMIT_SHA", data["commit_sha"], gh_env_file) + add_env("SUFFIX", data["suffix"], gh_env_file) + add_env("BRANCH_NAME", data["branch_name"], gh_env_file) + add_env("DIST_SUFFIX", data["suffix"], gh_env_file) + add_env("WORKFLOW_BRANCH_OR_TAG", data["branch_name"], gh_env_file) + add_set_output_var("branch_name", data["branch_name"], gh_out_file) + add_set_output_var("commit_sha", data["commit_sha"], gh_out_file) + add_set_output_var("default_target", os.getenv("DEFAULT_TARGET"), gh_out_file) + add_set_output_var("suffix", data["suffix"], gh_out_file) + if args.type == "pull": + add_env("PULL_ID", data["pull_id"], gh_env_file) + add_env("PULL_NAME", data["pull_name"], gh_env_file) + + +def main(): + args = parse_args() + event_file = open(args.event_file, "r") + event = json.load(event_file) + gh_env_file = open(os.environ["GITHUB_ENV"], "a") + gh_out_file = open(os.environ["GITHUB_OUTPUT"], "a") + data = get_details(event, args) + add_envs(data, gh_env_file, gh_out_file, args) + event_file.close() + gh_env_file.close() + gh_out_file.close() + + +if __name__ == "__main__": + main()