Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- name: Push to AWS ECR and deploy
- on:
- workflow_dispatch:
- jobs:
- Build:
- name: Build
- runs-on: ubuntu-latest
- defaults:
- run:
- working-directory: ./Backend
- steps:
- - name: Checkout
- uses: actions/checkout@v2
- - name: Configure AWS credentials
- uses: aws-actions/configure-aws-credentials@v1
- with:
- aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
- aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- aws-region: ap-south-1
- - name: Login to Amazon ECR
- id: login-ecr
- uses: aws-actions/amazon-ecr-login@v1
- - name: Build, tag, and push the image to Amazon ECR
- id: build-image
- env:
- ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
- ECR_REPOSITORY: supercharge
- IMAGE_TAG: latest
- run: |
- docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
- docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
- # https://blog.benoitblanchon.fr/github-action-run-ssh-commands/
- - name: Configure SSH
- env:
- SSH_USER: ubuntu
- SSH_KEY: ${{ secrets.EC_PRIVATE_KEY }}
- SSH_HOST: ${{ vars.HOST_IP }}
- run: |
- mkdir -p ~/.ssh/
- echo "$SSH_KEY" > ~/.ssh/deploy.key
- chmod 600 ~/.ssh/deploy.key
- cat >>~/.ssh/config <<END
- Host deploy
- HostName $SSH_HOST
- User $SSH_USER
- IdentityFile ~/.ssh/deploy.key
- StrictHostKeyChecking no
- END
- - name: copying the dockercompose file
- run: cat dockercompose.yml | ssh deploy 'cat > ~/supercharge/dockercompose.yml'
- # This is important because the authorization token expires after 12 hours
- - name: Authorizing ECR
- env:
- ECR_URI: ${{ vars.ECR_URI }}
- run: ssh deploy 'aws ecr get-login-password --region ap-south-1 | docker login --username AWS --password-stdin <aws-id>.dkr.ecr.ap-south-1.amazonaws.com'
- - name: Fetching changes
- run: ssh deploy 'cd ~/supercharge/ && docker-compose -f dockercompose.yml pull && docker-compose -f dockercompose.yml up -d'
- - name: Deleting Obsolete Images
- run: ssh deploy 'docker image prune -f'
Advertisement
Add Comment
Please, Sign In to add comment