Is there a bazel rule that supports publishing a go binary?

427 Views Asked by At

On successful build of a go binary (cli) I would like to publish the binary to a repository manager e.g. Artifactory. I have found various references to uploading jar dependencies but nothing specific to the rules_go

Can anyone point me in the right direction?

2

There are 2 best solutions below

0
On

I don't think there is a publish story for go. It took 4 years to implement #1372, it's probably easier to leave the publish part out of the Bazel whose mission is to build.

0
On

Thanks for the followup. I kept digging and did not find a solutions but came up with the following.

def _local_deploy_impl(ctx):
    target = ctx.attr.target
    shell_commands = ""

    for s in ctx.files.srcs:
        shell_commands += "sudo cp %s %s\n" % (s.short_path, target) # <2>

    ctx.actions.write(
        output = ctx.outputs.executable,
        is_executable = True,
        content = shell_commands,
    )
    runfiles = ctx.runfiles(files = ctx.files.srcs)
    return DefaultInfo(
        executable = ctx.outputs.executable,
        runfiles = runfiles,
    )

local_deploy = rule(
    executable = True,
    implementation = _local_deploy_impl,
    attrs = {
        "srcs": attr.label_list(allow_files = True),
        "target": attr.string(default = "/usr/local/bin", doc = "Deployment target directory"),
    },
)

Including in a build file:

local_deploy(
    name = "install",
    srcs = [":binary"],
)

srcs references the binary outputs of another rule that will be installed locally.

Any suggestions to improve?