- Published on
- · 3 min read
Deploy frontend application (React) to Vercel using Github Action - Part 3
- Authors
- Name
- Nguyễn Tạ Minh Trung
Before getting with this session, if you don't know how to create a Vercel app and use it with GitHub Action. Please refer to Deploy frontend application (React) to Vercel using Github Action - Part 1 and Deploy frontend application (React) to Vercel using Github Action - Part 2.
Get started
In part 2, we've already have a full buildAndDeployment.yaml
file. Let's re-use it for this session as following.
- GitHub Action introduces
workflow_dispatch
to manually trigger the pipeline
workflow_dispatch:
inputs:
env:
description: 'Environment'
required: true
type: choice
default: preview
options: [ preview, production ]
- At this configuration, we have to define the
inputs
for the workflow. We only need to environment which we target to, then defineenv
with a typechoice
to choose the velue fromoptions
. Don't forget to setdefault
value is one of item in theoptions
. - GitHub Action automatic have branch dropdown within
workflow_dispatch
selection, you can choose thebranch
you need to build and deploy and theenv
from theinputs
- Next, let's update the Setup stage a little bit, focus on step
Check target deployment
- name: Check target deployment
id: check-target-deployment
run: |
echo "${{ github.event_name }}"
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
echo "env=${{ github.event.inputs.env }}" >> $GITHUB_OUTPUT
elif [[ "${{ github.ref }}" == "refs/heads/master" ]]; then
echo "env=production" >> $GITHUB_OUTPUT
else
echo "env=preview" >> $GITHUB_OUTPUT
fi
- GitHub Action provide the
github.event_name
with have a lot of events from GitHub. In order to work withworkflow_dispatch
, we have to usegithub.event_name
asworkflow_dispatch
and retrieve theenv
which we defined to store into the $GITHUB_OUTPUT - The following is the fully of the Setup stage which includes both of automatic trigger by pushing the code to
develop
andmaster
branches, and manually trigger the workflow byworkflow_dispatch
:
setup:
name: Setup
runs-on: ubuntu-latest
outputs:
env: ${{ steps.check-target-deployment.outputs.env }}
steps:
- name: Check target deployment
id: check-target-deployment
run: |
echo "${{ github.event_name }}"
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
echo "env=${{ github.event.inputs.env }}" >> $GITHUB_OUTPUT
elif [[ "${{ github.ref }}" == "refs/heads/master" ]]; then
echo "env=production" >> $GITHUB_OUTPUT
else
echo "env=preview" >> $GITHUB_OUTPUT
fi
- name: Checkout Code
uses: actions/checkout@v2
- name: Install Vercel CLI
run: npm install --global vercel@latest
- name: Pull Vercel Environment Information
run: |
vercel pull --yes --environment=${{ steps.check-target-deployment.outputs.env }} \
--token=${{ secrets.VERCEL_TOKEN }}
- name: List Vercel Environment Files
run: ls -la .vercel
- name: Upload Vercel Environment Files as Artifact
uses: actions/upload-artifact@v4
with:
name: vercel-environment-files
path: .vercel/
include-hidden-files: true
- Adjust
buildAndDeployment.yaml
with new Setup stage. Pushing the changes to GitHub, release the changes tomaster
and check the Actions. You will see theworkflow_dispatch
dropdown
Testing time
This is your time to testing the manually build and deploy with 
workflow_dispatch
event from GitHub. Choose the branch
and env
, then click Run workflow
, and alert This workflow has a workflow_dispatch event trigger. will be show as below: 
Conclusion
GitHub Action provide to us a lot of events which developers can use to trigger the pipeline, workflow_dispatch
is one. Use workflow_dispatch
to manually trigger the deployment if you would like to verify the build and deployment on any branches and environment you have.