DEV Community

Discussion on: Continuous AWS Amplify deployment

Collapse
 
tarhis profile image
TARHiS

You can reduce the script lines by the fact that you can also deploy straight from S3 or public URL without needing to call aws amplify create-deployment as follows:

aws amplify start-deployment \
        --app-id $amplify_id \
        --branch-name $git_branch \
        --source-url s3://$s3_bucket/$archive
Enter fullscreen mode Exit fullscreen mode

It should also be a little less error sensitive when you can use zip files already uploaded to S3 and don't have to upload them again with curl.

Here is full modified example:

#!/bin/bash

archive="archive.zip"
s3_bucket="elasticbeanstalk-eu-west-3-something"
amplify_id="something"
git_branch=$(git rev-parse --abbrev-ref HEAD)

if [ -f "$archive" ] ; then
    printf "Delete previously created archive\n"
    rm "$archive"
fi

# We create the Nuxtjs dist/ build directory  
printf "Make bundle build\n"
yarn run build

# Create the archive of the dist/ directory
printf "Create the code archive\n"  
zip -q -r $archive dist/*

# Check that the latest archive on s3 is not the same as the one we just created
# to avoid useless deployments
printf "Generate current deploy checksum\n"
CURRENT_MD5=$(find dist/ -type f -exec md5sum {} \; | sort -k 2 | md5sum)

printf "Verify latest deploy checksum\n"
LATEST_MD5=$(aws s3api head-object --bucket $s3_bucket --key $archive \
  | jq -r '.Metadata.md5')

if [ "$CURRENT_MD5" = "$LATEST_MD5" ]; then
    # No deploy required
    printf "The Latest project version is already deployed\n"
else
    # Save the archive to S3 with the MD5 checksum in metadata to simplify 
    # checks in the next deployment
    aws s3 cp $archive s3://$s3_bucket/ --metadata md5="$CURRENT_MD5"

    # Start the deployment
    printf "Start Amplify deployment\n"
    aws amplify start-deployment \
        --app-id $amplify_id \
        --branch-name $git_branch \
        --source-url s3://$s3_bucket/$archive > amplify-deploy-job.json

    AMPLIFY_JOB_ID=$(cat amplify-deploy-job.json \
        | jq -r '.jobSummary.jobId')

    while :
    do
        sleep 10

        # Poll the deployment job status every 10 seconds until it's not pending
        # anymore
        STATUS=$(aws amplify get-job \
            --app-id $amplify_id \
            --branch-name $git_branch \
            --job-id $AMPLIFY_JOB_ID \
            | jq -r '.job.summary.status')

        if [ $STATUS != 'PENDING' ]; then
          break
       fi
    done

    printf "Amplify deployment status $STATUS\n"
fi
Enter fullscreen mode Exit fullscreen mode