GitHub Actions

Begin makes it easy to deploy from CI by providing integration with GitHub Actions.

Let’s work through an example of how you could use the action in your GitHub workflow. In this example, we’ll assume that you’ve already created your Begin app and two environments — one named staging and, and another, production.

Retrieve your Begin authentication token

Begin Deploy’s CLI provides the tokens list command to retrieve your authentication token. Run the following command in your terminal:

Note that the --display flag will print your token to the terminal. Be sure to keep this token secure and never check it in to source code control.

npx begin tokens list --display
npx begin tokens list --display

Copy your “client token”.

Create a BEGIN_TOKEN secret on Github

  1. On GitHub, navigate to the main page of your app’s repository.
  2. Under your repository name, click Settings.
  3. In the Security section of the sidebar, select Secrets, then click Actions.
  4. Click New repository secret.
  5. Type BEGIN_TOKEN as the name for your secret in the Name input field.
  6. Enter the value of your access_token for the Secret input field.
  7. Click Add secret.

If you’ll be deploying multiple apps in the same GitHub organization, your respositories can share the same BEGIN_TOKEN via Organization Secrets.

To prevent security risks, never check your access_token in to source code control!

Example GitHub Actions

You can automatically deploy your code to Begin via GitHub Actions by creating a .github/workflows/deploy.yml file in your app’s repository. Below you can see two examples of how to deploy using tag based or branch based deployments.

Tag based deployment

Our recommended approach is to do tag based deployment. When this GitHub Action is installed, all commits to the main branch will be deployed to your staging environment. When a new tag is created on the repo, this will trigger a deploy to production.

name: Begin deploy

on: [ push, pull_request ]

defaults:
  run:
    shell: bash

jobs:
  # Deploy the build
  deploy:
    runs-on: ubuntu-latest
    if: github.event_name == 'push'

    steps:
      - name: Check out repo
        uses: actions/checkout@v4

      - name: Deploy to staging
        if: github.ref == 'refs/heads/main'
        uses: beginner-corp/actions/deploy@v1
        with:
          begin_token: ${{ secrets.BEGIN_TOKEN }}
          begin_env_name: staging

      - name: Deploy to production
        if: startsWith(github.ref, 'refs/tags/v')
        uses: beginner-corp/actions/deploy@v1
        with:
          begin_token: ${{ secrets.BEGIN_TOKEN }}
          begin_env_name: production

Branch based deployment

An alternative approach is to do branch based deployment. When this GitHub Action is installed, all commits to the main branch will be deployed to your staging environment. Commits to the production branch will be deployed to your production environment.

Feel free to adjust branch names as required.

name: Begin deploy

on: [ push, pull_request ]

defaults:
  run:
    shell: bash

jobs:
  # Deploy the build
  deploy:
    runs-on: ubuntu-latest
    if: github.event_name == 'push'

    steps:
      - name: Check out repo
        uses: actions/checkout@v4

      - name: Deploy to staging
        if: github.ref == 'refs/heads/main'
        uses: beginner-corp/actions/deploy@v1
        with:
          begin_token: ${{ secrets.BEGIN_TOKEN }}
          begin_env_name: staging

      - name: Deploy to production
        if: github.ref == 'refs/heads/production'
        uses: beginner-corp/actions/deploy@v1
        with:
          begin_token: ${{ secrets.BEGIN_TOKEN }}
          begin_env_name: production