我想使用 GitHub Actions 调用带有目录列表的 shell 脚本。 (本质上等同于将 Ansible vars 列表传递给 shell 脚本)
我真的不知道怎么做,这甚至可能吗?这是我到目前为止所拥有的,如何改进它?
name: CI
on:
push:
branches:
- master
tags:
- v*
pull_request:
jobs:
run-script:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Run script on targets
run: ./.github/workflows/script.sh {{ targets }}
env:
targets:
- FolderA/SubfolderA/
- FolderB/SubfolderB/
最佳答案
今天我能够使用以下 YAML(截断的)执行此操作:
...
with:
targets: |
FolderA/SubfolderA
FolderB/SubfolderB
实际的 GitHub Action 将其作为参数传递,如下所示:
runs:
using: docker
image: Dockerfile
args:
- "${{ inputs.targets }}"
它所做的只是将参数作为嵌入了换行符的字符串发送,然后可以通过以下 shell 代码以类似于 POSIX 兼容的方式对它进行迭代:
#!/bin/sh -l
targets="${1}"
for target in $targets
do
echo "Proof that this code works: $target"
done
如果我正确理解问题,它应该能够完成您想要的任务。如果您的用例需要,您始终可以在循环中运行类似 sh ./script.sh $target
的东西。
关于yaml - GitHub Actions 将变量列表传递给 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65345432/