介绍:
连续集成和连续部署(CI/CD)已成为现代软件开发的关键方面。通过自动化构建,测试和部署流程,开发人员可以节省时间,减少错误并提供高质量的应用程序。 GitHub Action是GitHub提供的强大功能,使开发人员可以轻松自动化这些过程。在本文中,我们将探讨如何在Ubuntu环境上为React和Node.js应用程序的CI/CD设置GitHub操作。
先决条件:
潜入设置GitHub动作之前,请确保您有以下内容:
- 带有react或node.js应用程序存储库的github帐户。
- 对git和github工作流的基本理解。
- 基于Ubuntu的机器或用于运行CI/CD管道的虚拟机。
设置环境:
- 首先在Ubuntu机器上创建一个新目录,以存储CI/CD管道的必要文件。
- 安装node.js和npm(节点软件包管理器)如果尚未安装。您可以通过运行以下命令来执行此操作:
sudo apt update
sudo apt install nodejs npm
配置github操作:
- 导航到您的github存储库,然后单击“操作”选项卡。
- 单击“自己设置工作流”按钮以创建一个新的工作流文件。
- 选择该文件的名称,例如CI-CD.YML,并以下更新内容:
# This workflow is a Continuous Integration/Continuous Deployment (CI/CD) pipeline for deploying a Node.js backend application
# to a remote ubuntu server.
#
# Here are some comments to make it easier for everyone to use:
#
# 1. The workflow uses environment variables to set the application name, component name, and release path.
# Ensure that these are set correctly according to your application's configuration.
#
# 2. The workflow runs on a push to the main branch and can be manually triggered with inputs to specify the target environment.
#
# 3. It's important to note that this workflow uses several secrets that need to be set up
# in order for it to work properly. You will need to create the following secrets in your repository settings:
#
# 3.1. REMOTE_SERVER: the IP address or domain name of the remote server you are deploying to.
# 3.2. REMOTE_USER: the username you use to SSH into the remote server (root user is recommended).
# 3.3. REMOTE_KEY: the SSH private key you use to authenticate with the remote server.
# 3.4. ENV_FILE: the contents of your application's .env file. This should be a single-line string with each
# environment variable separated by spaces.
# 3.5. NGINX_CONFIG: the contents of your NGINX configuration file. This should be a multi-line string with each line
# separated by a newline character.
#
# Overall, this workflow provides a good starting point for deploying a Node.js backend application to a remote server using GitHub Actions.
# However, it should be customized according to your application's specific requirements.
name: 'CI/CD'
env:
APP_NAME: 'node'
COMPONENT_NAME: 'backend'
RELEASE_PATH: /root/node/backend
on:
push:
branches:
- main
workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
steps:
# The first step checks out the code from the repository.
- name: Checkout code
uses: actions/checkout@v2
# The second step sets up Node.js with version 16.
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 16
# The third step transfers the code to the remote server using the Secure Copy (SCP) protocol.
# The source directory is set to .,!./uploads, which excludes the uploads directory from being transferred.
# Ensure that this is set correctly according to your application's file structure.
- name: Transfer files to server
uses: appleboy/scp-action@master
with:
host: ${{ secrets.REMOTE_SERVER }}
username: ${{ secrets.REMOTE_USER }}
key: ${{ secrets.REMOTE_KEY }}
rm: true
source: '.,!./uploads'
target: ${{ env.RELEASE_PATH }}/build
# The fourth step creates shared folders for the uploads directory and the node_modules directory and creates symbolic links
# to these folders in the current release directory. This helps to ensure that the uploads and node_modules directories
# persist across releases and are not overwritten.
- name: Create shared folders
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_SERVER }}
username: ${{ secrets.REMOTE_USER }}
key: ${{ secrets.REMOTE_KEY }}
script: |
cd ${{ env.RELEASE_PATH }}
mkdir -p shared/uploads
mkdir -p shared/node_modules
ln -sfn ${{ env.RELEASE_PATH }}/shared/uploads ${{ env.RELEASE_PATH }}/build/uploads
ln -sfn ${{ env.RELEASE_PATH }}/shared/node_modules ${{ env.RELEASE_PATH }}/build/node_modules
# The fifth step copies configuration files to the remote server, including the .env file and an NGINX configuration file.
# The NGINX configuration file is used to proxy requests to the Node.js backend application.
# Ensure that this is set correctly according to your application and server's configuration.
- name: Copy config files
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_SERVER }}
username: ${{ secrets.REMOTE_USER }}
key: ${{ secrets.REMOTE_KEY }}
script: |
echo '${{ secrets.ENV_FILE }}' > ${{ env.RELEASE_PATH }}/build/.env
echo '${{ vars.NGINX_CONFIG }}' > ${{ env.RELEASE_PATH }}/build/nginx.conf
sudo systemctl restart nginx
# The sixth step installs dependencies, builds the application.
- name: Install dependencies
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_SERVER }}
username: ${{ secrets.REMOTE_USER }}
key: ${{ secrets.REMOTE_KEY }}
script: |
cd ${{ env.RELEASE_PATH }}/build
yarn install
yarn run build
# The seventh step starts it with PM2.
# PM2 is a process manager for Node.js applications that helps to ensure that the application runs continuously
# and can be easily managed. Ensure that this is set correctly according to your application's dependencies and build process.
- name: Start PM2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_SERVER }}
username: ${{ secrets.REMOTE_USER }}
key: ${{ secrets.REMOTE_KEY }}
script: |
cd ${{ env.RELEASE_PATH }}/build
pm2 delete -s ${{ env.APP_NAME }} || :
pm2 start dist/index.js --name "${{ env.APP_NAME }}"
- 在上面的配置中,我们设置了node.js/express的示例工作流程,以触发推送到
main
分支。 -
build
作业查看存储库,设置node.js版本16,将文件传输到服务器,安装依赖项,创建构建并重新启动PM2服务。 - 您可以在将应用程序部署应用程序部署到Heroku,Netlify或AWS等步骤中添加/修改部署命令。
运行CI/CD管道:
- 提交并将
ci-cd.yml
文件推到您的github存储库中。 - github动作将自动检测新的工作流程并启动CI/CD管道。
- 您可以在存储库的“操作”选项卡中监视管道的进度。
- 如果在构建或测试阶段发生任何错误,GitHub操作将提供详细的日志以帮助您识别和解决问题。
结论:
github操作提供了一种自动化ci/cd管道的方便方法,用于react and node.js应用程序。通过利用工作流的力量,您可以节省时间和精力,同时保持代码库的质量。在本文中,我们探讨了如何在Ubuntu环境上使用GITHUB操作设置基本的CI/CD工作流程。随意自定义工作流程以满足您的特定需求,并利用GitHub动作的丰富生态系统,以进一步增强您的开发过程。
快乐自动化!