Skip to content

Commit

Permalink
yolo
Browse files Browse the repository at this point in the history
  • Loading branch information
multun committed May 29, 2024
1 parent bbe0d17 commit f99104c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
45 changes: 27 additions & 18 deletions .github/scripts/bake-metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand All @@ -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
Expand All @@ -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} ("
Expand All @@ -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}")
Expand All @@ -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)),
Expand Down Expand Up @@ -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,
Expand All @@ -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):
Expand All @@ -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__":
Expand Down
23 changes: 22 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f99104c

Please sign in to comment.