如何在可重用的 github 工作流程中执行远程脚本

Kal*_*rin 7 workflow github github-actions

我在一个名为的存储库中有这个工作流程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
Run Code Online (Sandbox Code Playgroud)

这是来自公共存储库的可重用工作流程文件git-workflows,其中包含应在其上运行的脚本。发生的情况是工作流程正在尝试使用存储库内的脚本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
Run Code Online (Sandbox Code Playgroud)

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

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

小智 6

解决此问题的一种方法是在可重用工作流程中执行签出,该工作流程实质上克隆了脚本所在的存储库的内容,只有这样您才能访问它。这不是最干净的解决方案,但它确实有效。

执行第二次签出,将具有可重用工作流程的存储库克隆到目录中reusable-workflow-repo

- name: Checkout reusable workflow dir
  uses: actions/checkout@v3
  with:
    repository: <your-org>/terraform-do-database
    token: ${{ secrets.GIT_ACCESS_TOKEN }}
    path: reusable-workflow-repo
Run Code Online (Sandbox Code Playgroud)

现在您已经拥有了所需的所有代码reusable-workflow-repo。用于${GITHUB_WORKSPACE}查找当前路径并将路径附加到脚本中。

- name: Verify the tag value
  run: ${GITHUB_WORKSPACE}/reusable-workflow-repo/scripts/tag_verify.sh
Run Code Online (Sandbox Code Playgroud)


Kal*_*rin 1

我能够解决这个问题,添加更多命令来手动下载脚本并执行它。

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
Run Code Online (Sandbox Code Playgroud)