Search code examples
aws-code-deploybitbucket-pipelines

CodeDeploy Bitbucket - How to Fail Bitbucket on CodeDeploy Failure


I have a successful bitbucket pipeline calling out to aws CodeDeploy, but I'm wondering if I can add a step that will check and wait for CodeDeploy success, otherwise fail the pipeline. Would this just be possible with a script that loops through a CodeDeploy call that continues to monitor the status of the CodeDeploy push? Any idea what CodeDeploy call that would be?

bitbucket-pipline.yml

image: pitech/gradle-awscli

pipelines:
  branches:
develop:
  - step:
      caches:
        - gradle
      script:
        - gradle build bootRepackage
        - mkdir tmp; cp appspec.yml tmp; cp build/libs/thejar*.jar tmp/the.jar; cp -r scripts/ ./tmp/
        - pip install awscli --upgrade --user
        - aws deploy push --s3-location s3://thebucket/the-deploy.zip --application-name my-staging-app --ignore-hidden-files --source tmp
        - aws deploy create-deployment --application-name server-staging --s3-location bucket=staging-codedeploy,key=the-deploy.zip,bundleType=zip --deployment-group-name the-staging --deployment-config-name CodeDeployDefault.AllAtOnce --file-exists-behavior=OVERWRITE

appspec.yml

version: 0.0
os: linux
files:
  - source: thejar.jar
    destination: /home/ec2-user/the-server/

permissions:
  - object: /
    pattern: "**"
    owner: ec2-user
    group: ec2-user

hooks:
  ApplicationStop:
    - location: scripts/server_stop.sh
      timeout: 60
      runas: ec2-user
  ApplicationStart:
    - location: scripts/server_start.sh
      timeout: 60
      runas: ec2-user
  ValidateService:
    - location: scripts/server_validate.sh
      timeout: 120
      runas: ec2-user

Unfortunately it doesn't seem like Bitbucket is waiting for the ValidateService to complete, so I'd need a way in Bitbucket to confirm before marking the build a success.


Solution

  • aws deploy create-deployment is an asynchronous call, and BitBucket has no idea that it needs to know about the success of your deployment. Adding a script to your CodeDeploy application will have no effect on BitBucket knowing about your deployment.

    You have one (maybe two) options to fix this issue.

    #1 Include a script that waits for your deployment to finish

    You need to add a script to your BitBucket pipeline to check the status of your deployment to finish. You can either use SNS notifications, or poll the CodeDeploy service directly.

    The pseudocode would look something like this:

    loop
        check_if_deployment_complete
        if false, wait and retry
        if true && deployment successful, return 0 (success)
        if true && deployment failed, return non-zero (failure)
    

    You can use the AWS CLI or your favorite scripting language. Add it at the end of your bitbucket-pipline.yml script. Make sure you use a wait between calls to CodeDeploy to check the status.

    #2 (the maybe) Use BitBucket AWS CodeDeploy integration directly

    BitBucket integrates with AWS CodeDeploy directly, so you might be able to use their integration rather than your script to integration properly. I don't know if this is supported or not.