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

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

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

  1. name: Deploy
  2. on:
  3. workflow_call:
  4. inputs:
  5. bucketFolder:
  6. type: string
  7. jobs:
  8. deploy:
  9. runs-on: ubuntu-latest
  10. strategy:
  11. matrix:
  12. node-version: [18]
  13. steps:
  14. - uses: actions/checkout@v3
  15. - uses: pnpm/action-setup@v2
  16. with:
  17. version: 7
  18. - uses: actions/setup-node@v3
  19. with:
  20. node-version: ${{ matrix.node-version }}
  21. cache: "pnpm"
  22. - name: Configure AWS Credentials
  23. uses: aws-actions/configure-aws-credentials@v1-node16
  24. with:
  25. aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
  26. aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  27. aws-region: us-east-1
  28. - name: Install modules
  29. run: pnpm install
  30. - name: Build application
  31. run: pnpm run build
  32. - name: Deploy to S3
  33. run: aws s3 sync ./dist/ s3://${{ secrets.BUCKET_ID }}/${{ inputs.bucketFolder }}
  34. - name: Create CloudFront invalidation
  35. uses: nick-fields/retry@v2
  36. with:
  37. max_attempts: 6
  38. retry_on: error
  39. timeout_seconds: 60
  40. retry_wait_seconds: 600
  41. command: aws cloudfront create-invalidation --distribution-id ${{ secrets.DISTRIBUTION_ID }} --paths "/*"

// .github/workflows/production.yml

  1. name: Deploy - Production
  2. on:
  3. push:
  4. branches:
  5. - main
  6. workflow_dispatch:
  7. branches:
  8. - main
  9. jobs:
  10. awsDeploy:
  11. runs-on: ubuntu-latest
  12. steps:
  13. - uses: actions/checkout@v3
  14. - uses: ./.github/workflows/deploy.yml
  15. with:
  16. bucketFolder: production

// .github/workflows/staging.yml

  1. name: Deploy - Staging
  2. on:
  3. push:
  4. branches:
  5. - staging
  6. workflow_dispatch:
  7. branches:
  8. - staging
  9. jobs:
  10. awsDeploy:
  11. runs-on: ubuntu-latest
  12. steps:
  13. - uses: actions/checkout@v3
  14. - uses: ./.github/workflows/deploy.yml
  15. with:
  16. 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 必须在作业级别指定:

  1. jobs:
  2. awsDeploy:
  3. uses: ./.github/workflows/deploy.yml
  4. with:
  5. 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:

  1. jobs:
  2. awsDeploy:
  3. uses: ./.github/workflows/deploy.yml
  4. with:
  5. bucketFolder: staging

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

答案2

得分: 0

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

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

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

  1. name: 部署 - 暂存
  2. on:
  3. push:
  4. branches:
  5. - 暂存
  6. workflow_dispatch:
  7. branches:
  8. - 暂存
  9. jobs:
  10. awsDeploy:
  11. uses: ./.github/workflows/deploy.yml
  12. with:
  13. 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:

  1. name: Deploy - Staging
  2. on:
  3. push:
  4. branches:
  5. - staging
  6. workflow_dispatch:
  7. branches:
  8. - staging
  9. jobs:
  10. awsDeploy:
  11. uses: ./.github/workflows/deploy.yml
  12. with:
  13. 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:

确定