|
| 1 | +# Deployment Guide |
| 2 | + |
| 3 | +This document provides detailed instructions for setting up and configuring the deployment pipeline for the Toban Contribution Viewer project. |
| 4 | + |
| 5 | +## Infrastructure Setup |
| 6 | + |
| 7 | +### Backend: AWS Elastic Beanstalk |
| 8 | + |
| 9 | +1. **Create an Elastic Beanstalk Application:** |
| 10 | + ```bash |
| 11 | + aws elasticbeanstalk create-application --application-name toban-contribution-viewer |
| 12 | + ``` |
| 13 | + |
| 14 | +2. **Create an Elastic Beanstalk Environment:** |
| 15 | + ```bash |
| 16 | + aws elasticbeanstalk create-environment \ |
| 17 | + --application-name toban-contribution-viewer \ |
| 18 | + --environment-name toban-contribution-viewer-prod \ |
| 19 | + --solution-stack-name "64bit Amazon Linux 2023 v4.0.6 running Python 3.12" \ |
| 20 | + --option-settings file://backend/eb-config.json |
| 21 | + ``` |
| 22 | + |
| 23 | +3. **Configure Environment Variables:** |
| 24 | + Set the following environment variables in the Elastic Beanstalk environment: |
| 25 | + - `DATABASE_URL`: Your production PostgreSQL connection string |
| 26 | + - `SECRET_KEY`: A secure random string for production |
| 27 | + - `AUTH0_DOMAIN`: Your Auth0 domain |
| 28 | + - `AUTH0_CLIENT_ID`: Your Auth0 client ID |
| 29 | + - `AUTH0_CLIENT_SECRET`: Your Auth0 client secret |
| 30 | + - `AUTH0_AUDIENCE`: Your Auth0 API audience |
| 31 | + - `OPENAI_API_KEY`: Your OpenAI API key |
| 32 | + |
| 33 | +4. **Set Up Database:** |
| 34 | + - Create an RDS PostgreSQL instance |
| 35 | + - Configure security groups to allow access from your Elastic Beanstalk environment |
| 36 | + - Create the initial database schema |
| 37 | + |
| 38 | +### Frontend: AWS S3 and CloudFront |
| 39 | + |
| 40 | +1. **Create an S3 Bucket:** |
| 41 | + ```bash |
| 42 | + aws s3 mb s3://toban-contribution-viewer-frontend --region us-east-1 |
| 43 | + ``` |
| 44 | + |
| 45 | +2. **Configure the S3 Bucket for Static Website Hosting:** |
| 46 | + ```bash |
| 47 | + aws s3 website s3://toban-contribution-viewer-frontend \ |
| 48 | + --index-document index.html \ |
| 49 | + --error-document index.html |
| 50 | + ``` |
| 51 | + |
| 52 | +3. **Set Bucket Policy for Public Access:** |
| 53 | + Create a file named `bucket-policy.json` with the following content: |
| 54 | + ```json |
| 55 | + { |
| 56 | + "Version": "2012-10-17", |
| 57 | + "Statement": [ |
| 58 | + { |
| 59 | + "Sid": "PublicReadGetObject", |
| 60 | + "Effect": "Allow", |
| 61 | + "Principal": "*", |
| 62 | + "Action": "s3:GetObject", |
| 63 | + "Resource": "arn:aws:s3:::toban-contribution-viewer-frontend/*" |
| 64 | + } |
| 65 | + ] |
| 66 | + } |
| 67 | + ``` |
| 68 | + Apply the policy: |
| 69 | + ```bash |
| 70 | + aws s3api put-bucket-policy \ |
| 71 | + --bucket toban-contribution-viewer-frontend \ |
| 72 | + --policy file://bucket-policy.json |
| 73 | + ``` |
| 74 | + |
| 75 | +4. **Create a CloudFront Distribution:** |
| 76 | + ```bash |
| 77 | + aws cloudfront create-distribution \ |
| 78 | + --origin-domain-name toban-contribution-viewer-frontend.s3.amazonaws.com \ |
| 79 | + --default-root-object index.html |
| 80 | + ``` |
| 81 | + |
| 82 | +5. **Configure Custom Domain (Optional):** |
| 83 | + - Create a certificate using AWS Certificate Manager |
| 84 | + - Add the domain to your CloudFront distribution |
| 85 | + - Configure DNS settings to point to your CloudFront distribution |
| 86 | + |
| 87 | +## GitHub Actions Setup |
| 88 | + |
| 89 | +### Creating Required Secrets |
| 90 | + |
| 91 | +Add the following secrets to your GitHub repository: |
| 92 | + |
| 93 | +1. AWS credentials: |
| 94 | + - `AWS_ACCESS_KEY_ID`: Your AWS access key |
| 95 | + - `AWS_SECRET_ACCESS_KEY`: Your AWS secret key |
| 96 | + - `AWS_REGION`: The AWS region (e.g., us-east-1) |
| 97 | + |
| 98 | +2. Elastic Beanstalk configuration: |
| 99 | + - `EB_APPLICATION_NAME`: Your Elastic Beanstalk application name (e.g., toban-contribution-viewer) |
| 100 | + - `EB_ENVIRONMENT_NAME`: Your Elastic Beanstalk environment name (e.g., toban-contribution-viewer-prod) |
| 101 | + |
| 102 | +3. S3 and CloudFront configuration: |
| 103 | + - `S3_BUCKET_NAME`: Your S3 bucket name (e.g., toban-contribution-viewer-frontend) |
| 104 | + - `CLOUDFRONT_DISTRIBUTION_ID`: Your CloudFront distribution ID |
| 105 | + - `SITE_DOMAIN`: Your site domain (e.g., app.yoursite.com) |
| 106 | + |
| 107 | +4. Environment-specific variables: |
| 108 | + - All the required backend and frontend environment variables for production |
| 109 | + |
| 110 | +5. Slack notifications (optional): |
| 111 | + - `SLACK_WEBHOOK_URL`: Your Slack webhook URL for deployment notifications |
| 112 | + |
| 113 | +### Testing the Deployment |
| 114 | + |
| 115 | +1. Push a change to the main branch to trigger the deployment workflow |
| 116 | +2. Monitor the GitHub Actions workflow run |
| 117 | +3. Verify the deployment was successful by accessing your application |
| 118 | + |
| 119 | +## Deployment Environments |
| 120 | + |
| 121 | +### Production |
| 122 | + |
| 123 | +The production environment is automatically deployed when changes are pushed to the main branch. You can also manually trigger a deployment through the GitHub Actions interface. |
| 124 | + |
| 125 | +### Staging (Optional) |
| 126 | + |
| 127 | +To set up a staging environment: |
| 128 | + |
| 129 | +1. Create additional Elastic Beanstalk environment and S3 bucket for staging |
| 130 | +2. Create a new GitHub workflow file specifically for staging deployment |
| 131 | +3. Configure the workflow to deploy to the staging environment when changes are pushed to a staging branch |
| 132 | + |
| 133 | +## Rollback Procedures |
| 134 | + |
| 135 | +### Backend Rollback |
| 136 | + |
| 137 | +1. Open the Elastic Beanstalk console |
| 138 | +2. Navigate to your environment |
| 139 | +3. Select the "Application versions" tab |
| 140 | +4. Select the previous working version |
| 141 | +5. Click "Deploy" to roll back to that version |
| 142 | + |
| 143 | +### Frontend Rollback |
| 144 | + |
| 145 | +1. Navigate to your S3 bucket |
| 146 | +2. Restore a previous version using S3 versioning |
| 147 | +3. Invalidate the CloudFront cache to serve the restored version |
0 commit comments