Github Actions报错:Input required and not supplied: image 求助
解决Github Actions部署Amazon ECS时的"Input required and not supplied: image"错误
问题场景
使用Github Actions部署Amazon ECS时触发错误:
Error: Input required and not supplied: image
原始Workflow配置如下:
name: Deploy to Amazon ECS on: push: tags: - '*' env: AWS_REGION: ap-southeast-1 ECR_REPOSITORY: web ECS_SERVICE: social-media-app ECS_CLUSTER: dev-cluster ECS_TASK_DEFINITION: .aws/task-definition.json CONTAINER_NAME: social-media-app defaults: run: shell: bash jobs: build: name: build image and push runs-on: ubuntu-latest permissions: packages: write contents: read steps: - name: Checkout uses: actions/checkout@v2 - name: Set Tag Number id: tag-number run: echo ::set-output name=tag::${GITHUB_REF#refs/*/} - name: Check Tag Release env: RELEASE_VERSION: ${{ steps.tag-number.outputs.tag }} run: | echo $RELEASE_VERSION echo ${{ steps.vars.outputs.tag }} - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v1 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: ${{ env.AWS_REGION }} - name: Login to Amazon ECR id: login-ecr uses: aws-actions/amazon-ecr-login@v1 - name: Build, tag, and push image to Amazon ECR id: build-image env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} IMAGE_TAG: ${{ steps.tag-number.outputs.tag }} run: | docker build --file ./Dockerfile -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" deploy-backend: name: deploy image runs-on: ubuntu-latest needs: build steps: - name: Checkout uses: actions/checkout@v2 - name: Fill in the new image ID in the Amazon ECS task definition id: task-def uses: aws-actions/amazon-ecs-render-task-definition@v1 with: task-definition: ${{ env.ECS_TASK_DEFINITION }} container-name: ${{ env.CONTAINER_NAME }} image: ${{ needs.build.outputs.image }} - name: Deploy Amazon ECS task definition uses: aws-actions/amazon-ecs-deploy-task-definition@v1 with: task-definition: ${{ steps.task-def.outputs.task-definition }} service: ${{ env.ECS_SERVICE }} cluster: ${{ env.ECS_CLUSTER }} wait-for-service-stability: false
尝试添加以下配置但未解决问题:
outputs: # Define the output variable 'image' for use in other jobs image: ${{ steps.image-out.outputs.image }}
错误原因
- Build任务未暴露输出变量:
build-image步骤虽尝试输出镜像地址,但未在buildjob层面定义outputs,导致deploy-backend任务无法通过needs.build.outputs.image获取值。 - 无效的输出引用:尝试添加的outputs配置中,引用了不存在的
steps.image-out步骤(实际步骤ID为build-image),无法正确关联输出。 - 弃用的输出语法:使用了已被Github Actions弃用的
::set-output命令,应替换为当前推荐的$GITHUB_OUTPUT方式。
修复步骤
步骤1:在Build Job中定义Outputs
在build job下添加outputs字段,将build-image步骤的输出作为整个job的输出:
jobs: build: name: build image and push runs-on: ubuntu-latest permissions: packages: write contents: read # 新增:定义job输出 outputs: image: ${{ steps.build-image.outputs.image }} steps: # ... 其他步骤保持不变
步骤2:替换为新的输出语法
将build-image步骤中的旧输出命令替换为新语法:
- name: Build, tag, and push image to Amazon ECR id: build-image env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} IMAGE_TAG: ${{ steps.tag-number.outputs.tag }} run: | docker build --file ./Dockerfile -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG # 替换旧语法为新的输出方式 echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
步骤3:同步Set Tag Number步骤的输出语法
同样将Set Tag Number步骤的旧语法替换为新方式:
- name: Set Tag Number id: tag-number run: echo "tag=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT
完整修复后的Workflow配置
name: Deploy to Amazon ECS on: push: tags: - '*' env: AWS_REGION: ap-southeast-1 ECR_REPOSITORY: web ECS_SERVICE: social-media-app ECS_CLUSTER: dev-cluster ECS_TASK_DEFINITION: .aws/task-definition.json CONTAINER_NAME: social-media-app defaults: run: shell: bash jobs: build: name: build image and push runs-on: ubuntu-latest permissions: packages: write contents: read # 定义job输出,供后续deploy任务使用 outputs: image: ${{ steps.build-image.outputs.image }} steps: - name: Checkout uses: actions/checkout@v2 - name: Set Tag Number id: tag-number run: echo "tag=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT - name: Check Tag Release env: RELEASE_VERSION: ${{ steps.tag-number.outputs.tag }} run: | echo $RELEASE_VERSION # 移除无效的vars步骤引用 # echo ${{ steps.vars.outputs.tag }} - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v1 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: ${{ env.AWS_REGION }} - name: Login to Amazon ECR id: login-ecr uses: aws-actions/amazon-ecr-login@v1 - name: Build, tag, and push image to Amazon ECR id: build-image env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} IMAGE_TAG: ${{ steps.tag-number.outputs.tag }} run: | docker build --file ./Dockerfile -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT deploy-backend: name: deploy image runs-on: ubuntu-latest needs: build steps: - name: Checkout uses: actions/checkout@v2 - name: Fill in the new image ID in the Amazon ECS task definition id: task-def uses: aws-actions/amazon-ecs-render-task-definition@v1 with: task-definition: ${{ env.ECS_TASK_DEFINITION }} container-name: ${{ env.CONTAINER_NAME }} image: ${{ needs.build.outputs.image }} - name: Deploy Amazon ECS task definition uses: aws-actions/amazon-ecs-deploy-task-definition@v1 with: task-definition: ${{ steps.task-def.outputs.task-definition }} service: ${{ env.ECS_SERVICE }} cluster: ${{ env.ECS_CLUSTER }} wait-for-service-stability: false
额外注意点
- 移除了
Check Tag Release步骤中无效的steps.vars.outputs.tag引用,避免日志报错。 - 确保
task-definition.json路径正确,且容器名称与CONTAINER_NAME环境变量完全匹配。
内容的提问来源于stack exchange,提问作者ryan
相关产品推荐
相关产品推荐

