GuidesAdvancedGitHub Actions

Using GitHub Actions Deployment Events

GitHub Actions are a great way to wire together Flightcontrol deployments with other services. For example, to set Vercel environment variables or start an end-to-end integration test.

Example of getting service domain URL on deploy success

This action runs on GitHub’s deployment_status event. It uses the if: to filter a few things:

  • Only run on deploy success: github.event.deployment_status.state == 'success'
  • Only run for a specific Flightcontrol project: github.event.deployment.payload.deployment.projectId == 'cluagwibz000ka6p9uzas7p26'
  • Only run for preview environment: github.event.deployment.payload.deployment.isPreviewEnvironment == true

You can adjust those filters as needed. For example, you can also filter by github.event.deployment.payload.deployment.environmentId or github.event.deployment.payload.deployment.environmentGivenId. But note that environmentId for preview environments will be different for each PR.

Additionally, it gets domain url of the FC_SERVICE_GIVEN_ID: "my-web-app" service. It does this by using the jq utility to search github.event.deployment.payload.deployment.serviceDeployments[] for the service with this given id and then selects the serviceDomain field.

name: Deployment
 
on:
  deployment_status:
 
env:
  FC_PROJECT_ID: "cluagwibz000ka6p9uzas7p26"
  FC_SERVICE_GIVEN_ID: "backend-api"
 
jobs:
  run-deployment:
    if: >
      github.event_name == 'deployment_status' &&
      github.event.deployment_status.state == 'success' &&
      github.event.deployment.payload.deployment.projectId == '${{ env.FC_PROJECT_ID }}' &&
      github.event.deployment.payload.deployment.isPreviewEnvironment == true
 
    runs-on: ubuntu-latest
 
    steps:
      - uses: actions/checkout@v2
 
      - name: Set DOMAIN env varaible
        run: |
          RESULT=$(echo '${{ toJson(github.event.deployment.payload.deployment) }}' | jq -r --arg serviceGivenId "$FC_SERVICE_GIVEN_ID" '.serviceDeployments[] | select(.serviceGivenId == $serviceGivenId) | .serviceDomain')
          echo "Domain $RESULT"
          echo "DOMAIN=$RESULT" >> $GITHUB_ENV
 
      - name: Do some action with $DOMAIN like set Vercel env variable or start e2e test
        run: echo true

Example of fetching deployment information

This action runs on GitHub’s deployment_status event.

It sets the FC_DEPLOYMENT_ID environment variable by reading github.event.deployment.payload.deployment.deploymentId. Then it uses the FC_API_KEY GitHub secret set on the repo to fetch our Get Deployment API endpoint

name: Deployment
 
on:
  deployment_status:
 
jobs:
  fetch-deployment:
    env:
      FC_DEPLOYMENT_ID: ${{ github.event.deployment.payload.deployment.deploymentId }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
       - name:  deployment
       - name: Fetch deployment
         run: |
           curl -H "Authorization: Bearer ${{ secrets.FC_API_KEY }}" https://api.flightcontrol.dev/v1/deployments/$FC_DEPLOYMENT_ID

Example triggering Vercel build on Flightcontrol deployment success

If you have a backend API on Flightcontrol and a frontend on Vercel, this shows how you can pass the backend preview environment URL to the Vercel frontend preview environment so you get an end-to-end preview environment experience.

First you must set the VERCEL_TOKEN GitHub secret on the repo. This is used in the action to call the Vercel API.

name: Trigger Vercel Build on Flightcontrol Deployment Success
 
on:
  deployment_status:
 
env:
  VERCEL_TEAM_ID: "team_4Z98W1jFKBTL8XIe3fvWaeSu"
  VERCEL_PROJECT_NAME: "frontend-dashboard"
  FC_PROJECT_ID: "cluagwibz000ka6p9uzas7p26"
  FC_SERVICE_GIVEN_ID: "backend-api"
 
jobs:
  trigger-vercel:
    runs-on:
      - runs-on=${{ github.run_id }}
      - runner=2cpu-linux-x64
    if: >
      github.event_name == 'deployment_status' &&
      github.event.deployment_status.state == 'success' &&
      github.event.deployment.payload.deployment.projectId == '${{ env.FC_PROJECT_ID }}' &&
      github.event.deployment.payload.deployment.isPreviewEnvironment == true
 
    steps:
      - name: Extract Deployment Domain
        id: extract-domain
        run: |
          echo "Full deployment payload:"
          echo '${{ toJson(github.event.deployment.payload.deployment) }}'
          DOMAIN=$(echo '${{ toJson(github.event.deployment.payload.deployment) }}' | jq -r --arg serviceGivenId "${{ env.FC_SERVICE_GIVEN_ID }}" '.serviceDeployments[] | select(.serviceGivenId == $serviceGivenId) | .serviceDomain')
          BRANCH_NAME=$(echo '${{ toJson(github.event.deployment.payload.deployment) }}' | jq -r '.gitBranch')
 
          echo "Deployment domain: $DOMAIN"
          echo "DOMAIN=$DOMAIN" >> $GITHUB_ENV
          echo "Branch: $BRANCH_NAME"
          echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
 
      - name: Update Preview Environment Variables
        env:
          VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
        run: |
          curl -X POST "https://api.vercel.com/v9/projects/${{ env.VERCEL_PROJECT_NAME }}/env?teamId=${{ env.VERCEL_TEAM_ID }}" \
            -H "Authorization: Bearer $VERCEL_TOKEN" \
            -H "Content-Type: application/json" \
            -d '{
              "key": "NEXT_PUBLIC_BACKEND_URI",
              "value": "'$DOMAIN'",
              "type": "plain",
              "target": ["preview"],
              "gitBranch": "'$BRANCH_NAME'"
            }'
 
      - name: Trigger Vercel Build
        env:
          VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
        run: |
          echo "Using DOMAIN: $DOMAIN"
          curl -X POST "https://api.vercel.com/v13/deployments?teamId=${{ env.VERCEL_TEAM_ID }}" \
            -H "Authorization: Bearer $VERCEL_TOKEN" \
            -H "Content-Type: application/json" \
            -d '{
              "name": "${{ env.VERCEL_PROJECT_NAME }}",
              "gitSource": {
                "type": "github",
                "org": "${{ github.repository_owner }}",
                "repo": "${{ github.event.repository.name }}",
                "branch": "'$BRANCH_NAME'",
                "ref": "'$BRANCH_NAME'"
              },
              "projectSettings": {
                "commandForIgnoringBuildStep": "exit 1"
              }
            }'