From f99104cf8049f3fc2da49941e18937f09136d38f Mon Sep 17 00:00:00 2001 From: "Victor \"multun\" Collod" Date: Wed, 29 May 2024 18:25:15 +0200 Subject: [PATCH] yolo --- .github/scripts/bake-metadata.py | 45 +++++++++++++++++++------------- .github/workflows/build.yml | 23 +++++++++++++++- 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/.github/scripts/bake-metadata.py b/.github/scripts/bake-metadata.py index afda3c320b5..b14b4c9b7d1 100755 --- a/.github/scripts/bake-metadata.py +++ b/.github/scripts/bake-metadata.py @@ -86,6 +86,12 @@ def get_stable_tag(self, target: Target) -> str: version = self.get_stable_version() return self.tag(target, version) + def output_method(self): + return "registry" + + def get_output(self, target: Target) -> List[str]: + return ["type=registry"] + def get_tags(self, target: Target) -> List[str]: return [self.get_stable_tag(target)] @@ -98,10 +104,7 @@ def get_cache_from(self, target: Target) -> List[str]: @dataclass class PullRequestEvent(BaseEvent): - # the namespace is where images get pushed - # if the image is a fork, this can't be the target repo - namespace: str - + is_fork: bool pr_id: str pr_branch: str # the target branch name @@ -114,9 +117,6 @@ class PullRequestEvent(BaseEvent): # the target branch commit hash target_hash: str - def get_namespace(self): - return self.namespace - def version_string(self): return ( f"pr {self.pr_id} (" @@ -128,6 +128,16 @@ def get_stable_version(self) -> str: # edge/osrd-front:pr-42-HASH-nginx return f"pr-{self.pr_id}-{self.merge_hash}" + def output_method(self): + if not self.is_fork: + return super().output_method() + return "artifact" + + def get_output(self, target: Target) -> List[str]: + if not self.is_fork: + return super().get_output(target) + return [f"type=docker,dest=osrd-{target.name}.tar"] + def pr_tag(self, target: Target) -> str: # edge/osrd-front:pr-42-nginx # pr-42 (merge of XXXX into XXXX) return self.tag(target, f"pr-{self.pr_id}") @@ -136,9 +146,13 @@ def get_tags(self, target: Target) -> List[str]: return [*super().get_tags(target), self.pr_tag(target)] def get_cache_to(self, target: Target) -> List[str]: + if self.is_fork: + return [] return [registry_cache(self.pr_tag(target))] def get_cache_from(self, target: Target) -> List[str]: + if self.is_fork: + return [] return [ registry_cache(self.tag(target, self.target_branch)), registry_cache(self.pr_tag(target)), @@ -261,17 +275,8 @@ def parse_event(context) -> Event: target_branch = context["base_ref"] orig_hash, target_hash = parse_merge_commit(commit_hash) repo_ctx = context["event"]["pull_request"]["head"]["repo"] - # if the repository is a fork, packages cannot be pushed to - # the upstream registry. Instead, we push to the fork's registry. - # The default namespace is suffixed with -edge, hence the special case - namespace = DEFAULT_EDGE_NAMESPACE - if repo_ctx["fork"]: - # multun/osrd - repo_name = repo_ctx["full_name"] - # ghcr wants lowercase org names - namespace = f"ghcr.io/{repo_name.lower()}" return PullRequestEvent( - namespace=namespace, + is_fork=repo_ctx["fork"], pr_id=parse_pr_id(ref), pr_branch=context["head_ref"], target_branch=target_branch, @@ -286,7 +291,10 @@ def generate_bake_file(event, targets): bake_targets = {} for target in targets: # TODO: add labels - target_manifest = {"tags": event.get_tags(target)} + target_manifest = { + "tags": event.get_tags(target), + "output": event.get_output(target), + } if cache_to := event.get_cache_to(target): target_manifest["cache-to"] = cache_to if cache_from := event.get_cache_from(target): @@ -311,6 +319,7 @@ def main(): stable_tags[target.name] = event.get_stable_tag(target) print(f"stable_version={event.get_stable_version()}", file=f) print(f"stable_tags={json.dumps(stable_tags)}", file=f) + print(f"output_method={event.output_method()}", file=f) if __name__ == "__main__": diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index cd3986f580d..2c66873a009 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,6 +19,7 @@ jobs: outputs: stable_tags: ${{ steps.bake-metadata.outputs.stable_tags }} stable_version: ${{ steps.bake-metadata.outputs.stable_version }} + output_method: ${{ steps.bake-metadata.outputs.output_method }} steps: - name: Checkout uses: actions/checkout@v4 @@ -72,7 +73,7 @@ jobs: for i in $(seq 1 3); do echo "::group::Try $i" - if docker buildx bake $BAKEFILE $METADATA --push 2>&1 | tee docker-build.log; then + if docker buildx bake $BAKEFILE $METADATA 2>&1 | tee docker-build.log; then echo "::endgroup::" exit 0 fi @@ -91,6 +92,13 @@ jobs: echo "All retries failed, exiting." exit 1 + - name: Upload artifact + uses: actions/upload-artifact@v4 + if: steps.bake-metadata.outputs.output_method == 'artifact' + with: + name: build + path: osrd-*.tar + check_generated_railjson_sync: runs-on: ubuntu-latest steps: @@ -332,6 +340,19 @@ jobs: - name: Checkout uses: actions/checkout@v4 + - name: Download built images + if: needs.build.outputs.output_method == 'artifact' + uses: actions/download-artifact@v4 + with: + name: build + path: . + + - name: Load built images + if: needs.build.outputs.output_method == 'artifact' + run: | + docker load --input ./osrd-*.tar + docker image ls -a + - name: Generate rtk bindings run: > docker run --rm --net=host -v $PWD/output:/app/tests/unit