DEV Community

Victorio Berra
Victorio Berra

Posted on

1 1

GitHub Actions Mini Tutorial - Only Deploy Master

I thought id share a little info on how to have a conditional statement for your actions that ONLY executes if you are on master.

Firstly, only run on pushes to master, and PRs to master.

on:
  # Trigger the workflow on push or pull request for master only.
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

Then finally, in any action:

    # Upload to S3
    - name: sync s3
      # Do not deploy anything other than master
      if: github.ref == 'refs/heads/master'    
      uses: jakejarvis/s3-sync-action@master
      with:
        args: --acl public-read --follow-symlinks --delete
      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'
        SOURCE_DIR: 'dist'

The key above being: if: github.ref == 'refs/heads/master'

Personally I wish they would make the branch name an env variable to make this much cleaner but Actions are still new.

Happy deploying!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Retry later