From 52b9eeea6652d2b44d71bed5231225d6ddb3aaf5 Mon Sep 17 00:00:00 2001 From: Artem Bulgakov Date: Mon, 20 Jul 2020 21:55:16 +0300 Subject: [PATCH] Push binaries to data branch instead of artifacts --- .ci/move_binary.py | 45 ++++++++++++++++++++++ .github/workflows/build.yml | 32 +++++++++++++--- README.md | 74 ++++++++++++++++++++++++++++++++++--- 3 files changed, 140 insertions(+), 11 deletions(-) create mode 100644 .ci/move_binary.py diff --git a/.ci/move_binary.py b/.ci/move_binary.py new file mode 100644 index 0000000..2eec8eb --- /dev/null +++ b/.ci/move_binary.py @@ -0,0 +1,45 @@ +#!/bin/python3 +import os +from os import environ as env +import sys +import shutil +import subprocess + +binary_filename = os.path.abspath(sys.argv[1]) +master_repository_directory = os.path.abspath(sys.argv[2]) +data_repository_directory = os.path.abspath(sys.argv[3]) +directory = sys.argv[4] + +os.chdir(master_repository_directory) + +filename = os.path.basename(binary_filename) +commit_hash = subprocess.check_output(["git", "rev-parse", "--verify", "--short", "HEAD"]).decode("utf-8").strip() +commit_subject = subprocess.check_output(["git", "log", "-1", "--pretty=format:'%s'"]).decode("utf-8").strip() + +is_tag = env["GITHUB_EVENT_NAME"] == "push" and env["GITHUB_REF"].startswith("refs/tags") +if not is_tag: + is_pr = env["GITHUB_REF"].startswith("refs/pull") + if is_pr: + # Pull Request - prN (pr1) + middle = "pr" + env["GITHUB_REF"].split("/")[2] + else: + # Latest commit - short hash (20f2448) + middle = commit_hash + filename_split = filename.split("-") + filename = "-".join([*filename_split[:2], middle, *filename_split[2:]]) + +# Set author info to the latest commit author +author_name = subprocess.check_output(["git", "log", "-1", "--pretty=format:'%an'"]).decode("utf-8") +author_email = subprocess.check_output(["git", "log", "-1", "--pretty=format:'%ae'"]).decode("utf-8") + +# Move file +os.chdir(data_repository_directory) +os.makedirs(directory, exist_ok=True) +shutil.copy(binary_filename, os.path.join(directory, filename)) + +# Push changes +subprocess.check_call(["git", "config", "user.name", author_name]) +subprocess.check_call(["git", "config", "user.email", author_email]) +subprocess.check_call(["git", "add", os.path.join(directory, filename)]) +subprocess.check_call(["git", "commit", "-m", f"Add binary for {commit_hash}: \"{commit_subject}\""]) +subprocess.check_call(["git", "push"]) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 86bc835..6b75ffa 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,5 +1,13 @@ name: Build -on: [push, pull_request] +on: + push: + branches-ignore: + - data + - gh-pages + pull_request: + branches-ignore: + - data + - gh-pages jobs: # Build job. Builds app for Android with Buildozer @@ -10,16 +18,28 @@ jobs: steps: - name: Checkout uses: actions/checkout@v2 + with: + path: master - name: Build with Buildozer - uses: ./ # REPLACE WITH ArtemSBulgakov/buildozer-action@v1 + uses: ./master # REPLACE WITH ArtemSBulgakov/buildozer-action@v1 id: buildozer with: + repository_root: master workdir: test_app buildozer_version: stable - - name: Upload artifacts - uses: actions/upload-artifact@v2 + - name: Checkout + uses: actions/checkout@v2 with: - name: package - path: ${{ steps.buildozer.outputs.filename }} + path: data + ref: data # Branch name + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.7 + architecture: x64 + + - name: Push binary to data branch + run: python master/.ci/move_binary.py "${{ steps.buildozer.outputs.filename }}" master data bin diff --git a/README.md b/README.md index 2a056dc..d256ac6 100644 --- a/README.md +++ b/README.md @@ -100,11 +100,63 @@ Example: buildozer_version: stable ``` +## Uploading binaries + +### As artifact + +You can upload binary as artifact to run. You will be able to download it by +clicking on "Artifacts" button on run page (where you see logs). + +```yaml +- name: Upload artifacts + uses: actions/upload-artifact@v2 + with: + name: package + path: ${{ steps.buildozer.outputs.filename }} +``` + +### To branch + +Artifacts use GitHub Storage and you have to pay for private repositories when +limit exceeded. Another way to upload binary is pushing it to branch in your +repository. + +Copy [.ci/move_binary.py](.ci/move_binary.py) script, edit it if you want and +add this to your workflow: + +```yaml +- name: Checkout + uses: actions/checkout@v2 + with: + path: data + ref: data # Branch name + +- name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.7 + architecture: x64 + +- name: Push binary to data branch + run: python master/.ci/move_binary.py "${{ steps.buildozer.outputs.filename }}" master data +``` + ## Full workflow +Builds app and uploads to the `data` branch. Also copy +[.ci/move_binary.py](.ci/move_binary.py) script. + ```yaml name: Build -on: [push, pull_request] +on: + push: + branches-ignore: + - data + - gh-pages + pull_request: + branches-ignore: + - data + - gh-pages jobs: # Build job. Builds app for Android with Buildozer @@ -115,19 +167,31 @@ jobs: steps: - name: Checkout uses: actions/checkout@v2 + with: + path: master - name: Build with Buildozer uses: ArtemSBulgakov/buildozer-action@v1 id: buildozer with: + repository_root: master workdir: test_app buildozer_version: stable - - name: Upload artifacts - uses: actions/upload-artifact@v2 + - name: Checkout + uses: actions/checkout@v2 with: - name: package - path: ${{ steps.buildozer.outputs.filename }} + path: data + ref: data # Branch name + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.7 + architecture: x64 + + - name: Push binary to data branch + run: python master/.ci/move_binary.py "${{ steps.buildozer.outputs.filename }}" master data ``` ## Action versioning