Created
November 7, 2022 08:58
-
-
Save MrCordeiro/13330887bf5ac12f32ed29e1fe1256ae to your computer and use it in GitHub Desktop.
Deploy container to Elastic Beanstalk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Release | |
on: | |
push: | |
tags: | |
- 'v*' | |
jobs: | |
deploy-to-aws-ebs: | |
runs-on: ubuntu-latest | |
environment: staging | |
permissions: | |
id-token: write | |
contents: read | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
steps: | |
- name: Check out repository | |
uses: actions/checkout@v3 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/ServiceRoleForEBSDeploy | |
aws-region: ${{ secrets.AWS_DEFAULT_REGION }} | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
- name: Get tag name | |
# Concatenate the tag name with the short SHA while replacing the '.' with '-' | |
run: echo "tag=`echo ${{ github.ref }} | sed -e 's/\./-/g' | cut -c11-`-`echo ${{ github.sha }} | cut -c1-8`" >> $GITHUB_ENV | |
- name: Build, tag, and push docker image to Amazon ECR | |
env: | |
REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
REPOSITORY: ${{ secrets.REPO_NAME }} | |
IMAGE_TAG: ${{ env.tag }} | |
run: | | |
docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG . | |
docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG | |
echo "IMAGE_NAME=$REGISTRY/$REPOSITORY:$IMAGE_TAG" >> $GITHUB_ENV | |
- name: Create deployment package | |
run: | | |
sed -e "s|<IMAGE_NAME>|${{ env.IMAGE_NAME }}|g" \ | |
docker/Dockerrun.aws.template.json > Dockerrun.aws.json | |
cat Dockerrun.aws.json | |
- name: Deploy to AWS Elastic Beanstalk | |
env: | |
AWS_EBS_APP_NAME: MY_APP | |
AWS_EBS_ENV_NAME: MY_APP_STAGING_ENV | |
run: | | |
aws s3 cp Dockerrun.aws.json s3://${{ secrets.AWS_S3_BUCKET_NAME }}/versions/${{ env.tag }}-Dockerrun.aws.json | |
aws elasticbeanstalk create-application-version \ | |
--application-name $AWS_EBS_APP_NAME \ | |
--source-bundle S3Bucket=${{ secrets.AWS_S3_BUCKET_NAME }},S3Key=versions/${{ env.tag }}-Dockerrun.aws.json \ | |
--version-label ${{ env.tag }} | |
aws elasticbeanstalk update-environment \ | |
--application-name $AWS_EBS_APP_NAME \ | |
--environment-name $AWS_EBS_ENV_NAME \ | |
--version-label ${{ env.tag }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment