Categorygithub.com/razzkumar/PR-Automation
modulepackage
1.0.2
Repository: https://github.com/razzkumar/pr-automation.git
Documentation: pkg.go.dev

# README

Static Site Automation

What it is?

This Gihub action that uses the golang aws sdk to build s3 bucket and attach policy for static website deploy the static file to that newly created s3 bucket and comment the url to the PR. To deploy static file it uses either from your repository or build during your workflow. There is self hosted tool, if Github action is not feasible.

Best for?

  • Immediate feedback visually to developers or anyone interested in changes.
  • Reduce burden of having to build application for QA and verify the changes.
  • Faster iterations.

How to use?

Add .yml file/s such as given examples in your .github/workflows folder. Refer to the documentation on workflow YAML syntax here.

The following example will:
  • Create s3 bucket and attach policy for static site
  • Build the javascript/typescript frontend application with the help of given command (ex: BUILD_COMMAND="yarn build")
  • Upload build file (static site) to s3
  • Comment the URL of the static site to the Pull Request
  • Delete the aws S3 bucket after PR is merged
Config file: .github/workflows/deploy-existing.yml
name: Next js frontend dev

on:
  push:
    branches:
    - dev

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Build and deploy Studio app
      uses: razzkumar/[email protected]
      env:
        AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }} 
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        AWS_REGION: "us-east-1"
        SRC_FOLDER: "out"
        ACTION: 'deploy'
        BUILD_COMMAND: "yarn build && yarn export"
        CLOUDFRONT_ID: ${{ secrets.CLOUDFRONT_ID }}
        SECRETS_MANAGER: ${{ secrets.SECRETS_MANAGER }} // name of secrets on secret manager
Config file: .github/workflows/deploy-on-pr.yml
name: Deploy site to S3 And add comment to PR and delete after merge

on:
  pull_request:
    branches:
    - master

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Static site deploy to s3 and comment on PR
      uses: razzkumar/[email protected]
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        GH_ACCESS_TOKEN: ${{ secrets.GH_ACCESS_TOKEN}}
        AWS_REGION: 'us-east-2'     # optional: defaults to us-east-2
        SRC_FOLDER: 'build'         # optional: defaults to build (react app)
        IS_BUILD: 'true'            # optional: defaults to true
        ACTION: "create"            # optional: defaults to create (option:create,delete and deploy)
        BUILD_COMMAND: "yarn build" # optional: defaults to `yarn build`
Config file: .github/workflows/cleanup-on-pr-merge.yml
name: Delete S3 bucket after PR merge

on:
  pull_request:
    types: [closed]

jobs:
  delete:
    runs-on: ubuntu-latest
    steps:
    - name: Clean up temperory bucket
      if: github.event.pull_request.merged == true
      uses: razzkumar/[email protected]
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        AWS_REGION: 'us-east-2'     # optional: defaults to us-east-2
        ACTION: "delete"            # Action must be delete to delete

Configuration

The following settings must be passed as environment variables as shown in the example. Sensitive information, especially GH_ACCESS_TOKEN,AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, should be set as encrypted secrets — otherwise, they'll be public to anyone browsing your repository's source code and CI logs.

KeySuggested TypeValueRequiredDefault
GH_ACCESS_TOKENsecrect envYour Github access token used while commenting PRYES/NO If ACTION: create then it's required,otherwise it's optionalNA
AWS_ACCESS_KEY_IDsecret envYour AWS Access Key. More info here.YesN/A
AWS_SECRET_ACCESS_KEYsecret envYour AWS Secret Access Key. More info here.YesN/A
AWS_S3_BUCKETsecret envThe name of the bucket you're syncing to. For example, jarv.is or my-app-releases.YES/NO- If running on PR it will genereat by tool PR-Branch.prPR-number.auto-deploy - In the case of depoyment it required
AWS_REGIONenvThe region where you created your bucket. Set to us-east-2 by default. Full list of regions here.Nous-east-2
SRC_FOLDERenvThe local directory (or file) you wish to deploy to S3. For example, public. Defaults to build.Nobuild (based on react app)
IS_BUILDenvThis is the flag that indicate that build a project or notNotrue (It will run yarn && yarn build by default)
ACTIONenvThis is also a flag that indicate what to do (create:-create s3 (if not exist) bucket,build react and comment on PR,deploy:helps to deploy to s3,delete: delete the s3 bucket)Nocreate (It will create s3 (if not exist),built the app, deploy to s3 and comment URL to PR`)
BUILD_COMMANDenvHow to build the react app if its npm run build then it will run npm install && npm run buildNoyarn build (It will run yarn && yarn build by default)
CLOUDFRONT_IDsecret envid of cloudfront for invalidationNo
SECRETS_MANAGERenvname of the aws secres manager keyNo

Note for S3 Bucket creation

  • It only create a s3 bucket if not exist
  • While Creating bucket for the pull_request S3 bucket name will be: PR-Branch.prPR-number.auto-deploy
    • For Eg.:
      • if base branch is SIG-1000 and PR number is 23 the the bucket name will be sig-100.pr23.auto-deploy
  • If we deploy app on push or (not on pull requst) like prebuild app deployment, app build and deploy then the bucket name will be $AWS_S3_BUCKET.auto-deploy
    • For Eg.
      • if AWS_S3_BUCKET=dev-test-deployment then bucket will be dev-test-deployment.auto-deploy

TODO

  • Add tests
  • Add option to deploy on aws cloudfront
  • Design PR comment done by tool
  • Maintain code quality

Contributing

Feel free to send pull requests

License

This project is distributed under the MIT license

HitCount

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author