使用CircleCI部署LAMP应用至AWS的命令及配置疑问
Understanding the AWS S3 Deploy Command & Fixing Your CircleCI Config
Let's break this down step by step, starting with the command you're confused about, then helping you flesh out your CircleCI configuration.
What That aws s3 sync Command Does
That run step is conditional deployment logic—it only runs if your tests pass and you're on the Master branch (the branch check is typically handled by CircleCI workflow rules or inline conditional logic). Here's a detailed breakdown of the command:
aws s3 sync: This is an AWS CLI command built to synchronize files between a local directory and an S3 bucket. Unlike a full bulk upload, it only transfers files that have changed (based on file size or last modified timestamp), making deployments faster and more efficient.jekyll/_site/docs: This is your local source directory. Jekyll compiles your static site into the_sitefolder by default, so this path points to the built documentation files you want to deploy.s3://circle-production-static-site/docs/: This is the target location in your S3 bucket. It tells AWS to sync the local docs folder into thedocs/prefix of thecircle-production-static-sitebucket.--delete: This critical flag ensures your S3 bucket matches your local directory exactly. It will delete any files in the S3docs/path that no longer exist in your localjekyll/_site/docsfolder—preventing old, outdated content from lingering in your deployed site.
Fixing & Completing Your CircleCI Config
Your current config is a good starting point, but it's missing key components to make the deployment work. Here's a complete, annotated example you can adapt:
version: 2 jobs: build-test: docker: # Use an image with Ruby (for Jekyll) and AWS CLI pre-installed - image: circleci/ruby:3.2-node-browsers steps: - checkout # Pull your code from Git into the container - run: name: Install dependencies command: bundle install # Install Jekyll and its dependencies - run: name: Build Jekyll site command: jekyll build # Generates the static site into _site/ - run: name: Run tests (if you have any) command: | # Add your test commands here (e.g., link checkers, validation tools) echo "Running tests..." deploy-job: docker: # Use an image with AWS CLI installed—you could also reuse the Ruby image above - image: circleci/aws-cli:latest steps: - checkout # Pull code to access the built _site folder (or use workspaces to skip rebuilding) - run: name: Deploy to S3 (Master branch only) command: | # Only run deployment if we're on the Master branch if [ "${CIRCLE_BRANCH}" == "master" ]; then aws s3 sync jekyll/_site/docs s3://circle-production-static-site/docs/ --delete echo "Deployment to S3 completed successfully!" else echo "Skipping deployment: not on Master branch" fi workflows: build-deploy: jobs: - build-test # Only run deploy-job if build-test passes AND we're on Master - deploy-job: requires: - build-test filters: branches: only: master
Key Notes for Your Config:
- Docker Image Selection: Ensure your image has all required tools—Ruby for Jekyll, AWS CLI for deployment. The examples use official CircleCI images pre-configured with these tools.
- AWS Credentials: You need to configure AWS access in CircleCI. The safest method is using AWS OIDC Identity Provider (linking your AWS account to CircleCI instead of hardcoding keys). Alternatively, add
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEYas environment variables in your CircleCI project settings. - Workflow Logic: The workflow ensures deployment only triggers after tests pass and you're on the Master branch—preventing broken code from being deployed.
- Workspaces (Optional): Instead of checking out and rebuilding the site in
deploy-job, use CircleCI workspaces to share the built_sitefolder frombuild-testtodeploy-job—saving time by avoiding duplicate builds.
内容的提问来源于stack exchange,提问作者newbie
相关产品推荐
相关产品推荐

