英文:
GitHub Actions - Set two output names from custom action in Golang code
问题
我已经写了一个自定义的 Golang 动作,其中有一个函数可以生成两个输出名称。
这些输出名称是为了在后续的工作流中使用,所以我可以在一个作业中调用该动作("Calculate version"),捕获输出名称,并在另一个作业中使用它("Build")。
在动作中用于创建输出名称的 Go 函数如下:
func SetTag(value string){
fmt.Printf(`::set-output name=repo_tag::%s`, value)
fmt.Printf(`::set-output name=ecr_tag::%s`, "v" + value)
}
而使用该输出的工作流如下:
Version:
name: Calculate Version
runs-on: ubuntu-latest
outputs:
repo_version_env: ${{ steps.version.outputs.repo_tag }}
ecr_version_env: ${{ steps.version.outputs.ecr_tag }}
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Calculate version
id: version
uses: my_custom_action/version@v1
Build:
name: Build Image
runs-on: ubuntu-latest
steps:
- name: Build
run: |
echo ${{ needs.Version.outputs.ecr_version_env }}
echo ${{ needs.Version.outputs.repo_version_env }}
当我运行工作流时,echo 命令在构建输出中给出了以下结果:
1 echo
2 echo v1.4.1::set-output name=ecr_tag::1.4.1
而不是:
1 echo v1.4.1
2 echo 1.4.1
我做错了什么?
如果有更好的方法来在工作流中使用从动作输出的变量,我也很乐意听取建议。
非常感谢。
英文:
I have written a custom action in Golang, which has a function that makes two output names.
The output names are created for later use in the workflow, so I can call the action in one job ("Calculate version"), catch the output names, and use it in a different job ("Build")
The Go function inside the action to create the output name is:
func SetTag(value string){
fmt.Printf(`::set-output name=repo_tag::%s`, value)
fmt.Printf(`::set-output name=ecr_tag::%s`, "v" + value)
}
And the workflow that uses the said output is like so:
Version:
name: Calculate Version
runs-on: ubuntu-latest
outputs:
repo_version_env: ${{ steps.version.outputs.repo_tag }}
ecr_version_env: ${{ steps.version.outputs.ecr_tag }}
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Calculate version
id: version
uses: my_custom_action/version@v1
Build:
name: Build Image
runs-on: ubuntu-latest
steps:
- name: Build
run: |
echo ${{ needs.Version.outputs.ecr_version_env }}
echo ${{ needs.Version.outputs.repo_version_env }}
When I run the workflow, The echo commands give me the following in the action build output:
1 echo
2 echo v1.4.1::set-output name=ecr_tag::1.4.1
Instead of:
1 echo v1.4.1
2 echo 1.4.1
What am I doing wrong?
If there are better ways to use the variables outputted from the action, between different jobs in the workflow I will be happy to hear as well.
Thanks a lot.
答案1
得分: 1
根据我在这里的测试,问题在于go函数会以这样的方式打印数据:
::set-output name=repo_tag::1.4.1::set-output name=ecr_tag::v1.4.1%
问题似乎与您没有换行有关。
通过更新Go函数,应该可以解决这个问题:
func SetTag(value string){
fmt.Printf(`::set-output name=repo_tag::%s`, value)
fmt.Print("\n")
fmt.Printf(`::set-output name=ecr_tag::%s`, "v" + value)
fmt.Print("\n")
}
请注意,第二个fmt.Print("\n")
将在终端上删除%
符号。
如果您想要检查我进行的测试:
输出结果与您期望的一致:
英文:
For what I tested here, the problem is that the go function will print the datas like this:
::set-output name=repo_tag::1.4.1::set-output name=ecr_tag::v1.4.1%
And the problem seems related to the fact that you didn't break the line.
It should work by updating the Go function to:
func SetTag(value string){
fmt.Printf(`::set-output name=repo_tag::%s`, value)
fmt.Print("\n")
fmt.Printf(`::set-output name=ecr_tag::%s`, "v" + value)
fmt.Print("\n")
}
Note that the second fmt.Print("\n")
will remove the %
symbol on the terminal.
If you want to check the test I made:
The output is the one you expect:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论