From 47018bc3cebf3b3fffa00c513bb4d8f2d1cba9cc Mon Sep 17 00:00:00 2001 From: Illia Vysochyn Date: Fri, 9 Aug 2024 13:20:33 +0200 Subject: [PATCH] CI: Automatically extract Docker tag Adds a script to extract Docker image tag from MODULE.bazel.lock file. Utilizes extracted Docker image tag to execute "Local Flow" CI workflow. Signed-off-by: Illia Vysochyn --- .github/scripts/extract_docker_revision.py | 65 ++++++++++++++++++++++ .github/workflows/ci.yml | 23 +++++--- MODULE.bazel | 1 - 3 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 .github/scripts/extract_docker_revision.py diff --git a/.github/scripts/extract_docker_revision.py b/.github/scripts/extract_docker_revision.py new file mode 100644 index 0000000..603587f --- /dev/null +++ b/.github/scripts/extract_docker_revision.py @@ -0,0 +1,65 @@ +import json +from pathlib import Path +import sys +from typing import Optional + + +def find_docker_orfs(data: dict) -> Optional[dict]: + """ + Recursively search for the 'docker_orfs' configuration in + the provided JSON data. + + Parameters + ---------- + data : dict + The JSON data to search for the 'docker_orfs' configuration. + + Returns + ------- + Optional[dict] + The 'docker_orfs' configuration if found, otherwise None. + """ + if isinstance(data, dict): + if 'docker_orfs' in data: + return data['docker_orfs'] + for key, value in data.items(): + result = find_docker_orfs(value) + if result: + return result + elif isinstance(data, list): + for item in data: + result = find_docker_orfs(item) + if result: + return result + return None + + +if __name__ == "__main__": + if len(sys.argv) < 2 or not Path(sys.argv[1]).exists(): + print(f"Usage: {sys.argv[0]} ") + exit(1) + + with open(sys.argv[1], 'r') as file: + try: + data = json.load(file) + except json.JSONDecodeError: + print(f"Unable to parse JSON data from {sys.argv[1]}") + exit(1) + + docker_orfs = find_docker_orfs(data) + if docker_orfs is None or docker_orfs.get('attributes', None) is None: + print("Unable to find 'docker_orfs' configuration in the provided file") # noqa: E501 + exit(1) + + docker_tag = docker_orfs.get('attributes').get('image', None) + docker_sha256 = docker_orfs.get('attributes').get('sha256', None) + + if docker_tag is None or docker_sha256 is None: + print("Unable to find Docker tag or SHA256 in the provided file.") + exit(1) + + print(json.dumps({ + 'image': f"{docker_tag}@sha256:{docker_sha256}", + 'tag': docker_tag, + 'sha256': docker_sha256 + })) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 13f007d..eb680ee 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,6 +23,20 @@ jobs: run: | buildifier -lint warn -r . + generate-config: + name: Generate configs + runs-on: ubuntu-latest + outputs: + docker-tag: ${{ steps.docker-image.outputs.docker-orfs }} + steps: + - name: Checkout megaboom + uses: actions/checkout@v4 + - name: Lint Bazel files + run: bazel mod tidy && git diff --exit-code + - name: Extract Docker tag + id: docker-image + run: echo "docker-orfs=$(python3 .github/scripts/extract_docker_revision.py MODULE.bazel.lock)" | tee -a $GITHUB_OUTPUT + test-make-target: name: ${{ matrix.STAGE_TARGET }} runs-on: ubuntu-22.04 @@ -68,18 +82,15 @@ jobs: run: | bazel run --subcommands --verbose_failures --sandbox_debug ${{ matrix.STAGE_TARGET }} -- `pwd`/build - test-target-local: name: Local flow - test targets runs-on: ubuntu-22.04 + needs: generate-config container: - # NOTE: Ensure this is kept in sync with MODULE.bazel - image: openroad/orfs:v3.0-1114-g46acc762@sha256:ae4df23391c26bcc48a506f8e0d0da73742d1b6cb3b1dc02f4d5ea71170195b5 + image: ${{ fromJSON(needs.generate-config.outputs.docker-tag).image }} defaults: run: shell: bash - strategy: - fail-fast: false env: DEBIAN_FRONTEND: "noninteractive" FLOW_HOME: /OpenROAD-flow-scripts/flow @@ -122,8 +133,6 @@ jobs: defaults: run: shell: bash - strategy: - fail-fast: false env: DEBIAN_FRONTEND: "noninteractive" steps: diff --git a/MODULE.bazel b/MODULE.bazel index 19f6b65..2156408 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -23,7 +23,6 @@ git_override( #) orfs = use_extension("@bazel-orfs//:extension.bzl", "orfs_repositories") -# NOTE: Ensure this is kept in sync with CI orfs.default( image = "openroad/orfs:v3.0-1114-g46acc762", sha256 = "ae4df23391c26bcc48a506f8e0d0da73742d1b6cb3b1dc02f4d5ea71170195b5",