ci: add github previews #2
Workflow file for this run
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: Preview | |
| on: | |
| pull_request: | |
| branches: | |
| - "*" | |
| permissions: | |
| id-token: write | |
| contents: read | |
| issues: write | |
| pull-requests: write | |
| env: | |
| BUCKET_NAME: ds-preview-stac-map-${{ github.event.number }} | |
| AWS_ROLE_ARN: arn:aws:iam::552819999234:role/stac-map-gh-preview | |
| AWS_REGION: us-west-2 | |
| DIST_DIRECTORY: dist | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: actions/setup-node@v4 | |
| with: | |
| node-version-file: ".node-version" | |
| cache: "yarn" | |
| - name: Post building comment | |
| uses: actions/github-script@v6 | |
| with: | |
| script: | | |
| const { createDeployingComment } = require('./.github/workflows/github-pr-update.js') | |
| await createDeployingComment({ github, context, core }) | |
| - name: Install | |
| run: yarn install --ignore-engines | |
| - name: Build | |
| run: yarn build | |
| - name: Post error comment | |
| uses: actions/github-script@v6 | |
| if: failure() | |
| with: | |
| script: | | |
| const { createFailedComment } = require('./.github/workflows/github-pr-update.js') | |
| await createFailedComment({ github, context, core }) | |
| - uses: actions/upload-artifact@v4 | |
| with: | |
| path: ${{ env.DIST_DIRECTORY }} | |
| deploy: | |
| runs-on: ubuntu-latest | |
| needs: build | |
| steps: | |
| - uses: aws-actions/configure-aws-credentials@v4 | |
| with: | |
| role-to-assume: ${{ env.AWS_ROLE_ARN }} | |
| aws-region: ${{ env.AWS_REGION }} | |
| - uses: actions/download-artifact@v5 | |
| with: | |
| path: ${{ env.DIST_DIRECTORY }} | |
| - name: Check if bucket exists | |
| id: check_bucket | |
| run: | | |
| if aws s3 ls "s3://${{ env.BUCKET_NAME }}" 2>&1 | grep -q 'NoSuchBucket'; then | |
| echo "Bucket does not exist." | |
| echo "exists=false" >> "$GITHUB_OUTPUT" | |
| else | |
| echo "Bucket exists." | |
| echo "exists=true" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Create S3 bucket | |
| if: steps.check_bucket.outputs.exists == 'false' | |
| run: | | |
| aws s3 mb s3://${{ env.BUCKET_NAME }} | |
| - name: Enable static website hosting | |
| if: steps.check_bucket.outputs.exists == 'false' | |
| run: | | |
| aws s3 website \ | |
| s3://${{ env.BUCKET_NAME }} \ | |
| --index-document index.html \ | |
| --error-document index.html | |
| - name: Sync files | |
| run: | | |
| aws s3 sync \ | |
| ./${{env.DIST_DIRECTORY}} s3://${{ env.BUCKET_NAME }} \ | |
| --delete \ | |
| --quiet | |
| - name: Make bucket public access | |
| if: steps.check_bucket.outputs.exists == 'false' | |
| run: | | |
| aws s3api delete-public-access-block --bucket ${{ env.BUCKET_NAME }} | |
| - name: Add bucket policy for public access | |
| if: steps.check_bucket.outputs.exists == 'false' | |
| run: | | |
| echo '{ | |
| "Version": "2012-10-17", | |
| "Statement": [{ | |
| "Sid": "PublicReadGetObject", | |
| "Effect": "Allow", | |
| "Principal": "*", | |
| "Action": "s3:GetObject", | |
| "Resource": "arn:aws:s3:::${{ env.BUCKET_NAME }}/*" | |
| }] | |
| }' > bucket-policy.json | |
| aws s3api put-bucket-policy --bucket ${{ env.BUCKET_NAME }} --policy file://bucket-policy.json | |
| - name: Post error comment | |
| uses: actions/github-script@v6 | |
| if: success() | |
| with: | |
| script: | | |
| const { createSuccessComment } = require('./.github/workflows/github-pr-update.js') | |
| await createSuccessComment({ github, context, core }) | |
| - name: Post comment with preview URL | |
| uses: actions/github-script@v6 | |
| if: failure() | |
| with: | |
| script: | | |
| const { createFailedComment } = require('./.github/workflows/github-pr-update.js') | |
| await createFailedComment({ github, context, core }) |