【问题标题】:How to execute a a remote script in a reusable github workflow如何在可重用的 github 工作流中执行远程脚本
【发布时间】:2022-06-17 22:00:02
【问题描述】:

我在一个名为 terraform-do-database 的仓库中有这个工作流程,我正在尝试使用来自公共仓库 foo/git-workflows/.github/workflows/tag_validation.yaml@master 的可重用工作流程

name: Tag Validation

on:
  pull_request:
    branches: [master]
  push:
    branches:    
      - '*'         # matches every branch that doesn't contain a '/'
      - '*/*'       # matches every branch containing a single '/'
      - '**'        # matches every branch
      - '!master'   # excludes master
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:

  tag_check:
    uses: foo/git-workflows/.github/workflows/tag_validation.yaml@master

这是来自公共 git-workflows 存储库的可重用工作流文件,其中包含应在其上运行的脚本。正在发生的事情是工作流正在尝试使用 repo terraform-do-database

中的脚本
name: Tag Validation

on:
  pull_request:
    branches: [master]
  workflow_call:

jobs:

  tag_check:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3

      # Runs a single command using the runners shell
      - name: Verify the tag value
        run: ./scripts/tag_verify.sh

所以问题是:如何让工作流使用存储在 git-worflows 存储库中的脚本而不是 terraform-do-database?

我想要一个可以调用工作流和脚本的存储库,我不想在我的所有存储库中复制所有内容。

【问题讨论】:

    标签: github github-actions workflow


    【解决方案1】:

    根据thread on github-community,脚本需要单独下载/检出。

    您发布的“可重用”工作流在这个意义上是不可重用的,因为它不下载脚本,所以工作流只能在其自己的存储库(或已经有脚本的存储库)中运行。

    【讨论】:

    • 嘿@SebDieBln 如何单独下载脚本?
    • @KalebyCadorin 这个thread on github-community 指出了一些下载文件的方法。
    【解决方案2】:

    我能够通过添加更多命令来手动下载脚本并执行它来解决它。

    steps:
          # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
          - uses: actions/checkout@v3
    
          # Runs a single command using the runners shell
          - name: Check current directory
            run: pwd
          - name: Download the script
            run: curl -o $PWD/tag_verify.sh https://raw.githubusercontent.com/foo/git-workflows/master/scripts/tag_verify.sh
          - name: Give script permissions
            run: chmod +x $PWD/tag_verify.sh
          - name: Execute script
            run: $PWD/tag_verify.sh
    

    【讨论】:

      【解决方案3】:

      我发现如果我将脚本包装成一个复合动作。我可以使用 GitHub 上下文 github.action_path 来定位脚本。 示例:

      run: ${{ github.action_path }}/scripts/foo.sh

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-23
        • 2020-04-02
        • 2012-05-30
        • 1970-01-01
        • 2012-05-13
        • 2018-07-07
        • 2022-01-21
        • 2013-07-30
        相关资源
        最近更新 更多