Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for React App and GitHub Actions
Niladri
Niladri

Posted on • Originally published atMedium

     

React App and GitHub Actions

React App and GitHub Actions

From development to go-live in under 2 minutes

Who are we

As developers, we are often guilty of doing this. The problem statement is simple:

I have a React application and want it deployed on GitHub Pages.

The simplest way to achieve this is to locally build it, and push the build directory to “gh-pages” branch and viola. Then I introducedTravis CI to achieve this and recently switched to GitHub Actions.

I have already spent more hours setting this up than I probably would have spent doing it manually, so let me first try to justify my actions:

  • GitHub Action is a new kid in town, and as developers, it is our responsibility to become early (debatable at this point) adopters
  • It has inherent support — I don’t have to share keys and secrets with another third party.
  • It has a vibrant ecosystem that will only get better with time. For instance, it is just 2 lines to set up a Linux machine with NodeJs.
  • As a corporate employee who used Jenkins, the setup process feels welcoming.
  • It supports parallelly running multiple jobs! You can let everything run parallel, or create dependencies. It promises to be quite versatile.

Although my current problem statement does not require much of the aforementioned features, it is easier to get my hands dirty with a simpler project.

How to get started?

GitHub Actions Home

The simplest way to start a workflow is to go to the “Actions” tab on your repository and click “set up a workflow yourself”. This will create a file calledmain.yml with some basic workflow. There is a structure to this YAML file to understand which we need to understand the components of GitHub actions.

The Components of GitHub Actions

Components of GitHub Actions
The Components of GitHub Actions, fromGitHub Docs

GitHub events (push, pull requests, and so on) trigger one or more jobs. Each job is run on a server that is called Runner instance (Virtual machines or Docker instances). Each job comprises one or more steps and each step performs some actions, ranging from echo on a bash terminal to a complicated action developed by the GitHub community that does some heavy lifting. To know more about the components check out theofficial documentation.

Plan of Action

Now that we are equipped with the understanding of GitHub workflows, it is time to break our task at hand into smaller workable units.

We will be starting with a Ubuntu server, and get the latest NodeJS environment setup. We will have to check out the code and install dependencies. Then we can build and finally publish to GitHub Pages.

And at what point do I want to do this? To answer that, I need to talk briefly about the workflow I follow. I have adevelopment branch on which I work. I make frequent commits and often I do not want to publish changes after each commit. Once I feel I have reached a stable point, I create a pull request on themaster branch (this repository existed before they renamedmaster tomain). So I want my action to kick in at a push onmaster. Note that if I trigger it on pull-request, it will be triggered when the request is created, and I want it to trigger after it is merged. If you have multiple developers, you’ll need to control who can commit onmaster/main directly.

Code of Action

name:build_deploy CIon:push:branches:[master]jobs:build:runs-on:ubuntu-lateststeps:-uses:actions/checkout@v2-name:Use Node.js ${{ matrix.node-version }}uses:actions/setup-node@v1with:node-version:'12.x'-name:Install dependenciesrun:yarn --frozen-lockfile-name:Buildrun:yarn run build-name:Deployuses:peaceiris/actions-gh-pages@v3.7.3with:github_token:${{ secrets.GITHUB_TOKEN }}publish_dir:./buildcname:<domain_name>enable_jekyll:falseuser_name:'github-actions[bot]'user_email:'github-actions[bot]@users.noreply.github.com'
Enter fullscreen modeExit fullscreen mode

Let us go over the steps.

  1. The action has a name!
  2. We run it on a push to master.
  3. The job uses a Ubuntu server
  4. Check out code
  5. Setup NodeJS environment. We can specify the version(s), and yarn comes preinstalled.
  6. We install the dependencies and ensure the lock file is not changed in the process.
  7. We create an optimized production build.
  8. We use an action from the GitHub community to push the changes to the specific branch. We can make customization by setting certain parameters. (Since I have a custom domain, I set that here withcname.)

And that is all. You are all set to harness the goodness of GitHub Actions!

The results

Apart from this, I have another action that runs the build job on every push todevelopment branch. This ensures I am not breaking anything that will prevent the build when I merge a pull request. Here is how it looks on my repository:

GitHub Actions in action.png
GitHub Actions in action, Niladri Roy

The failing build on my development branch helped me correct my mistake and once that started passing, I opened a pull request and merged it. This triggered the “build_deploy CI” workflow, deploying my changes to my website, all in about a minute and a half! How cool is that?


Originally published athttps://medium.com/ones-zeros/react-app-and-github-actions-96c9f5444963

Buy Me A Coffee

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Developer by the day, a student at night.Photographer on vacations, muser when it's quiet!
  • Location
    Bengaluru
  • Work
    Lead Software Engineer at Informatica
  • Joined

More fromNiladri

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp