Github Actions - 更新 HTML 文件

4
我有一个静态网站,现在正在使用Github Actions进行部署。该网站包括1个HTML文件和几个.yaml配置文件。有一个简单的JavaScript函数来加载所有的.yaml文件。但目前.yaml文件列表是硬编码的。是否可以通过以下方式之一实现更新JavaScript数组并在部署网站前使用Github Actions:
  1. 使用Github Actions更新JavaScript数组
  2. 使用JavaScript根据配置目录中的文件动态创建列表
目录结构
index.html
config/
|-- first.yaml
|-- second.yaml
|-- ...

index.html

...
<script>
// how can we dynamically create this array?
var files = [
  {"name": "first.yaml"},
  {"name": "second.yaml"},
  ...
]
</script>

也许可以执行 ls config 命令并将其导入到一个 sed 命令中,以替换 HTML 文件中的文件? - rethab
2个回答

0

我从之前的答案中整合了一个解决方案,这样你就可以更好地想象它。

首先,工作流程会遍历配置目录中的yaml文件,并将它们打印为字符串:"config1.yaml","config2.yaml"

然后使用提到的bluwy/substitute-string-action@v1操作将其替换为index.html文件中的内容。

这只是一个原始的解决方案,仅用于展示如何解决此问题。实际的解决方案应该更加复杂。

您可以在我的test action中查看“运行cat index.html”步骤。

index.html

<script type="text/javascript">
  var files = [%%files%%]
</script>

工作流程:

jobs:
  get_config:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Get files from config folder
        id: dir
        run: echo "::set-output name=files::$(ls -m config | sed -r 's/([a-z0-9]+\.ya?ml)/"\1"/gi')"

      - name: Replace file in index.html
        uses: bluwy/substitute-string-action@v1
        with:
          _input-file: 'index.html'
          _output-file: 'index.html'
          _format-key: '%%key%%'
          files: ${{ steps.dir.outputs.files }}

0

你好 - 你能否提供一个符合我问题结构的例子吗?我很难理解这是如何工作的。谢谢。 - Daniel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接