自定义 GitHub Actions 中的新输出格式,因为 set-output 将被弃用

Aja*_*y k 3 github github-actions

根据 GitHub 最近的公告,github 操作set-output将于明年被弃用。我能够echo "{name}={value}" >> $GITHUB_OUTPUT在工作流程文件中使用新格式 ( ),并且它有效。但我们还有一个用 python 编写的自定义 GitHub 操作(私有操作),我们也将set-output其用作main.py

                output_matrix = json.dumps(jsondata)
                print(f"::set-output name=output_matrix::{output_matrix}")
Run Code Online (Sandbox Code Playgroud)

到目前为止我尝试过的是:

print(f"output_matrix = {output_matrix} >> $GITHUB_OUTPUT")

但这似乎没有帮助。我们如何set-output在自定义 github 操作 python 文件中使用新格式的任何解决方案。

Gui*_*urd 5

我在这里使用这个工作流程实现和这个python 脚本进行了测试。

您可以在python 脚本中对输出变量执行与环境变量相同的实现。

环境变量的示例:(是关于它的另一个主题

import os

env_file = os.getenv('GITHUB_ENV')

hello='hello'

with open(env_file, "a") as myfile:
    myfile.write(f"TEST={hello}")
Run Code Online (Sandbox Code Playgroud)

输出变量的示例:

import os

output_file = os.getenv('GITHUB_OUTPUT')

hello='hello'
    
with open(output_file, "a") as myfile:
    myfile.write(f"TEST={hello}")
Run Code Online (Sandbox Code Playgroud)

请注意,此实施甚至在有关折旧的更新之前就可以运行::set-output