如何从同一文件夹中调用可重用的 GitHub 工作流

huangapple go评论64阅读模式
英文:

How to call reusable GitHub workflow from same folder

问题

无法找到'/home/runner/work/{repository-name}/{repository-name}/.github/workflows/deploy.yml'路径下的'action.yml'、'action.yaml'或'Dockerfile'文件。在运行本地操作之前,您是否忘记运行actions/checkout?问题是否出在路径中使用了'{repository-name}/{repository-name}'?如果是的话,我该如何修复它?如果不是,那么问题出在哪里?

(Note: The code part remains the same and is not translated.)

英文:

I am currently trying to call a reusable workflow from two other workflows both located in the same directory and the same private repository as the reusable workflow. The files look like this:

// .github/workflows/deploy.yml

name: Deploy
on:
    workflow_call:
        inputs:
            bucketFolder:
                type: string
jobs:
    deploy:
        runs-on: ubuntu-latest
        strategy:
            matrix:
                node-version: [18]
        steps:
            - uses: actions/checkout@v3
            - uses: pnpm/action-setup@v2
              with:
                  version: 7
            - uses: actions/setup-node@v3
              with:
                  node-version: ${{ matrix.node-version }}
                  cache: "pnpm"
            - name: Configure AWS Credentials
              uses: aws-actions/configure-aws-credentials@v1-node16
              with:
                  aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
                  aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
                  aws-region: us-east-1
            - name: Install modules
              run: pnpm install
            - name: Build application
              run: pnpm run build
            - name: Deploy to S3
              run: aws s3 sync ./dist/ s3://${{ secrets.BUCKET_ID }}/${{ inputs.bucketFolder }}
            - name: Create CloudFront invalidation
              uses: nick-fields/retry@v2
              with:
                  max_attempts: 6
                  retry_on: error
                  timeout_seconds: 60
                  retry_wait_seconds: 600
                  command: aws cloudfront create-invalidation --distribution-id ${{ secrets.DISTRIBUTION_ID }} --paths "/*"

// .github/workflows/production.yml

name: Deploy - Production
on:
    push:
        branches:
            - main
    workflow_dispatch:
        branches:
            - main
jobs:
    awsDeploy:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v3
            - uses: ./.github/workflows/deploy.yml
              with:
                  bucketFolder: production

// .github/workflows/staging.yml

name: Deploy - Staging
on:
    push:
        branches:
            - staging
    workflow_dispatch:
        branches:
            - staging
jobs:
    awsDeploy:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v3
            - uses: ./.github/workflows/deploy.yml
              with:
                  bucketFolder: staging

Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under '/home/runner/work/{repository-name}/{repository-name}/.github/workflows/deploy.yml'. Did you forget to run actions/checkout before running your local action?

Is the fact it is using {repository-name}/{repository-name} in the path the issue and if it is how do I fix it? If not, what is going wrong here?

答案1

得分: 3

指定步骤级别的uses,就像您在示例中所做的那样,告诉解析器查找GitHub操作而不是工作流程(这就是为什么会出现Can't find 'action.yml'错误消息的原因)。

要调用可重用工作流,uses 必须在作业级别指定:

jobs:
    awsDeploy:
        uses: ./.github/workflows/deploy.yml
        with:
            bucketFolder: staging

要了解更多详细信息,请查看调用可重用工作流文档。

英文:

When you specify uses at the step level, as you've done with your examples, this tells the parser to look for a GitHub Action not a workflow (which is why you are getting the Can't find 'action.yml' error message).

In order to call a reusable workflow, the uses must be specified at the job level:

jobs:
    awsDeploy:
        uses: ./.github/workflows/deploy.yml
        with:
            bucketFolder: staging

For more details, take a look at the Calling a reusable workflow documentation.

答案2

得分: 0

我找到了解决方案,就在发布后不久。来自文档:

“您可以使用 uses 关键字调用可重用的工作流程。与在工作流程内使用操作不同,您直接在作业中调用可重用工作流程,而不是在作业步骤内调用。”

这是一个可重用的工作流程,而不是一个操作,所以我只需移除 stepsuses: actions/checkout@v3runs-on,如下所示:

name: 部署 - 暂存
on:
    push:
        branches:
            - 暂存
    workflow_dispatch:
        branches:
            - 暂存
jobs:
    awsDeploy:
        uses: ./.github/workflows/deploy.yml
        with:
            bucketFolder: 暂存
英文:

I found the solution shortly after posting. From the docs:

You call a reusable workflow by using the uses keyword. Unlike when you are using actions within a workflow, you call reusable workflows directly within a job, and not from within job steps.

It was a reusable workflow, not an action, so I just had to remove the steps, uses: actions/checkout@v3 and runs-on like so:

name: Deploy - Staging
on:
    push:
        branches:
            - staging
    workflow_dispatch:
        branches:
            - staging
jobs:
    awsDeploy:
        uses: ./.github/workflows/deploy.yml
        with:
            bucketFolder: staging

huangapple
  • 本文由 发表于 2023年3月9日 23:59:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定