AWS EC2上Bitbucket Pipeline未部署最新代码问题咨询
Let's walk through the most likely issues and fixes here—since your Bitbucket pipeline reports success and S3 has the latest bundle, the problem is almost certainly happening on the CodeDeploy or EC2 side.
1. Confirm CodeDeploy Actually Triggered a Deployment
First, rule out if the deployment even reached AWS CodeDeploy:
- Head to the AWS CodeDeploy Console > Deployments and look for the latest deployment tied to your pipeline run.
- If it’s marked Failed or In Progress, click into it to view detailed error messages.
- If there’s no matching deployment entry, double-check the
aws deploy create-deploymentcommand in yourbitbucket-pipelines.yml:- Ensure the
$APPLICATION_NAMEand$DEPLOYMENT_GROUP_NAMEvariables in Bitbucket exactly match the names in AWS CodeDeploy. Typos here can cause the command to exit successfully without creating a deployment.
- Ensure the
2. Check the CodeDeploy Agent Health on EC2
The CodeDeploy agent on your Ubuntu instance needs to be running and authorized to pull the bundle from S3:
- SSH into your EC2 instance and run:
sudo systemctl status codedeploy-agent - If it’s stopped, start it with:
sudo systemctl start codedeploy-agent - Enable it to auto-start on instance reboot:
sudo systemctl enable codedeploy-agent - Also, verify the EC2 instance’s IAM role has the
s3:GetObjectpermission for your deployment bucket—without this, the agent can’t download the bundle.
3. Validate Your AppSpec.yml Configuration
Your appspec.yml has a few critical points to check:
- Files Section: The
source: /tells CodeDeploy to copy all bundle files to/home/ubuntu/aok. Ensure:- The
/home/ubuntu/aokdirectory exists on EC2. If not, CodeDeploy might fail silently (check logs for this). You can add a pre-hook to create the directory if needed. - The CodeDeploy agent (running as the
codedeployuser) has write access to this directory. Your permissions section sets owner/group toubuntu, but confirm the agent can write here.
- The
- Hook Scripts: The
AfterInstallhooks runinstall_dependenciesandstart_server. Verify:- These scripts exist in your deployment bundle under the
scripts/folder (download the S3 zip to confirm). - They’re executable. Test them manually on EC2:
sudo /home/ubuntu/aok/scripts/install_dependencies sudo /home/ubuntu/aok/scripts/start_server - If
start_serverdoesn’t properly restart your MEAN app (e.g., it starts a new process without killing the old one), the original app instance may still be serving outdated content.
- These scripts exist in your deployment bundle under the
4. Dig Into CodeDeploy Logs on EC2
The most detailed error info lives in EC2’s CodeDeploy logs:
- Check the agent log at:
cat /var/log/aws/codedeploy-agent/codedeploy-agent.log - Check deployment-specific script logs at:
cat /opt/codedeploy-agent/deployment-root/<deployment-group-id>/<deployment-id>/logs/scripts.log - Look for red flags like:
- Permission denied errors when writing to the destination directory
- Scripts exiting with non-zero codes
- Missing files or incorrect source paths
5. Verify the Deployment Bundle Content
Even if S3 has the latest code, double-check the bundle itself:
- Download
aok.zipfrom S3, unzip it locally, and confirm your latest changes are present. - Your
aws deploy pushcommand uses--ignore-hidden-files—if your MEAN app relies on hidden files (like.env), they won’t be included. Remove this flag if needed.
6. Ensure the MEAN App Serves the Correct Directory
Finally, confirm your MEAN app’s server is configured to serve files from /home/ubuntu/aok. If it’s using a symlink that wasn’t updated, or pointing to an old directory, it will continue to show outdated content.
内容的提问来源于stack exchange,提问作者ah92

