|
| 1 | +name: Frontend Deployment |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - dev |
| 7 | + - staging |
| 8 | + - demo |
| 9 | + - prod |
| 10 | + paths: |
| 11 | + - 'frontend/**' |
| 12 | + - '.github/workflows/deploy-frontend.yaml' |
| 13 | + |
| 14 | +permissions: |
| 15 | + id-token: write |
| 16 | + contents: read |
| 17 | + |
| 18 | +jobs: |
| 19 | + build_and_deploy: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + environment: ${{ github.ref == 'refs/heads/prod' && 'prod' || github.ref == 'refs/heads/staging' && 'staging' || github.ref == 'refs/heads/dev' && 'dev' }} |
| 22 | + env: |
| 23 | + PRODUCT: wallet |
| 24 | + APPLICATION: frontend |
| 25 | + AWS_DEFAULT_REGION: eu-west-1 |
| 26 | + BUILD_DIR: ./frontend/build |
| 27 | + |
| 28 | + steps: |
| 29 | + - uses: actions/checkout@v4 |
| 30 | + |
| 31 | + - uses: actions/setup-node@v4 |
| 32 | + with: |
| 33 | + node-version: '20' |
| 34 | + |
| 35 | + - name: Set dev ENV |
| 36 | + if: github.ref == 'refs/heads/dev' |
| 37 | + run: | |
| 38 | + echo "ENV=dev" >> $GITHUB_ENV |
| 39 | + echo "AWS_ACCOUNT_ID=759055860286" >> $GITHUB_ENV |
| 40 | + echo "AWS_S3_BUCKET=wallet-frontend-dev-759055860286-eu-west-1" >> $GITHUB_ENV |
| 41 | +
|
| 42 | + - name: Set staging ENV |
| 43 | + if: github.ref == 'refs/heads/staging' |
| 44 | + run: | |
| 45 | + echo "ENV=staging" >> $GITHUB_ENV |
| 46 | + echo "AWS_ACCOUNT_ID=924226199168" >> $GITHUB_ENV |
| 47 | + echo "AWS_S3_BUCKET=wallet-frontend-staging-924226199168-eu-west-1" >> $GITHUB_ENV |
| 48 | +
|
| 49 | + - name: Set demo ENV |
| 50 | + if: github.ref == 'refs/heads/demo' |
| 51 | + run: | |
| 52 | + echo "ENV=staging" >> $GITHUB_ENV |
| 53 | + echo "AWS_ACCOUNT_ID=924226199168" >> $GITHUB_ENV |
| 54 | + echo "AWS_S3_BUCKET=wallet-frontend-demo-924226199168-eu-west-1" >> $GITHUB_ENV |
| 55 | + |
| 56 | + - name: Set prod ENV |
| 57 | + if: github.ref == 'refs/heads/prod' |
| 58 | + run: | |
| 59 | + echo "ENV=prod" >> $GITHUB_ENV |
| 60 | + echo "AWS_ACCOUNT_ID=255525589248" >> $GITHUB_ENV |
| 61 | + echo "AWS_S3_BUCKET=wallet-frontend-prod-255525589248-eu-west-1" >> $GITHUB_ENV |
| 62 | +
|
| 63 | + - name: Prepare .npmrc |
| 64 | + run: | |
| 65 | + echo "//npm.pkg.github.com/:_authToken=${{ secrets.NPM_AUTH_TOKEN }}" >> .npmrc |
| 66 | + cat .npmrc |
| 67 | +
|
| 68 | + - name: Enable Corepack |
| 69 | + run: corepack enable |
| 70 | + |
| 71 | + - name: Install dependencies |
| 72 | + run: yarn install --frozen-lockfile |
| 73 | + |
| 74 | + - name: Building sources |
| 75 | + run: CI=false yarn build |
| 76 | + |
| 77 | + - name: Configure AWS credentials |
| 78 | + uses: aws-actions/configure-aws-credentials@v4 |
| 79 | + with: |
| 80 | + role-to-assume: arn:aws:iam::${{ env.AWS_ACCOUNT_ID }}:role/${{ env.PRODUCT }}-${{ env.APPLICATION }}-deployer-${{ env.AWS_DEFAULT_REGION }}-${{ env.ENV }} |
| 81 | + aws-region: ${{ env.AWS_DEFAULT_REGION }} |
| 82 | + |
| 83 | + - name: Deploy to S3 |
| 84 | + run: | |
| 85 | + aws s3 sync ${{ env.BUILD_DIR }} s3://${{ env.AWS_S3_BUCKET }} --follow-symlinks --delete --no-progress |
| 86 | +
|
| 87 | + - name: Dynamically retreive the CloudFront Distribution Ids |
| 88 | + run: | |
| 89 | + cloudfrontdistids=$(aws cloudfront list-distributions | jq -r ".DistributionList.Items[].ARN") |
| 90 | + for dist in $cloudfrontdistids; do |
| 91 | + if [ $(aws cloudfront list-tags-for-resource --resource $dist --query "Tags.Items[?Key=='Application'].Value[]" --output text) == ${{ env.APPLICATION }} ] && |
| 92 | + [ $(aws cloudfront list-tags-for-resource --resource $dist --query "Tags.Items[?Key=='Environment'].Value[]" --output text) == ${{ env.ENV }} ]; then |
| 93 | + CLOUDFRONT_DISTRIBUTION_IDS="$CLOUDFRONT_DISTRIBUTION_IDS ${dist##*/}" |
| 94 | + fi |
| 95 | + done |
| 96 | + echo "Cloudfront distributionids : $CLOUDFRONT_DISTRIBUTION_IDS" |
| 97 | + echo "CLOUDFRONT_DISTRIBUTION_IDS=$CLOUDFRONT_DISTRIBUTION_IDS" >> $GITHUB_ENV |
| 98 | +
|
| 99 | + - name: Invalidate CloudFront cache of distributions ${{ env.CLOUDFRONT_DISTRIBUTION_IDS }} |
| 100 | + run: | |
| 101 | + for dist in $CLOUDFRONT_DISTRIBUTION_IDS; do |
| 102 | + echo "Create invalidation for distribution-id $dist" |
| 103 | + aws cloudfront create-invalidation --distribution-id $dist --paths "/*" |
| 104 | + done |
0 commit comments