name: Deploy to Elastic Beanstalk
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.<ENV>_AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.<ENV>_AWS_SECRET_KEY }}
aws-region: <REGION>
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build, tag, and push image to Amazon ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: <REPOSITORY>
IMAGE_TAG: ${{ github.sha }}
run: |
docker build \
-t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
- name: Prepare Dockerrun file
run: |
export IMAGE_TAG=${{ github.sha }}
export APP_NAME=<App-Name>
sed -i 's/<app-name>/'"$APP_NAME"'/g' Dockerrun.aws.json
sed -i 's/<version-tag>/'"$IMAGE_TAG"'/g' Dockerrun.aws.json
zip -r deploy.zip Dockerrun.aws.json .platform/
- name: Deploy to Elastic Beanstalk
uses: einaregilsson/beanstalk-deploy@v18
with:
aws_access_key: ${{ secrets.<ENV>_AWS_ACCESS_KEY }}
aws_secret_key: ${{ secrets.<ENV>_AWS_SECRET_KEY }}
application_name: <Application-Name>
environment_name: <Environment-Name>
existing_bucket_name: <S3-Bucket-Name>
region: <REGION>
version_label: ${{ github.sha }}
deployment_package: deploy.zip
{
"AWSEBDockerrunVersion": "1",
"Image": {
"Name": "<STRING>.dkr.ecr.<REGION>.amazonaws.com/<app-name>:<version-tag>",
"Update": "true"
},
"Ports": [{
"ContainerPort": "80"
}]
}
- In AWS IAM servıce create a role with
AWS Service
in Trusted entities andEC2
in Use case - Attach these policies to the role
AWSElasticBeanstalkWorkerTier
AWSElasticBeanstalkWebTier
AWSElasticBeanstalkMulticontainerDocker
- If you use ECR for deployment, attach
AmazonEC2ContainerRegistryReadOnly
policy - Set the role name
as
aws-elasticbeanstalk-ec2-role-<applicationName>-react-<environmentName>
- In AWS IAM service create a user and attach
AdministratorAccess-AWSElasticBeanstalk
policy - If you user ECR attach these policies as well
AmazonEC2ContainerRegistryPowerUser
,AmazonEC2ContainerRegistryReadOnly
andAmazonEC2ContainerServiceEventsRole
- In Security Credentials of created user create an access key and secret key using CLI
- Add these keys to GitHub repository secrets as
<ENV>_AWS_ACCESS_KEY
and<ENV>_AWS_SECRET_KEY
- Select Web server environment
- Enter application name:
<application-name>-React
- Enter environment name:
<environment-name>-React
- You can use the default domain name or create a new one
- Select
Docker
- Select
Docker running on 64bit Amazon Linux 2
- Platform version: select the latest version
-
Select
Single instance (free tier)
- Select
Create and use new service role
in Service role - Select the role created in the previous step
- Review the configuration and click
Create environment
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
COPY nginx.conf /etc/nginx/conf.d/default.conf