From ecfb2cc379999b738a93dd801501af16bda2600a Mon Sep 17 00:00:00 2001 From: Ivan Zorin Date: Mon, 8 Apr 2024 19:03:54 +0300 Subject: [PATCH] CI/CD basic implementation based on GitHub Actions (#167) CI/CD basic implementation based on GitHub Actions --- .github/workflows/push.yml | 54 +++++++++++++++++++++++++++ scripts/env.sh | 33 ++++++++++++++++ src/build-scripts/hydrafw-revision.py | 5 ++- src/build-scripts/hydrafw-version.py | 11 +++++- 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/push.yml create mode 100755 scripts/env.sh diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml new file mode 100644 index 00000000..d760ee27 --- /dev/null +++ b/.github/workflows/push.yml @@ -0,0 +1,54 @@ +name: CI + + +on: + push: + branches: + - master + pull_request: + branches: + - master + + +jobs: + build: + runs-on: ubuntu-22.04 + strategy: + matrix: + model: [ "hydrafw" ] + fail-fast: true + + steps: + - name: Update package cache + run: sudo apt update -y + + - name: Install packages + run: sudo apt install -y --no-install-recommends --no-install-suggests bash coreutils tar bzip2 git make python3 python3-pip + + - name: Install python modules + run: python3 -m pip install GitPython intelhex --upgrade + + - uses: actions/checkout@v4 + with: + submodules: true + fetch-tags: true + fetch-depth: 0 + + - name: Set git meta info + run: echo "GITHUB_CI_PR_SHA=${{github.event.pull_request.head.sha}}" >> "${GITHUB_ENV}" + + - name: Install standalone reference GCC toolchain + run: bash scripts/env.sh + + - name: Build ${{ matrix.model }} + run: source build.env && arm-none-eabi-gcc --version && arm-none-eabi-gcc -print-search-dirs && make V=1 -j$(nproc) -C src/ + + - name: Archive ${{ matrix.model }} artifacts + uses: actions/upload-artifact@v4 + with: + name: ${{ matrix.model }} + path: | + src/build/${{ matrix.model }}.dfu + if-no-files-found: error + + diff --git a/scripts/env.sh b/scripts/env.sh new file mode 100755 index 00000000..7d65c924 --- /dev/null +++ b/scripts/env.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +#set -x +set -e + +# This script is used to enroll toolchain with GitHub Actions for CI/CD purposes +# But this script can be used as reference to install recommended toolchain for building hydrafw.dfu locally as well + +MD5SUM=8a4a74872830f80c788c944877d3ad8c +TARBALL=gcc-arm-none-eabi-4_9-2015q3-20150921-linux.tar.bz2 +TARDIR=gcc-arm-none-eabi-4_9-2015q3 +LINK=https://launchpad.net/gcc-arm-embedded/4.9/4.9-2015-q3-update/+download/"${TARBALL}" +CURDIR="$(pwd)" + +echo "${MD5SUM} ${TARBALL}" > md5.txt +wget "${LINK}" +md5sum -c md5.txt +rm md5.txt + +tar xvjf "${TARBALL}" +rm "${TARBALL}" + +if [ -n "${GITHUB_CI_PR_SHA}" ]; then + # This is for GitHub Actions CI/CD tooling only + echo "export PATH=\"${CURDIR}/${TARDIR}/bin\":\"\${PATH}\"" > build.env +else + # And this is for local install + echo "" + echo "Add a line like" + echo "export PATH=\"${CURDIR}/${TARDIR}/bin\":\"\${PATH}\"" + echo "to your ~/.profile and/or ~/.bashrc and run the line to enable toolchain as default one for arm-none-eabi-* target" + echo "" +fi; + diff --git a/src/build-scripts/hydrafw-revision.py b/src/build-scripts/hydrafw-revision.py index b265fa85..1c70982e 100644 --- a/src/build-scripts/hydrafw-revision.py +++ b/src/build-scripts/hydrafw-revision.py @@ -7,4 +7,7 @@ git=Repo(search_parent_directories=True).git version = git.describe(tags=True,always=True,dirty=True,long=True) -print(r.search(version).group(1)) +if r.search(version): + print(r.search(version).group(1)) +else: + print("0.0") diff --git a/src/build-scripts/hydrafw-version.py b/src/build-scripts/hydrafw-version.py index 85740bf9..1f3bd6aa 100644 --- a/src/build-scripts/hydrafw-version.py +++ b/src/build-scripts/hydrafw-version.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 import sys +import os +import re from optparse import OptionParser from datetime import * from git import * @@ -14,7 +16,14 @@ if len(args)==1: sys.stdout = open(args[0], 'w') git=Repo(search_parent_directories=True).git - print('#define HYDRAFW_GIT_TAG "' + git.describe(tags=True,always=True,dirty=True,long=True) + '"') + vermagic=git.describe(tags=True,always=True,dirty=True,long=True) + if os.environ.get("GITHUB_CI_PR_SHA", "") != "": + # if GITHUB_CI_PR_SHA is set, then it's GitHub Actions build, so workaround to set a valid tag is required + sha_id=os.environ["GITHUB_CI_PR_SHA"][:7] + print('#define HYDRAFW_GIT_TAG "' + re.sub('-g.*$', '-g' + sha_id, vermagic) + '"') + else: + # If it's local build, then set "normal" tag + print('#define HYDRAFW_GIT_TAG "' + vermagic + '"') print('#define HYDRAFW_CHECKIN_DATE "' + git.show(['-s', '--pretty=format:%ai']).partition(' ')[0] + '"') else: parser.print_help()