```yaml --- ################################### ################################### # # # EBS DEPLOY GITHUB ACTION # # DEV AND PROD ENVS # # AND # # CONTAINER REGISTRY # # IMAGE PUBLISH # # # ################################### ################################### name: Elastic Beanstalk API Deploys # Documentation: # https://help.github.com/en/articles/workflow-syntax-for-github-actions # https://docs.github.com/en/actions/learn-github-actions/contexts#github-context # # Actions: # https://github.com/actions/checkout # https://github.com/rtCamp/action-slack-notify # https://github.com/actions/setup-node # https://github.com/gerred/current-time # https://github.com/frabert/replace-string-action # https://github.com/einaregilsson/beanstalk-deploy # https://github.com/rtCamp/action-slack-notify on: push: branches: - dev - main jobs: image_push: name: Github Container Registry Push if: ${{ github.ref == 'refs/heads/dev' }} concurrency: group: image_push-${{ github.ref }} runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 env: DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: API env for testing run: | touch .env echo NODE_ENV=testing >> .env echo HOST=${{ secrets.TESTING_HOST }} >> .env echo PORT=${{ secrets.TESTING_PORT }} >> .env echo POSTGRES_HOST=${{ secrets.POSTGRES_HOST }} >> .env echo POSTGRES_PORT=${{ secrets.POSTGRES_PORT }} >> .env echo POSTGRES_USER=${{ secrets.POSTGRES_USER }} >> .env echo POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }} >> .env echo POSTGRES_DB=${{ secrets.POSTGRES_DB }} >> .env # echo rest of vars needed to run tests - name: Container Build & Image Tag id: container_build run: | docker compose build docker tag :latest ghcr.io// - name: GitHub Container Registry Auth id: ghcr_auth run: echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u $ --password-stdin - name: Image Push id: image_push run: docker push ghcr.io//:latest - name: Slack Notification if: always() uses: rtCamp/action-slack-notify@v2 env: SLACK_CHANNEL: ci SLACK_COLOR: ${{ job.status }} SLACK_FOOTER: '' SLACK_LINK_NAMES: true SLACK_MESSAGE: 'Image published on ghcr' SLACK_USERNAME: SLACK_WEBHOOK: ${{ secrets.CI_SLACK_WEBHOOK }} dev_deploy: # runs on push events to dev name: dev/test deploy if: ${{ github.ref == 'refs/heads/dev' }} # cancel workflow if currently running concurrency: group: dev_deploy-${{ github.ref }} cancel-in-progress: true runs-on: ubuntu-latest env: NODE_ENV: 'production' steps: - name: Checkout uses: actions/checkout@v4 - name: Node Setup uses: actions/setup-node@v4 with: node-version: 16.17.0 - name: Cache node modules id: cache-npm uses: actions/cache@v3 env: cache-name: cache-node-modules with: # npm cache files are stored in `~/.npm` on Linux/macOS path: ~/.npm key: ${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} restore-keys: | ${{ env.cache-name }}- - if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }} name: List the state of node modules continue-on-error: true run: npm list - name: TSC Install run: | npm install -g typescript tsc -v - name: Dependency Install run: npm install --production=false - name: Compile Application run: npm run compile - name: Generate Bundle run: zip -r deploy.zip . -x '.*git*' - name: Grab Timestamp uses: gerred/actions/current-time@master id: current-time - name: Format Timestamp uses: frabert/replace-string-action@master id: format-time with: pattern: '[:\.]+' string: '${{ steps.current-time.outputs.time }}' replace-with: '-' flags: 'g' - name: Elastic Beanstalk Dev Deploy uses: einaregilsson/beanstalk-deploy@v21 with: aws_access_key: ${{ secrets.AWS_EB_ACCESS_KEY }} aws_secret_key: ${{ secrets.AWS_EB_SECRET_KEY }} application_name: ${{ secrets.AWS_EB_APP_NAME }} deployment_package: deploy.zip environment_name: ${{ secrets.AWS_EB_APP_ENV_DEV }} region: us-east-1 version_label: ' -${{ steps.format-time.outputs.replaced }}' - name: Get Commit Message id: deploy_message run: | echo "::setOutput name=commit_msg::$(git log --format=%B -n 1 $(git log -1 --pretty=format:%h))" echo "::setOutput name=commit_sha::$(git log -1 --pretty=format:%H)" - name: Slack Notification if: always() uses: rtCamp/action-slack-notify@v2 env: SLACK_CHANNEL: deploy-release SLACK_COLOR: ${{ job.status }} SLACK_FOOTER: '' SLACK_LINK_NAMES: true SLACK_MESSAGE: ${{ steps.deploy_message.outputs.commit_msg }} SLACK_USERNAME: SLACK_WEBHOOK: ${{ secrets.DEPLOY_SLACK_WEBHOOK }} prod_deploy: # should only run on push events to main name: prod deploy if: ${{ github.ref == 'refs/heads/main' }} # cancel workflow if currently running concurrency: group: prod_deploy-${{ github.ref }} cancel-in-progress: true runs-on: ubuntu-latest env: NODE_ENV: 'production' steps: - name: Checkout uses: actions/checkout@v4 - name: Node Setup uses: actions/setup-node@v4 with: node-version: 16.17.0 - name: Cache node modules id: cache-npm uses: actions/cache@v3 env: cache-name: cache-node-modules with: # npm cache files are stored in `~/.npm` on Linux/macOS path: ~/.npm key: ${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} restore-keys: | ${{ env.cache-name }}- - if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }} name: List the state of node modules continue-on-error: true run: npm list - name: TSC Install run: | npm install -g typescript tsc -v - name: Dependency Install run: npm install --production=false - name: Compile Application run: npm run compile # "compile": "tsc -p .", - name: Source Map Upload to Bugsnag run: npm run postbuild # "postbuild": "node lib/scripts/upload-bugsnag-sourcemaps.js", - name: Generate Bundle run: zip -r deploy.zip . -x '.*git*' - name: Grab Timestamp uses: gerred/actions/current-time@master id: current-time - name: Format Timestamp uses: frabert/replace-string-action@master id: format-time with: pattern: '[:\.]+' string: '${{ steps.current-time.outputs.time }}' replace-with: '-' flags: 'g' - name: Elastic Beanstalk Prod Deploy uses: einaregilsson/beanstalk-deploy@v21 with: aws_access_key: ${{ secrets.AWS_EB_ACCESS_KEY }} aws_secret_key: ${{ secrets.AWS_EB_SECRET_KEY }} application_name: ${{ secrets.AWS_EB_APP_NAME }} deployment_package: deploy.zip environment_name: ${{ secrets.AWS_EB_APP_ENV_PROD }} region: us-east-1 version_label: ' SLACK_WEBHOOK: ${{ secrets.DEPLOY_SLACK_WEBHOOK }} ```