Guest User

Untitled

a guest
Sep 24th, 2024
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | Source Code | 0 0
  1. name: Push to AWS ECR and deploy
  2. on:
  3. workflow_dispatch:
  4.  
  5. jobs:
  6. Build:
  7. name: Build
  8. runs-on: ubuntu-latest
  9. defaults:
  10. run:
  11. working-directory: ./Backend
  12. steps:
  13. - name: Checkout
  14. uses: actions/checkout@v2
  15.  
  16. - name: Configure AWS credentials
  17. uses: aws-actions/configure-aws-credentials@v1
  18. with:
  19. aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
  20. aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  21. aws-region: ap-south-1
  22.  
  23. - name: Login to Amazon ECR
  24. id: login-ecr
  25. uses: aws-actions/amazon-ecr-login@v1
  26. - name: Build, tag, and push the image to Amazon ECR
  27. id: build-image
  28. env:
  29. ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
  30. ECR_REPOSITORY: supercharge
  31. IMAGE_TAG: latest
  32. run: |
  33. docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
  34. docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
  35.  
  36. # https://blog.benoitblanchon.fr/github-action-run-ssh-commands/
  37. - name: Configure SSH
  38. env:
  39. SSH_USER: ubuntu
  40. SSH_KEY: ${{ secrets.EC_PRIVATE_KEY }}
  41. SSH_HOST: ${{ vars.HOST_IP }}
  42. run: |
  43. mkdir -p ~/.ssh/
  44. echo "$SSH_KEY" > ~/.ssh/deploy.key
  45. chmod 600 ~/.ssh/deploy.key
  46. cat >>~/.ssh/config <<END
  47. Host deploy
  48. HostName $SSH_HOST
  49. User $SSH_USER
  50. IdentityFile ~/.ssh/deploy.key
  51. StrictHostKeyChecking no
  52. END
  53.  
  54. - name: copying the dockercompose file
  55. run: cat dockercompose.yml | ssh deploy 'cat > ~/supercharge/dockercompose.yml'
  56.  
  57. # This is important because the authorization token expires after 12 hours
  58. - name: Authorizing ECR
  59. env:
  60. ECR_URI: ${{ vars.ECR_URI }}
  61. 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'
  62.  
  63.  
  64. - name: Fetching changes
  65. run: ssh deploy 'cd ~/supercharge/ && docker-compose -f dockercompose.yml pull && docker-compose -f dockercompose.yml up -d'
  66.  
  67. - name: Deleting Obsolete Images
  68. run: ssh deploy 'docker image prune -f'
Advertisement
Add Comment
Please, Sign In to add comment