简体   繁体   English

Github 工作流操作仅修改文件

[英]Github Workflow Actions Only Modified Files

For my projects PR process I want to add a code format check.对于我的项目 PR 流程,我想添加代码格式检查。 I am using a github marketplace linter, Black ( https://github.com/marketplace/actions/black-code-formatter ) to do this.我正在使用 github 市场 linter Black ( https://github.com/marketplace/actions/black-code-formatter ) 来执行此操作。 I have this working from the code example, but it runs for all files in the repo, instead of just the modified files in the PR.我从代码示例中进行了此操作,但它针对 repo 中的所有文件运行,而不仅仅是 PR 中修改后的文件。 Is there any way to change this?有什么办法可以改变这种情况吗?

I've run the example from the market place page in my main.workflow file and it works as expected我已经在我的main.workflow文件中运行了市场页面中的main.workflow ,它按预期工作

I think the issues has to do with the black call, right now it is: args = ". --check --fast" and this checks every file.我认为问题与黑色调用有关,现在是: args = ". --check --fast"并且这会检查每个文件。 I am not sure how to change this to only check formatting of modified files from the PR.我不确定如何将其更改为仅检查来自 PR 的修改文件的格式。

I don't think you can do what you want with that particular action.我不认为你可以用那个特定的动作做你想做的事。 In order to pass an argument to an action, you need to use either a literal or a context expression .为了将参数传递给操作,您需要使用文字或上下文表达式 However, to find the list of files, you'd need to use a shell pipeline, and it isn't possible to invoke the shell in a context expression.但是,要查找文件列表,您需要使用 shell 管道,并且不可能在上下文表达式中调用 shell。

If you wanted to add Black as a development dependency to your project, you could use the environment variables GITHUB_SHA and GITHUB_BASE_REF as described in the documentation and then run something like the following to invoke it:如果您想将 Black 作为开发依赖项添加到您的项目中,您可以使用文档中描述的环境变量GITHUB_SHAGITHUB_BASE_REF ,然后运行类似以下内容来调用它:

git diff --name-only "$GITHUB_BASE_REF..$GITHUB_SHA" | xargs black --check --fast

That assumes that the application is named black and that it can take an arbitrary number of files to check on the command line;这假设应用程序名为black ,并且可以在命令行上检查任意数量的文件; you may need to adjust it if that's not the case.如果不是这种情况,您可能需要调整它。 That command will exit nonzero if the check fails, and zero if every black invocation exited successfully.如果检查失败,该命令将非零退出,如果每个black调用都成功退出,则该命令将退出为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM