0

I'm trying to package a bundle for uploading to Google Cloud. I have an output of pkg_web from an angular build that I did, which, if I pass into this custom rule I'm generating, is a File object that is a directory of the files. The custom rule I am generating takes the app.yaml, etc, and the bundle, and uploads.

However, the bundle becomes a directory, and I need the files of that directory expanded for uploading in the root of command.

For example:

- bundle/index.html <-- bundle directory
- bundle/main.js
- app.yaml

and I need:

- index.html
- main.js
- app.yaml

My rule:

deploy(
  name = "deploy",
  srcs = [":bundle"] <-- pkg_web rule,
  yaml = ":app.yaml"
)

Rule implementation:

def _deploy_pkg(ctx):
    inputs = []
    inputs.append(ctx.file.yaml)
    inputs.extend(ctx.files.srcs)

    script_template = """
       #!/bin/bash
       gcloud app deploy {yaml_path}
    """
    script = ctx.actions.declare_file("%s-deploy" % ctx.label.name)
    ctx.actions.write(script, script_content, is_executable = True)

    runfiles = ctx.runfiles(files = inputs, transitive_files = depset(ctx.files.srcs))
    return [DefaultInfo(executable = script, runfiles = runfiles)]

Thank you for your ideas!

normmcgarry
  • 661
  • 2
  • 6
  • 22

1 Answers1

0

Seems a bit excessive, but I ended using a custom shell command to accomplish this:

def _deploy_pkg(ctx):
    inputs = []

    out = ctx.actions.declare_directory("out")
    yaml_out = ctx.actions.declare_file(ctx.file.yaml.basename)

    inputs.append(out)

    ctx.actions.run_shell(
        outputs = [yaml_out],
        inputs = [ctx.file.yaml],
        arguments = [ctx.file.yaml.path, yaml_out.path],
        progress_message = "Copying yaml to output directory.",
        command = "cp $1 $2",
    )

    for f in ctx.files.srcs:
        if f.is_directory:
            ctx.actions.run_shell(
                outputs = [out],
                inputs = [f],
                arguments = [f.path, out.path],
                progress_message = "Copying %s to output directory.".format(f.basename),
                command = "cp -a -R $1/* $2",
            )
        else:
            out_file = ctx.actions.declare_file(f.basename)
            inputs.append(out_file)
            ctx.actions.run_shell(
                outputs = [out_file],
                inputs = [f],
                arguments = [f.path, out_file.path],
                progress_message = "Copying %s to output directory.".format(f.basename),
                # This is what we're all about here. Just a simple 'cp' command.
                # Copy the input to CWD/f.basename, where CWD is the package where
                # the copy_filegroups_to_this_package rule is invoked.
                # (To be clear, the files aren't copied right to where your BUILD
                # file sits in source control. They are copied to the 'shadow tree'
                # parallel location under `bazel info bazel-bin`)
                command = "cp -a $1 $2",
            )
    ....
  
``
normmcgarry
  • 661
  • 2
  • 6
  • 22