staging #25222
This file contains 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: Run Tests and Deploy | |
on: | |
deployment: | |
branches: | |
- master | |
- develop | |
pull_request: | |
branches: | |
- master | |
push: | |
branches: | |
- master | |
- develop | |
# ------- Environment | |
env: | |
# calculated values (for steps.env.outputs.*) | |
APP_ENV: ${{ (github.event_name == 'deployment' && github.event.deployment.environment) || (github.ref == 'refs/heads/master' && 'production') || 'staging' }} | |
SHOULD_DEPLOY: ${{ github.event_name != 'pull_request' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/develop') && 'true' || 'false' }} | |
# for deploy | |
AWS_DEPLOY_ACCESS_ID: ${{ secrets.AWS_DEPLOY_ACCESS_ID }} | |
AWS_DEPLOY_SECRET_KEY: ${{ secrets.AWS_DEPLOY_SECRET_KEY }} | |
# build, test, and deploy | |
CONTENTFUL_MASTER_TOKEN: ${{ secrets.CONTENTFUL_MASTER_TOKEN }} | |
CONTENTFUL_PREVIEW_TOKEN: ${{ secrets.CONTENTFUL_PREVIEW_TOKEN }} | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Convert Environment to Output | |
id: env | |
# Unfortunately we are unable to access env (eg. SHOULD_DEPLOY) inside of an expression (eg. if, with, etc). | |
# So we have to convert environment variables to output variables. | |
run: | | |
echo ::set-output name=app_env::$APP_ENV | |
echo ::set-output name=should_deploy::$SHOULD_DEPLOY | |
- name: DEBUG Calculated Environment | |
# app_env = String production|staging | |
# should_deploy = String true|false # deploys if event != pull_request on develop and master branches | |
run: | | |
echo "app_env=${{ steps.env.outputs.app_env }}" | |
echo "should_deploy=${{ steps.env.outputs.should_deploy }}" | |
# -------- Github Checkout | |
- uses: actions/checkout@v3 | |
# -------- Ruby & Dependencies | |
- name: Install Ruby | |
uses: ruby/setup-ruby@v1 | |
with: | |
bundler-cache: true | |
# -------- JS & Dependencies | |
- name: Install Node | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '10.x' | |
- name: Install Javascript Dependencies | |
run: yarn install --frozen-lockfile --non-interactive | |
# -------- Build Static Files | |
- name: Download Data from Contentful | |
run: bundle exec middleman contentful | |
- name: Build Static HTML | |
run: bundle exec middleman build --verbose | |
# -------- Run Tests | |
- name: Run Ruby Tests | |
run: bundle exec rspec spec -p --format d | |
# -------- Deploy to S3 and notify Github Deployment of Status | |
- name: Update deployment status to pending | |
# NOTE: Only for deployment events, not commits/etc (which don't have an external deployment context) | |
uses: 'deliverybot/[email protected]' | |
if: github.event_name == 'deployment' && success() && steps.env.outputs.should_deploy == 'true' | |
with: | |
state: 'pending' | |
token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Deploy to S3 | |
if: success() && steps.env.outputs.should_deploy == 'true' | |
run: bundle exec middleman s3_sync | |
- name: Update deployment status to success | |
# NOTE: Only for deployment events, not commits/etc (which don't have an external deployment context) | |
if: github.event_name == 'deployment' && success() && steps.env.outputs.should_deploy == 'true' | |
uses: 'deliverybot/[email protected]' | |
with: | |
state: 'success' | |
token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Update deployment status to failure | |
# NOTE: Only for deployment events, not commits/etc (which don't have an external deployment context) | |
if: github.event_name == 'deployment' && failure() && steps.env.outputs.should_deploy == 'true' | |
uses: 'deliverybot/[email protected]' | |
with: | |
state: 'failure' | |
token: ${{ secrets.GITHUB_TOKEN }} | |
# -------- Slack Notifications (different for test vs. deploy). | |
- name: Send Deploy Notification | |
uses: lazy-actions/[email protected] | |
if: steps.env.outputs.should_deploy == 'true' && always() | |
with: | |
type: ${{ job.status }} | |
job_name: "[help-${{ steps.env.outputs.app_env }}] Deploy" | |
channel: '#static_site_builds' | |
icon_emoji: ':rocket:' | |
url: ${{ secrets.SLACK_WEBHOOK_URL }} | |
- name: Send Test Failed Notification | |
uses: lazy-actions/[email protected] | |
if: steps.env.outputs.should_deploy == 'false' && failure() | |
with: | |
type: ${{ job.status }} | |
job_name: "[help-${{ github.ref }}] Tests" | |
channel: '#static_site_builds' | |
icon_emoji: ':goose:' | |
url: ${{ secrets.SLACK_WEBHOOK_URL }} |