Skip to content

Commit

Permalink
Add Smart Previews PR mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
haimlevy2006 committed Jul 9, 2024
1 parent 026f91a commit 5f281a1
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 12 deletions.
19 changes: 8 additions & 11 deletions .github/workflows/automated-publish-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
push:
branches:
- v*.*
paths-ignore:
- 'github/**'

jobs:
env:
Expand Down Expand Up @@ -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:
Expand All @@ -63,12 +65,7 @@ jobs:
git config user.name "circleci-runai"
git config user.email "[email protected]"
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..."
Expand All @@ -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:
Expand All @@ -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: ""
110 changes: 110 additions & 0 deletions .github/workflows/create-preview-on-pr-v2.yaml
Original file line number Diff line number Diff line change
@@ -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 }}/*"
87 changes: 87 additions & 0 deletions .github/workflows/create-preview-on-pr-v3.yaml
Original file line number Diff line number Diff line change
@@ -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 }}/*"
2 changes: 1 addition & 1 deletion .github/workflows/deploy-staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down

0 comments on commit 5f281a1

Please sign in to comment.