SECRET OF CSS

How to Create GitHub Actions for Vercel Deployment | by Arctype | Jun, 2022


To create projects easier and faster

How to create Github Actions for Vercel Deployment
image by author

Vercel is one of the best hosting services because of its simplicity and adaptability. It allows you to host your project as fast as possible with little or no setup. When Vercel is used with GitHub for project deployment, things get much more fascinating. When a new update is pushed to GitHub, this enables automated code deployment, streamlining your CI/CD workflow.

GitHub Actions is a CI/CD platform for automating your build, test, and deployment workflows. You can build and test every pull request in your repository using workflows, or you may deploy merged pull requests to the production repository.

GitHub Actions make it simple to automate all of your software activities, and it now includes world-class CI/CD, which allows you to customize code reviews, branch management, and issue triaging.

​The GitHub actions automatically deploy your GitHub projects with Vercel, providing preview deployment URLs and automatic custom domain updates. When you create a new Vercel project on GitHub and enable automatic deployments on every branch, Vercel watches every push on the branch and deploys by default. If Vercel is already building a previous commit, the current build will be canceled to build the most recent commit so that you always have the latest changes deployed.

  • Option 1 for testing: The Fork Model
  • Option 2 for testing: tmate

GitHub Actions in Action

To demonstrate how to deploy an application to Vercel using GitHub actions, I have created and pushed a simple React project to my GitHub repository. Clone the repository to your computer and follow along.

Configuring Vercel

To set up GitHub action with Vercel, you need a Vercel project ID and org ID. To do that, run the command below and follow the prompts:

vercel

The above command will generate a .vercel/package.json file where your projectId and orgId are generated.

Perhaps, you can tell Vercel not to trigger project deployment when you push your code to GitHub. This way, the GitHub Actions workflow only gets triggered when there is a release, push to a specific branch, or manual trigger on the workflow. To do, create a vercel.json file in your project root directory and add the configurations below:

{
"github": {
"enabled": false,
"silent": true
}
}

Next, you need to generate a token on Vercel to enable you to use the CLI on GitHub Actions. Once you generate the token, copy the token and save it in a safe place. You’ll need it later.

Then push your project to GitHub. Once the push is completed, go to the project on GitHub. In the settings tab, click on “Secrets > Actions” and add the Vercel credentials to your project. On the Actions page, click on the Create repository secret button and add the following secrets.

  • VERCEL_ORG_ID: This is the orgId in the .vercel/package.json file in the project root directory.
  • VERCEL_PROJECT_ID: This is the projectId in the .vercel/package.json file in the project root directory.
  • VERCEL_TOKEN: This is the vercel token you generated.

After creating the above credentials, we should be ready to configure the workflow.

With the Vercel your Vercel credentials added to your project on GitHub, go ahead and configure the GitHub workflow to enable continuous and continuous deployment. To do that, create a .github/workflows/deploy.yml file in the project root directory and add the following configurations specified below:

Now commit the changes and push your code to GitHub. Once the code is pushed, The GitHub actions will trigger an automatic deployment.

Throughout this tutorial, you’ve learned how to set up GitHub Actions to deploy your project to Vercel. You learned what GitHub actions are and how they work with Vercel. Now that you have learned how GitHub and Vercel work together, feel free to glance at some more configurations to add to your project.



News Credit

%d bloggers like this: