You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitLab合并触发Pipeline报错:Template file not found at /builds/.../template.yml

解决GitLab Pipeline中SAM找不到模板文件的问题

问题原因

GitLab Runner默认在仓库根目录执行Pipeline命令,而你的template.yaml和项目文件都在/lambda_functions/app/子目录下,直接执行sam build会在根目录查找模板文件,自然找不到。本地部署正常是因为你在/lambda_functions/app/目录下运行命令,路径匹配。

修复方案

修改.gitlab-ci.yml的script部分,先切换到项目所在的子目录,再执行SAM命令:

image: python:3.12

stages:
  - deploy

production:
  stage: deploy
  before_script:
    - pip3 install awscli --upgrade
    - pip3 install aws-sam-cli --upgrade
  script:
    # 切换到项目文件所在目录
    - cd lambda_functions/app/
    - sam build
    - sam package --output-template-file output.yaml --s3-bucket my-bucket-name
    - sam deploy --template-file output.yaml --stack-name sam-test-1 --capabilities CAPABILITY_IAM
  environment: production

备选方案(不切换目录)

如果不想切换目录,可以在每个SAM命令中通过--template参数明确指定模板路径:

script:
  - sam build --template lambda_functions/app/template.yaml
  - sam package --template-file lambda_functions/app/template.yaml --output-template-file output.yaml --s3-bucket my-bucket-name
  - sam deploy --template-file output.yaml --stack-name sam-test-1 --capabilities CAPABILITY_IAM

注意:如果之前重命名过模板文件为template.yml,要把命令中的路径改成对应的文件名。

内容的提问来源于stack exchange,提问作者Matt Cremeens

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 04:30:58