Skip to main content

Streamlining React Deployment with GitHub Actions and FTP

· 5 min read
Arkin Eliezer Julijanto

Deploying a React application to a server via FTP (File Transfer Protocol) can be a manual and time-consuming process. However, with the power of GitHub Actions, you can automate this deployment workflow, saving you valuable time and effort. In this blog post, we'll explore how to leverage GitHub Actions to automate the deployment of a React application using FTP. Let's dive in!

Setting Up the GitHub Repository​

To get started, create a new repository for your React application on GitHub and clone it to your local development environment. If you already have an existing React project, ensure it's properly configured for deployment.

Configuring FTP Credentials​

Next, you'll need to acquire FTP credentials from your hosting provider or server administrator. Once you have the credentials, store them securely as secrets in your GitHub repository's settings.

  1. Go to your GitHub repository's page.
  2. Click on the "Settings" tab.
  3. In the left sidebar, click on "Secrets".
  4. Click on the "New repository secret" button.
  5. Enter FTP_USERNAME as the name and provide the corresponding FTP username as the value.
  6. Click on the "Add secret" button.
  7. Repeat the process for FTP_PASSWORD, providing the FTP password as the value.

These secrets will be used in the GitHub Actions workflow to authenticate and establish a connection with the FTP server.

Creating the Deployment Workflow​

Now it's time to create a deployment workflow file in your GitHub repository. This file will define the deployment workflow, including the necessary steps and configurations.

  1. In your local repository, create a new directory named .github/workflows.
  2. Inside the workflows directory, create a file named deploy.yml.
  3. Open the deploy.yml file in a text editor and add the following content:
name: Deploy to FTP

on:
push:
branches:
- main

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 14

- name: Install Dependencies
run: npm ci

- name: Build Application
run: npm run build

- name: FTP Deploy
uses: SamKirkland/FTP-Deploy-Action@3.1.1
with:
server: <ftp_server>
username: ${{ secrets.FTP_USERNAME }}
password: ${{ secrets.FTP_PASSWORD }}
server-dir: /public_html
local-dir: build

Let's go through the different sections of this workflow:

  • name: Specifies the name of the workflow, which will be displayed in the Actions tab of your GitHub repository.
  • on: Defines the events that will trigger the workflow. In this case, the workflow will be triggered on any push event to the main branch.
  • jobs: Contains one or more jobs that make up the workflow. In our case, we have a single job called deploy.
  • runs-on: Specifies the operating system on which the job will run. Here, we use ubuntu-latest.
  • steps: Contains the individual steps that will be executed as part of the job.
  • name: Provides a descriptive name for each step.
  • uses: Specifies an action that will be used for the step. In this example, we use the actions/checkout and actions/setup-node actions to set up the repository and Node.js environment, respectively.
  • run: Executes a shell command for the step. Here, we install the project dependencies using npm ci and build the React application using npm run build.
  • uses: Utilizes the SamKirkland/FTP-Deploy-Action action to deploy the built application to the FTP server.
  • with: Provides the necessary configuration options for the FTP deployment action. Make sure to replace <ftp_server> with the actual FTP server address.

Testing and Deploying the Application​

Before committing and pushing the changes to the repository, it's essential to test the deployment workflow locally. Make sure you have the required dependencies installed and run the deployment workflow locally using a tool like act.

Once you're confident with the workflow, commit and push the changes to your GitHub repository. The deployment workflow will be triggered automatically upon pushing to the main branch, and your React application will be deployed to the specified FTP server.

Monitoring and Troubleshooting​

To monitor the deployment process and identify potential issues, access and analyze the deployment logs generated by GitHub Actions. In case of common errors, consider the following troubleshooting steps:

  • Double-check FTP credentials, ensuring they are correctly set as secrets.
  • Verify the FTP server details, such as the server address and target directory.
  • Ensure your React application is correctly configured for production deployment.

Scaling and Advanced Configurations​

For more advanced scenarios, you can handle environment-specific configurations by using environment variables or specialized deployment scripts. Additionally, you can configure multiple FTP deployments or include additional steps specific to your use case within the deployment workflow.

Conclusion​

By harnessing the power of GitHub Actions, you can automate the deployment of your React application using FTP, streamlining the process and saving valuable time. This blog post has guided you through the setup and configuration of GitHub Actions, creating the deployment workflow, and handling common scenarios. Embrace automation and enjoy the benefits of seamless React deployment, allowing you to focus more on building exceptional web applications.