Skip to content

code deploy deployment file

0

need to deploy code using code deploy via deployment.sh file give .sh file which will be able to zip code with code deploy files created in repo like appsec.yml,stop-server.sh,etc and push to s3 which will be used by code deploy and trigger its deployment already application,deploymnet group created in code deploy. ec2 has running code depoy agent need to automate deployment using sh script so later can be used in bitbucket please provide script which can be used to run to local system to deploy initially

asked 4 months ago66 views
1 Answer
1

Here's a shell script that will automate your CodeDeploy deployment process:

#!/bin/bash

# Configuration variables
APPLICATION_NAME="your-application-name"
DEPLOYMENT_GROUP="your-deployment-group-name"
DEPLOYMENT_CONFIG="CodeDeployDefault.OneAtATime"
S3_BUCKET="your-s3-bucket-name"
REGION="us-east-1"

# Generate timestamp for unique deployment file
TIMESTAMP=$(date +%Y%m%d%H%M%S)
ZIP_FILE="deployment-${TIMESTAMP}.zip"
S3_KEY="deployments/${ZIP_FILE}"

echo "Starting deployment process..."

# Create zip file with all necessary files
echo "Creating deployment package..."
zip -r ${ZIP_FILE} . -x "*.git*" "*.sh" "deployment-*.zip"

# Upload to S3
echo "Uploading to S3..."
aws s3 cp ${ZIP_FILE} s3://${S3_BUCKET}/${S3_KEY} --region ${REGION}

if [ $? -ne 0 ]; then
echo "Failed to upload to S3"
rm ${ZIP_FILE}
exit 1
fi

echo "Package uploaded successfully to s3://${S3_BUCKET}/${S3_KEY}"

# Create deployment
echo "Creating CodeDeploy deployment..."
DEPLOYMENT_ID=$(aws deploy create-deployment \
--application-name ${APPLICATION_NAME} \
--deployment-group-name ${DEPLOYMENT_GROUP} \
--deployment-config-name ${DEPLOYMENT_CONFIG} \
--s3-location bucket=${S3_BUCKET},bundleType=zip,key=${S3_KEY} \
--region ${REGION} \
--query 'deploymentId' \
--output text)

if [ $? -ne 0 ]; then
echo "Failed to create deployment"
rm ${ZIP_FILE}
exit 1
fi

echo "Deployment created with ID: ${DEPLOYMENT_ID}"

# Clean up local zip file
rm ${ZIP_FILE}

# Monitor deployment status
echo "Monitoring deployment status..."
aws deploy wait deployment-successful \
--deployment-id ${DEPLOYMENT_ID} \
--region ${REGION}

if [ $? -eq 0 ]; then
echo "Deployment completed successfully!"
else
echo "Deployment failed or timed out. Check AWS Console for details."
exit 1
fi

Important Notes:

  1. Update the configuration variables at the top of the script with your actual values (application name, deployment group, S3 bucket, and region)

  2. Ensure your appspec.yml file is in the root of your repository with the correct structure, including file mappings and lifecycle hooks for your stop-server.sh and other scripts

  3. AWS CLI must be configured on your local system with appropriate credentials that have permissions to upload to S3 and create CodeDeploy deployments

  4. Make the script executable: chmod +x deployment.sh

  5. Run from your repository root: ./deployment.sh

The script will:

  • Package your code and CodeDeploy files (appspec.yml, scripts, etc.) into a zip file
  • Upload the package to your specified S3 bucket
  • Trigger a CodeDeploy deployment using the uploaded revision
  • Monitor the deployment status and report success or failure

For Bitbucket integration, you can adapt this script to run in your Bitbucket Pipeline by ensuring the AWS CLI is available in your pipeline environment and credentials are configured via environment variables.
Sources
Deployments on an EC2/On-Premises Compute Platform - AWS CodeDeploy
Step 4: Deploy your WordPress application - AWS CodeDeploy

answered 4 months ago
EXPERT
reviewed 4 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.