Deploy to AWS #61
This file contains hidden or 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: Deploy to AWS | |
| on: | |
| workflow_run: | |
| workflows: | |
| - CI Build | |
| types: | |
| - completed | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| concurrency: | |
| group: production-deployment | |
| cancel-in-progress: true | |
| permissions: | |
| contents: read | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 | |
| with: | |
| fetch-depth: 1 | |
| - name: Setup Node | |
| uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0 | |
| with: | |
| node-version: 20.x | |
| cache: npm | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Build application | |
| run: npm run build:prod | |
| - name: Upload build artifacts | |
| uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 | |
| with: | |
| name: devtoolbox-artifacts | |
| path: dist/dev-tool-box/browser/ | |
| retention-days: 1 | |
| deploy: | |
| needs: build | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| id-token: write | |
| environment: | |
| name: production | |
| url: https://devtoolbox.raymondsplinter.com | |
| env: | |
| DEPLOY_URL: https://devtoolbox.raymondsplinter.com | |
| AWS_REGION: eu-central-1 | |
| AWS_ROLE_ARN: ${{ secrets.AWS_ROLE_ARN }} | |
| AWS_S3_BUCKET: ${{ secrets.S3_BUCKET }} | |
| AWS_CLOUDFRONT_DISTRIBUTION_ID: ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} | |
| steps: | |
| - name: Download build artifacts | |
| uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 | |
| with: | |
| name: devtoolbox-artifacts | |
| path: dist/dev-tool-box/browser/ | |
| - name: Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@ec61189d14ec14c8efccab744f656cffd0e33f37 # v6.1.0 | |
| with: | |
| aws-region: ${{ env.AWS_REGION }} | |
| role-to-assume: ${{ env.AWS_ROLE_ARN }} | |
| - name: Deploy application to S3 | |
| run: aws s3 sync dist/dev-tool-box/browser/ s3://${{ env.AWS_S3_BUCKET }} | |
| - name: Invalidate CloudFront cache | |
| id: invalidate | |
| run: | | |
| INVALIDATION_ID=$(aws cloudfront create-invalidation --distribution-id ${{ env.AWS_CLOUDFRONT_DISTRIBUTION_ID }} --paths "/*" --query 'Invalidation.Id' --output text) | |
| echo "invalidation-id=$INVALIDATION_ID" >> $GITHUB_OUTPUT | |
| - name: Wait for CloudFront invalidation | |
| run: | | |
| aws cloudfront wait invalidation-completed --distribution-id ${{ env.AWS_CLOUDFRONT_DISTRIBUTION_ID }} --id ${{ steps.invalidate.outputs.invalidation-id }} | |
| echo "✅ CloudFront cache invalidation completed" | |
| - name: Smoke test - Validate deployment | |
| run: | | |
| RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$DEPLOY_URL") | |
| echo "HTTP Response Code: $RESPONSE" | |
| if [ "$RESPONSE" = "200" ]; then | |
| echo "✅ Smoke test passed: Website is accessible at $DEPLOY_URL" >> $GITHUB_STEP_SUMMARY | |
| else | |
| echo "❌ Smoke test failed: HTTP $RESPONSE" >> $GITHUB_STEP_SUMMARY | |
| exit 1 | |
| fi | |
| - name: Deployment notification | |
| if: success() | |
| run: echo "✅ Deployment to production completed successfully" >> $GITHUB_STEP_SUMMARY | |
| - name: Deployment failure notification | |
| if: failure() | |
| run: | | |
| echo "❌ Deployment failed" >> $GITHUB_STEP_SUMMARY | |
| exit 1 |