Skip to content

New Copybara Github Action & Docker image #137

@politician

Description

@politician

Hey guys,

Thank you for the great work with Copybara. I recently learnt about it and decided to create a Copybara GitHub Action to make it really easy to use with as little config as possible.

It can be dropped in any GitHub repo and will work out of the box for GitHub <=> GitHub.

My action's repo is also building nightly a Copybara Docker image straight from the official repo (google/copybara). Check out the source here.

Here is the copy.bara.sky I am using as a template under the hood. Any advice on how to improve it is more than welcome:

# Variables
SOT_REPO = "git@github.com:olivr/copybara-action.git"
SOT_BRANCH = "main"
DESTINATION_REPO = "git@github.com:olivr/copybara-action-test.git"
DESTINATION_BRANCH = "main"
COMMITTER = "Github Actions <actions@github.com>"
LOCAL_SOT = "file:///usr/src/app"

push_include = ["**"]
PUSH_EXCLUDE_FILES = []
PUSH_TRANSFORMATIONS = []

pr_include = ["**"]
pr_exclude = []
PR_TRANSFORMATIONS = []

# Push workflow
core.workflow(
    name = "push",
    origin = git.origin(
        url = LOCAL_SOT if LOCAL_SOT else SOT_REPO,
        ref = SOT_BRANCH,
    ),
    destination = git.github_destination(
        url = DESTINATION_REPO,
        push = DESTINATION_BRANCH,
    ),
    origin_files = glob(push_include, exclude = PUSH_EXCLUDE_FILES),
    authoring = authoring.pass_thru(default = COMMITTER),
    mode = "ITERATIVE",
    transformations = [
        metadata.restore_author("ORIGINAL_AUTHOR", search_all_changes = True),
        metadata.expose_label("COPYBARA_INTEGRATE_REVIEW"),
    ] + PUSH_TRANSFORMATIONS if PUSH_TRANSFORMATIONS else core.reverse(PR_TRANSFORMATIONS),
)

# Pull Request workflow
core.workflow(
    name = "pr",
    origin = git.github_pr_origin(
        url = DESTINATION_REPO,
        branch = DESTINATION_BRANCH,
    ),
    destination = git.github_pr_destination(
        url = SOT_REPO,
        destination_ref = SOT_BRANCH,
        integrates = [],
    ),
    destination_files = glob(push_include, exclude = PUSH_EXCLUDE_FILES),
    origin_files = glob(pr_include if pr_include else ["**"], exclude = pr_exclude),
    authoring = authoring.pass_thru(default = COMMITTER),
    mode = "CHANGE_REQUEST",
    set_rev_id = False,
    transformations = [
        metadata.save_author("ORIGINAL_AUTHOR"),
        metadata.expose_label("GITHUB_PR_NUMBER", new_name = "Closes", separator = DESTINATION_REPO.replace("git@github.com:", " ").replace(".git", "#")),
    ] + PR_TRANSFORMATIONS,
)

It is using a common flow:

 Source of Truth                  Destination

+---------------+   Copybara   +---------------+
|     Branch    +------------> |     Branch    |
+-------+-------+              +---------------+
        ^
        |
        |
+-------+-------+   Copybara   +---------------+
| Pull Requests | <------------+ Pull Requests |
+---------------+              +---------------+

I believe it would be helpful to add this template to your documentation and add a link to my GitHub Action, but I don't want to appear like I am self-promoting, if you agree please just let me know and I'll create a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions