Introduction to the
GitHub Actions is GitHub’s continuous integration service. It was launched in October 2018 and is now available for use in the official environment.
What is continuous integration?
If you think about how your code goes from development to live, it usually goes through
Code development -> Code submission -> server compile, test, package -> Server deployment (docker for large-scale deployment)
The entire process, with the exception of code development and code submission, is developer dependent.
After the server compilation, testing, packaging and deployment are repetitive work, can be replaced by procedures, there is no need to use manual operation, and if you want to deploy 100 servers, all by hand, operation and maintenance personnel will be tired and vomiting blood
To solve these problems, savvy programmers invented continuous integration!
What does continuous integration do?
The goal of continuous integration is to automate all server compilation, testing, packaging, and deployment after code submission.
The previous general scheme is shown in the figure below, where the dashed box is the continuous integration part
It can be seen that the whole process is very smooth, but Jenkins needs to be manually built by developers, and it takes up some resources of the server, so there are some troubles
Is there a better plan? That’s GitHub Actions for today!
GitHub Actions
How to find Actions
GitHub Actions means that you submit your code to GitHub and GitHub directly provides the server to help you build, test, and package your code. You don’t need to manually build any continuous integration tools or use your own server resources to do these things
After that, each action can be interpreted as an action, or actions added together
Official offers the actions in the market, can choose according to their own requirements, https://github.com/marketplace?type=actions
How to write Actions
The GitHub Actions configuration file is stored in the.github/workflows directory of the repository as a Yaml file named according to your needs, such as main.yml or develop.yml
The file configuration items may refer to https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions official documentation to write, here are some common options
- Name: Name Indicates the name of the current action
- On: On indicates the trigger condition, such as when the code pushes to the master branch
on:
push:
branches:
- master
Copy the code
- Jobs: Jobs represents the actual work that needs to be performed. For example, in the code below, the build work is performed using Ubuntu using the appleboy/ ssh-Action repository’s capabilities
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: executing remote ssh commands using password uses: appleboy/ssh-action@master Copy the code
Example: Login server packaging
There is a React front-end application that can automatically log in to the server every time the code is pushed to the master branch, pull the latest code and put it online.
First of all, it involves the SSH login problem, so find to a warehouse, https://github.com/appleboy/ssh-action, can automatically simulate the SSH login, established in accordance with the documents. Making/workflows/main yml file
name: remote ssh command
on:
push:
branches:
- master
jobs: build: name: Build runs-on: ubuntu-latest steps: - name: executing remote ssh commands using password uses: appleboy/ssh-action@master with: host: The ${{ secrets.HOST }} username: The ${{ secrets.USERNAME }} password: The ${{ secrets.PASSWORD }} command_timeout: 5m script: | cd ~/test git pull origin master yarn yarn build Copy the code
The previous content in the configuration file is easy to understand, from the beginning of the part, involves the login of personal server information, need to use the account and password, so if the account password in plain text on GitHub, it will be very insecure, GitHub will not allow such a thing, so there are secrets configuration
Select Settings ->secrets and add secrets in the file, where the value will not be leaked
The HOST value is the IP address, USERNAME is the USERNAME, and PASSWORD is the login PASSWORD
Such as SSH [email protected]The HOST = 10.20.0.1USERNAME = root
PASSWORD = ****** (Server login PASSWORD)Copy the code
According to the configuration file, instructions in the script are executed consecutively after login
script: |
cd~ /test
git pull origin master
yarn build
Copy the code
The idea here is to go to the code directory, pull the latest code, and package it (with the code directory, Git, Node.js, yarn, etc., ready in advance) so that it can be done with just a YamL configuration file and GitHub Actions
summary
GitHub Actions is a great combination of application versioning and continuous integration for development and deployment.
Beyond the examples above, there are many more features worth exploring, so I suggest you try them out and give your code the wings of continuous integration!
This article is formatted using MDNICE
– END –