From 4f26f3177c6d714d149d7b4e2449c2cba9704fac Mon Sep 17 00:00:00 2001 From: Kevin Schoedel <67607049+kpschoedel@users.noreply.github.com> Date: Mon, 29 Nov 2021 10:24:03 -0500 Subject: [PATCH] Size reports: add a script to streamline workflows (#12299) #### Problem Workflow files have become cluttered with copy-pasted setup for size reports. #### Change overview Encapsulate the setup in a script, `gh_sizes_environment.py`. #### Testing Script checked manually, but confirmation for Github integration can only come from live CI. --- .github/workflows/examples-efr32.yaml | 18 ++---- .github/workflows/examples-esp32.yaml | 15 ++--- .github/workflows/examples-infineon.yaml | 17 ++---- .github/workflows/examples-k32w.yaml | 15 ++--- .../workflows/examples-linux-standalone.yaml | 15 ++--- .github/workflows/examples-mbed.yaml | 15 ++--- .github/workflows/examples-nrfconnect.yaml | 15 ++--- .github/workflows/examples-qpg.yaml | 15 ++--- .github/workflows/examples-telink.yaml | 15 ++--- scripts/tools/memory/gh_sizes_environment.py | 55 +++++++++++++++++++ 10 files changed, 92 insertions(+), 103 deletions(-) create mode 100755 scripts/tools/memory/gh_sizes_environment.py diff --git a/.github/workflows/examples-efr32.yaml b/.github/workflows/examples-efr32.yaml index b54a80ee3f3935..cd24c6092d0fde 100644 --- a/.github/workflows/examples-efr32.yaml +++ b/.github/workflows/examples-efr32.yaml @@ -21,7 +21,7 @@ on: concurrency: group: ${{ github.ref }}-${{ github.workflow }}-${{ (github.event_name == 'pull_request' && github.event.number) || (github.event_name == 'workflow_dispatch' && github.run_number) || github.sha }} cancel-in-progress: true - + jobs: efr32: name: EFR32 @@ -30,9 +30,6 @@ jobs: env: EFR32_BOARD: BRD4161A BUILD_TYPE: gn_efr32 - GH_EVENT_PR: ${{ github.event_name == 'pull_request' && github.event.number || 0 }} - GH_EVENT_HASH: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} - GH_EVENT_BASE: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }} runs-on: ubuntu-latest if: github.actor != 'restyled-io[bot]' @@ -47,15 +44,10 @@ jobs: with: submodules: true - - name: Get parent for size reports - shell: bash - run: | - MERGE_PARENT="$(git --no-pager log -1 --pretty=tformat:%s | sed -n -e 's/Merge [0-9a-f]\+ into //p')" - if test -z "${MERGE_PARENT}" || test "${GH_EVENT_PARENT}" = 0 - then - MERGE_PARENT=$GH_EVENT_BASE - fi - echo "GH_EVENT_PARENT=$MERGE_PARENT" >>$GITHUB_ENV + - name: Set up environment for size reports + env: + GH_CONTEXT: ${{ toJson(github) }} + run: scripts/tools/memory/gh_sizes_environment.py "${GH_CONTEXT}" - name: Bootstrap timeout-minutes: 25 diff --git a/.github/workflows/examples-esp32.yaml b/.github/workflows/examples-esp32.yaml index 858a7f6aedd22a..fe59c53d8016c8 100644 --- a/.github/workflows/examples-esp32.yaml +++ b/.github/workflows/examples-esp32.yaml @@ -30,9 +30,6 @@ jobs: env: BUILD_TYPE: esp32 - GH_EVENT_PR: ${{ github.event_name == 'pull_request' && github.event.number || 0 }} - GH_EVENT_HASH: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} - GH_EVENT_BASE: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }} runs-on: ubuntu-latest if: github.actor != 'restyled-io[bot]' @@ -48,14 +45,10 @@ jobs: with: submodules: true - - name: Get parent for size reports - run: | - MERGE_PARENT="$(git --no-pager log -1 --pretty=tformat:%s | sed -n -e 's/Merge [0-9a-f]\+ into //p')" - if test -z "${MERGE_PARENT}" || test "${GH_EVENT_PARENT}" = 0 - then - MERGE_PARENT=$GH_EVENT_BASE - fi - echo "GH_EVENT_PARENT=$MERGE_PARENT" >>$GITHUB_ENV + - name: Set up environment for size reports + env: + GH_CONTEXT: ${{ toJson(github) }} + run: scripts/tools/memory/gh_sizes_environment.py "${GH_CONTEXT}" - name: Bootstrap timeout-minutes: 25 diff --git a/.github/workflows/examples-infineon.yaml b/.github/workflows/examples-infineon.yaml index 05174f70243398..7bef8b9552f252 100644 --- a/.github/workflows/examples-infineon.yaml +++ b/.github/workflows/examples-infineon.yaml @@ -28,11 +28,6 @@ jobs: name: Infineon examples building timeout-minutes: 30 - env: - GH_EVENT_PR: ${{ github.event_name == 'pull_request' && github.event.number || 0 }} - GH_EVENT_HASH: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} - GH_EVENT_BASE: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }} - runs-on: ubuntu-latest if: github.actor != 'restyled-io[bot]' @@ -45,14 +40,10 @@ jobs: with: submodules: true - - name: Get parent for size reports - run: | - MERGE_PARENT="$(git --no-pager log -1 --pretty=tformat:%s | sed -n -e 's/Merge [0-9a-f]\+ into //p')" - if test -z "${MERGE_PARENT}" || test "${GH_EVENT_PARENT}" = 0 - then - MERGE_PARENT=$GH_EVENT_BASE - fi - echo "GH_EVENT_PARENT=$MERGE_PARENT" >>$GITHUB_ENV + - name: Set up environment for size reports + env: + GH_CONTEXT: ${{ toJson(github) }} + run: scripts/tools/memory/gh_sizes_environment.py "${GH_CONTEXT}" - name: Bootstrap timeout-minutes: 25 diff --git a/.github/workflows/examples-k32w.yaml b/.github/workflows/examples-k32w.yaml index ea16700d130f2d..22676e6b0708e8 100644 --- a/.github/workflows/examples-k32w.yaml +++ b/.github/workflows/examples-k32w.yaml @@ -29,9 +29,6 @@ jobs: env: BUILD_TYPE: gn_k32w - GH_EVENT_PR: ${{ github.event_name == 'pull_request' && github.event.number || 0 }} - GH_EVENT_HASH: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} - GH_EVENT_BASE: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }} runs-on: ubuntu-latest if: github.actor != 'restyled-io[bot]' @@ -46,14 +43,10 @@ jobs: with: submodules: true - - name: Get parent for size reports - run: | - MERGE_PARENT="$(git --no-pager log -1 --pretty=tformat:%s | sed -n -e 's/Merge [0-9a-f]\+ into //p')" - if test -z "${MERGE_PARENT}" || test "${GH_EVENT_PARENT}" = 0 - then - MERGE_PARENT=$GH_EVENT_BASE - fi - echo "GH_EVENT_PARENT=$MERGE_PARENT" >>$GITHUB_ENV + - name: Set up environment for size reports + env: + GH_CONTEXT: ${{ toJson(github) }} + run: scripts/tools/memory/gh_sizes_environment.py "${GH_CONTEXT}" - name: Bootstrap timeout-minutes: 25 diff --git a/.github/workflows/examples-linux-standalone.yaml b/.github/workflows/examples-linux-standalone.yaml index 0c0735f97dea4d..f6593e5d7a5886 100644 --- a/.github/workflows/examples-linux-standalone.yaml +++ b/.github/workflows/examples-linux-standalone.yaml @@ -29,9 +29,6 @@ jobs: env: BUILD_TYPE: gn_linux - GH_EVENT_PR: ${{ github.event_name == 'pull_request' && github.event.number || 0 }} - GH_EVENT_HASH: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} - GH_EVENT_BASE: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }} runs-on: ubuntu-latest if: github.actor != 'restyled-io[bot]' @@ -47,14 +44,10 @@ jobs: with: submodules: true - - name: Get parent for size reports - run: | - MERGE_PARENT="$(git --no-pager log -1 --pretty=tformat:%s | sed -n -e 's/Merge [0-9a-f]\+ into //p')" - if test -z "${MERGE_PARENT}" || test "${GH_EVENT_PARENT}" = 0 - then - MERGE_PARENT=$GH_EVENT_BASE - fi - echo "GH_EVENT_PARENT=$MERGE_PARENT" >>$GITHUB_ENV + - name: Set up environment for size reports + env: + GH_CONTEXT: ${{ toJson(github) }} + run: scripts/tools/memory/gh_sizes_environment.py "${GH_CONTEXT}" - name: Bootstrap timeout-minutes: 10 diff --git a/.github/workflows/examples-mbed.yaml b/.github/workflows/examples-mbed.yaml index 23bc0573e8563c..ed5932aad9f5c0 100644 --- a/.github/workflows/examples-mbed.yaml +++ b/.github/workflows/examples-mbed.yaml @@ -32,9 +32,6 @@ jobs: BUILD_TYPE: mbedos APP_PROFILE: release APP_TARGET: CY8CPROTO_062_4343W - GH_EVENT_PR: ${{ github.event_name == 'pull_request' && github.event.number || 0 }} - GH_EVENT_HASH: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} - GH_EVENT_BASE: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }} runs-on: ubuntu-latest if: github.actor != 'restyled-io[bot]' @@ -50,14 +47,10 @@ jobs: with: submodules: true - - name: Get parent for size reports - run: | - MERGE_PARENT="$(git --no-pager log -1 --pretty=tformat:%s | sed -n -e 's/Merge [0-9a-f]\+ into //p')" - if test -z "${MERGE_PARENT}" || test "${GH_EVENT_PARENT}" = 0 - then - MERGE_PARENT=$GH_EVENT_BASE - fi - echo "GH_EVENT_PARENT=$MERGE_PARENT" >>$GITHUB_ENV + - name: Set up environment for size reports + env: + GH_CONTEXT: ${{ toJson(github) }} + run: scripts/tools/memory/gh_sizes_environment.py "${GH_CONTEXT}" - name: Bootstrap timeout-minutes: 10 diff --git a/.github/workflows/examples-nrfconnect.yaml b/.github/workflows/examples-nrfconnect.yaml index 8f3f8bc026a4db..63bd1c210c6238 100644 --- a/.github/workflows/examples-nrfconnect.yaml +++ b/.github/workflows/examples-nrfconnect.yaml @@ -29,9 +29,6 @@ jobs: env: BUILD_TYPE: nrfconnect - GH_EVENT_PR: ${{ github.event_name == 'pull_request' && github.event.number || 0 }} - GH_EVENT_HASH: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} - GH_EVENT_BASE: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }} runs-on: ubuntu-latest if: github.actor != 'restyled-io[bot]' @@ -47,14 +44,10 @@ jobs: with: submodules: true - - name: Get parent for size reports - run: | - MERGE_PARENT="$(git --no-pager log -1 --pretty=tformat:%s | sed -n -e 's/Merge [0-9a-f]\+ into //p')" - if test -z "${MERGE_PARENT}" || test "${GH_EVENT_PARENT}" = 0 - then - MERGE_PARENT=$GH_EVENT_BASE - fi - echo "GH_EVENT_PARENT=$MERGE_PARENT" >>$GITHUB_ENV + - name: Set up environment for size reports + env: + GH_CONTEXT: ${{ toJson(github) }} + run: scripts/tools/memory/gh_sizes_environment.py "${GH_CONTEXT}" - name: Bootstrap timeout-minutes: 25 diff --git a/.github/workflows/examples-qpg.yaml b/.github/workflows/examples-qpg.yaml index ee90789371c815..486f774c7c79d0 100644 --- a/.github/workflows/examples-qpg.yaml +++ b/.github/workflows/examples-qpg.yaml @@ -29,9 +29,6 @@ jobs: env: BUILD_TYPE: gn_qpg - GH_EVENT_PR: ${{ github.event_name == 'pull_request' && github.event.number || 0 }} - GH_EVENT_HASH: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} - GH_EVENT_BASE: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }} runs-on: ubuntu-latest if: github.actor != 'restyled-io[bot]' @@ -46,14 +43,10 @@ jobs: with: submodules: true - - name: Get parent for size reports - run: | - MERGE_PARENT="$(git --no-pager log -1 --pretty=tformat:%s | sed -n -e 's/Merge [0-9a-f]\+ into //p')" - if test -z "${MERGE_PARENT}" || test "${GH_EVENT_PARENT}" = 0 - then - MERGE_PARENT=$GH_EVENT_BASE - fi - echo "GH_EVENT_PARENT=$MERGE_PARENT" >>$GITHUB_ENV + - name: Set up environment for size reports + env: + GH_CONTEXT: ${{ toJson(github) }} + run: scripts/tools/memory/gh_sizes_environment.py "${GH_CONTEXT}" - name: Bootstrap timeout-minutes: 25 diff --git a/.github/workflows/examples-telink.yaml b/.github/workflows/examples-telink.yaml index 45557aa3f393d4..b24da206d6848d 100644 --- a/.github/workflows/examples-telink.yaml +++ b/.github/workflows/examples-telink.yaml @@ -27,9 +27,6 @@ jobs: name: Telink env: BUILD_TYPE: telink - GH_EVENT_PR: ${{ github.event_name == 'pull_request' && github.event.number || 0 }} - GH_EVENT_HASH: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} - GH_EVENT_BASE: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }} runs-on: ubuntu-latest if: github.actor != 'restyled-io[bot]' @@ -45,14 +42,10 @@ jobs: with: submodules: true - - name: Get parent for size reports - run: | - MERGE_PARENT="$(git --no-pager log -1 --pretty=tformat:%s | sed -n -e 's/Merge [0-9a-f]\+ into //p')" - if test -z "${MERGE_PARENT}" || test "${GH_EVENT_PARENT}" = 0 - then - MERGE_PARENT=$GH_EVENT_BASE - fi - echo "GH_EVENT_PARENT=$MERGE_PARENT" >>$GITHUB_ENV + - name: Set up environment for size reports + env: + GH_CONTEXT: ${{ toJson(github) }} + run: scripts/tools/memory/gh_sizes_environment.py "${GH_CONTEXT}" - name: Build example Telink Lighting App run: | diff --git a/scripts/tools/memory/gh_sizes_environment.py b/scripts/tools/memory/gh_sizes_environment.py new file mode 100755 index 00000000000000..bca21b5fa4356f --- /dev/null +++ b/scripts/tools/memory/gh_sizes_environment.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 + +""" +Set up environment variables used to generate size report artifacts. + +Takes a single argument, a JSON dictionary of the `github` context. +Typically run as: + +``` + - name: Set up environment for size reports + env: + GH_CONTEXT: ${{ toJson(github) }} + run: gh_sizes_environment.py "${GH_CONTEXT}" +``` + +Sets the following environment variables: + +- `GH_EVENT_PR` For a pull request, the PR number; otherwise 0. +- `GH_EVENT_HASH` SHA of the commit under test. +- `GH_EVENT_PARENT` SHA of the parent commit to which the commit under + test is applied. +""" + +import json +import os +import re +import subprocess +import sys + +github = json.loads(sys.argv[1]) + +if github['event_name'] == 'pull_request': + pr = github['event']['number'] + commit = github['event']['pull_request']['head']['sha'] + r = subprocess.run(['git', 'show', '--no-patch', '--format=%s', 'HEAD'], + capture_output=True, text=True, check=True) + m = re.fullmatch('Merge [0-9a-f]+ into ([0-9a-f]+)', r.stdout) + if m: + parent = m.group(1) + else: + parent = github['event']['pull_request']['base']['sha'] +else: + pr = 0 + commit = github['sha'] + parent = github['event']['before'] + +# Environment variables for subsequent workflow steps are set by +# writing to the file named by `$GITHUB_ENV`. + +env = os.environ.get('GITHUB_ENV') +assert env +with open(env, 'at') as out: + print(f'GH_EVENT_PR={pr}', file=out) + print(f'GH_EVENT_HASH={commit}', file=out) + print(f'GH_EVENT_PARENT={parent}', file=out)