Action fails when there are no SAM changes
Describe the bug When a change is pushed to main which doesn't update the SAM template, the deployment job will fail.
To Reproduce Steps to reproduce the behavior: The previous workflow must have completed successfully, and the repo hasn't been updated.
- Go to Actions
- Click on workflow "SAM Validate, Build, Test, Deploy"
- Choose "Re-run jobs"
This job will fail
Expected behavior The running job should succeed
Screenshots
Step "SAM deploy" fails with the following error:
(note that I ran this from my own fork, so the stack name is different to what you will expect)
A possible fix: Change the last command in template.yml to the following:
sam deploy --template-file .aws-sam/build/template.yaml \
--stack-name ${{ steps.vars.outputs.environment }} \
--s3-bucket ${{ steps.vars.outputs.environment }} \
--parameter-overrides \
'ParameterKey=Name,ParameterValue=example \
ParameterKey=Version,ParameterValue=${{ steps.vars.outputs.version }}' \
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \
|| echo "Nothing to deploy, no changes"
Hi @emlynoregan - thank you for reporting this and recommending a solution. Unfortunately the SAM deploy command (ultimately CloudFormation) does return an exit code when there are no changes to deploy, resulting in an error - so this is expected behavior, although I can understand your frustration here. This repo was built for example purposes, rather than a full fledged CI/CD solution.
My only concern about the proposed fix was that this may cause future failures that return exit codes to also display the "Nothing to deploy, no changes" error message. As an alternative solution to this problem, you could implement a basic git diff check within the deploy job, to check if there were any changes in template.yaml to determine whether to skip the sam deploy command using an if statement.
We will definitely consider adding something of the sort in the future to help resolve this use-case. PRs are certainly welcome if you would like to propose a change to the repo :)
I think this could be addressed by using the sam deploy --no-fail-on-empty-changeset argument.