- Newest
- Most votes
- Most comments
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:
-
Update the configuration variables at the top of the script with your actual values (application name, deployment group, S3 bucket, and region)
-
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
-
AWS CLI must be configured on your local system with appropriate credentials that have permissions to upload to S3 and create CodeDeploy deployments
-
Make the script executable:
chmod +x deployment.sh -
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
Relevant content
- asked 4 months ago
- AWS OFFICIALUpdated 4 years ago
