0

我正在尝试打包一个捆绑包以上传到 Google Cloud。我有一个pkg_web我所做的角度构建的输出,如果我传递到我正在生成的这个自定义规则中,File它是一个作为文件目录的对象。我正在生成的自定义规则需要 app.yaml 等,以及捆绑包和上传。

但是,捆绑包变成了一个目录,我需要扩展该目录的文件以在命令的根目录中上传。

例如:

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

我需要:

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

我的规则:

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

规则实现:

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)]

谢谢你的想法!

4

1 回答 1

0

似乎有点过分,但我结束了使用自定义 shell 命令来完成此操作:

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",
            )
    ....
  
``
于 2021-03-02T19:00:21.830 回答