From 5f281a1a171e68992778d919e8a5efae296a2d90 Mon Sep 17 00:00:00 2001 From: haimlevy2006 Date: Tue, 9 Jul 2024 15:52:44 +0300 Subject: [PATCH] Add Smart Previews PR mechanism --- .github/workflows/automated-publish-docs.yaml | 19 ++- .../workflows/create-preview-on-pr-v2.yaml | 110 ++++++++++++++++++ .../workflows/create-preview-on-pr-v3.yaml | 87 ++++++++++++++ .github/workflows/deploy-staging.yaml | 2 +- 4 files changed, 206 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/create-preview-on-pr-v2.yaml create mode 100644 .github/workflows/create-preview-on-pr-v3.yaml diff --git a/.github/workflows/automated-publish-docs.yaml b/.github/workflows/automated-publish-docs.yaml index f64da1ccdb..97c38ef47d 100644 --- a/.github/workflows/automated-publish-docs.yaml +++ b/.github/workflows/automated-publish-docs.yaml @@ -4,6 +4,8 @@ on: push: branches: - v*.* + paths-ignore: + - 'github/**' jobs: env: @@ -37,8 +39,8 @@ jobs: echo "Deploying $CURRENT_BRANCH which is not the latest version ( => $NEWEST_VERSION )..." fi - install-dependencies: - name: install dependencies + install-dependencies-and-deploy: + name: Install Dependencies and Deploy needs: [env] runs-on: ubuntu-latest steps: @@ -63,12 +65,7 @@ jobs: git config user.name "circleci-runai" git config user.email "circleci-runai@run.ai" - deploy: - name: deploy mkdocs - needs: [env, install-dependencies] - runs-on: ubuntu-latest - steps: - - name: deploy mkdocs latest + - name: deploy mkdocs run: | if [[ "${{ needs.env.outputs.CURRENT_BRANCH }}" == "${{ needs.env.outputs.NEWEST_VERSION }}" ]]; then echo "Deploying ${{ needs.env.outputs.NEWEST_VERSION }} as latest..." @@ -87,7 +84,7 @@ jobs: slack-notification: name: Slack Notification - needs: [env, install-dependencies, deploy] + needs: [env, install-dependencies-and-deploy] if: always() runs-on: ubuntu-latest steps: @@ -96,8 +93,8 @@ jobs: env: SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }} SLACK_COLOR: ${{ contains(needs.*.result, 'failure') && 'failure' || 'success' }} - SLACK_TITLE: "Airgapped package for ${{ inputs.env }} environment ${{ contains(needs.*.result, 'failure') && 'failed' || 'finished successfully' }}" + SLACK_TITLE: "RunAI-Docs: Version ${{ needs.env.outputs.CURRENT_BRANCH }} Deployment ${{ contains(needs.*.result, 'failure') && 'failed' || 'completed successfully' }}" SLACK_MESSAGE_ON_SUCCESS: "Docs were updated successfully for version ${{ needs.env.outputs.TITLE }}" SLACK_MESSAGE_ON_FAILURE: "Docs update FAILED for version ${{ needs.env.outputs.TITLE }}" MSG_MINIMAL: true - SLACK_FOOTER: "" + SLACK_FOOTER: "" \ No newline at end of file diff --git a/.github/workflows/create-preview-on-pr-v2.yaml b/.github/workflows/create-preview-on-pr-v2.yaml new file mode 100644 index 0000000000..7a9d7e5ead --- /dev/null +++ b/.github/workflows/create-preview-on-pr-v2.yaml @@ -0,0 +1,110 @@ +# name: Create Preview Env using S3 and CloudFront + +# on: +# pull_request: +# types: +# - opened +# - reopened +# - synchronize +# - closed +# branches: +# - master +# - v*.* +# env: +# PR_NUMBER: ${{ github.event.number }} + +# jobs: +# deploy: +# runs-on: ubuntu-latest +# steps: +# - name: Checkout code +# uses: actions/checkout@v4 + +# - name: Install AWS CLI +# run: | +# sudo apt-get update +# sudo apt-get install awscli -y + +# - name: Sync S3 bucket +# env: +# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} +# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} +# AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }} +# run: | +# aws s3 sync ./docs s3://your-bucket-name + +# - name: Invalidate CloudFront cache +# env: +# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} +# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} +# AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }} +# run: | +# aws cloudfront create-invalidation --distribution-id ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} --paths "/*" + +# preview: +# runs-on: ubuntu-latest +# if: github.event_name == 'pull_request' +# steps: +# - name: Checkout code +# uses: actions/checkout@v2 + +# - name: Install AWS CLI +# run: | +# sudo apt-get update +# sudo apt-get install awscli -y + +# - name: Sync S3 bucket for preview +# env: +# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} +# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} +# AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }} +# run: | +# aws s3 sync ./docs s3://your-bucket-name/${{ env.PR_NUMBER }} + +# - name: Invalidate CloudFront cache +# env: +# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} +# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} +# AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }} +# run: | +# aws cloudfront create-invalidation --distribution-id ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} --paths "/${{ env.PR_NUMBER }}/*" + +# - name: Output preview URL +# run: | +# echo "Preview environment URL: https://your-cloudfront-domain/${{ env.PR_NUMBER }}/" + +# - name: Comment on PR with preview URL +# uses: actions/github-script@v6 +# with: +# script: | +# github.rest.issues.createComment({ +# issue_number: context.issue.number, +# owner: context.repo.owner, +# repo: context.repo.repo, +# body: `Preview environment URL: https://your-cloudfront-domain/${{ env.PR_NUMBER }}/` +# }) + +# cleanup: +# runs-on: ubuntu-latest +# if: github.event_name == 'pull_request' && github.event.action == 'closed' +# steps: +# - name: Install AWS CLI +# run: | +# sudo apt-get update +# sudo apt-get install awscli -y + +# - name: Remove PR directory from S3 bucket +# env: +# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} +# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} +# AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }} +# run: | +# aws s3 rm s3://your-bucket-name/pr-${{ env.PR_NUMBER }} --recursive + +# - name: Invalidate CloudFront cache +# env: +# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} +# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} +# AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }} +# run: | +# aws cloudfront create-invalidation --distribution-id ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} --paths "/pr-${{ env.PR_NUMBER }}/*" diff --git a/.github/workflows/create-preview-on-pr-v3.yaml b/.github/workflows/create-preview-on-pr-v3.yaml new file mode 100644 index 0000000000..2c3f955e65 --- /dev/null +++ b/.github/workflows/create-preview-on-pr-v3.yaml @@ -0,0 +1,87 @@ +name: Create Preview Environment + +on: + pull_request: + types: [opened, reopened, synchronize, closed] + branches: + - master + - v*.* + +jobs: + build-and-deploy: + if: github.event.action != 'closed' + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: setup python + uses: actions/setup-python@v5 + with: + python-version: '3.9' + cache: 'pip' # caching pip dependencies + + - name: install dependencies + run: | + pip3 install -r requirements.txt + + - name: mkdocs-build-action + run: | + mkdocs build + + - name: Install AWS CLI + run: | + sudo apt-get update + sudo apt-get install awscli -y + + - name: Sync S3 bucket for preview + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }} + run: | + aws s3 sync ./site/ s3://${{ secrets.AWS_S3_BUCKET_NAME }}/PR-${{ github.event.number }} --delete + + - name: Invalidate CloudFront cache + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }} + run: | + aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_CLOUDFRONT_DISTRIBUTION_ID }} --paths "/PR-${{ github.event.number }}/*" + + - name: Comment on PR with preview URL + uses: actions/github-script@v6 + with: + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `Preview environment URL: https://your-cloudfront-domain/PR-${{ github.event.number }}/` + }) + + cleanup: + if: github.event.action == 'closed' + runs-on: ubuntu-latest + steps: + - name: Install AWS CLI + run: | + sudo apt-get update + sudo apt-get install awscli -y + + - name: Remove PR directory from S3 bucket + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }} + run: | + aws s3 rm s3://${{ secrets.AWS_S3_BUCKET_NAME }}/PR-${{ github.event.number }} --recursive + + - name: Invalidate CloudFront cache + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }} + run: | + aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_CLOUDFRONT_DISTRIBUTION_ID }} --paths "/PR-${{ github.event.number }}/*" diff --git a/.github/workflows/deploy-staging.yaml b/.github/workflows/deploy-staging.yaml index 4fadb8dbfe..feacb391d5 100644 --- a/.github/workflows/deploy-staging.yaml +++ b/.github/workflows/deploy-staging.yaml @@ -41,7 +41,7 @@ jobs: with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - aws-region: us-east-1 + aws-region: ${{ secrets.AWS_DEFAULT_REGION}} - name: Sync output to S3 run: |