GitHub Actions:带有输入的调度和计划工作流程

GitHub81 次阅读5 分钟

最近编写了一个项目,需要每天定时从 producthunt 抓取数据并生成阅读良好的 markdown 文档,项目已经上线,[参考地址](https://indiehack.org/)

除此之外,还希望能够输入日期,手动触发抓取这一天的数据。

也就是,工作流有输入。当工作流由 cron 触发时,我需要它使用一些默认变量运行。当手动触发时,我想允许用户覆盖一些默认值。

image.png

Github Actions 工作流示例

比如:有如下 github actions 工作流:

name: Print some text every 3rd minute
on:
  schedule:
    - cron: '1/3 * * * *'

jobs:
  print_text:
    runs-on: 'ubuntu-20.04'
    steps:
      -name: Print some text
       run: echo "Periodically printing passages"

每隔一段时间,输出一段文字Periodically printing passages

如果手动触发时,允许输入参数覆盖这一段文字,可以在定义工作流中,使用 inputs 来定义可以接收的输入,参考如下:

name: Print some text every 3rd minute
on:
  schedule:
    - cron: '1/3 * * * *'
  workflow_dispatch: # adding the workflow_dispatch so it can be triggered manually
    inputs:
      text_to_print:
        description: 'What text do you want to print?'
        required: true
        default: 'Periodically printing passages'

jobs:
  print_text:
    runs-on: 'ubuntu-20.04'
    steps:
      - name: Set the variables
        env:
          DEFAULT_MESSAGE: 'Periodically printing passages' # here is the default message
        run: echo "MESSAGE=${{ github.event.inputs.text_to_print || env.DEFAULT_MESSAGE }}" >> $GITHUB_ENV

      - name: Print some text
        run: echo "$MESSAGE" # prints the environmental variable, MESSAGE

我可以在 GitHub 中的 Actions 标签中触发此操作:

image.png

让这个工作发挥作用的关键是:

echo "MESSAGE=${{ github.event.inputs.text\_to\_print || env.DEFAULT\_MESSAGE }}" >> $GITHUB\_ENV

上面一行选择将定义的变量赋值给变量,MESSAGE然后将其注入到变量中$GITHUB\_ENV。我们可以在Print some text步骤中引用它。

参考资料

使用 Github Action自动执行 Netlify 部署

继续阅读

基于全文检索与主题相似度