Preface
In Part "teach you a GitHub code synchronization and Gitee" , we used GitHub GitHub Actions to solve the problem of automatic code synchronization Gitee, I translated this sort of GitHub Actions of official document , to introduce GitHub At the same time as Actions, it also explains the Actions code of the previous synchronous Gitee.
GitHub Actions
GitHub Actions is a continuous integration and continuous delivery platform that can automate construction, testing, and deployment. You can create a workflow, build and test each pull request or deploy the merged code to the production environment.
GitHub Actions can run a workflow when an event occurs in your code repository. For example, when someone creates an issue for your code repository, you can run a workflow to automatically add appropriate tags.
GitHub provides Linux, Windows, and macOS virtual machines to run your workflow. Of course, you can also customize the running environment.
GitHub Actions component
You can configure a GitHub Actions workflow (workflow ), which will be triggered when an event occurs in your warehouse, such as when a pull request or an issue is created.
Your workflow contains one or more tasks (jobs) , which can be executed in parallel or serially. Each task (jobs) will be on its own virtual machine runner (runner) , the task can have one or more steps (steps) , you can run a custom script or run a action (action ) , the so-called action (action) is a reusable extension to simplify your workflow.
Workflows
Workflow is a configurable automated program. To create a workflow, you need to define a YAML file. When your warehouse triggers an event, the workflow will run. Of course, you can trigger it manually or define a schedule.
A warehouse can create multiple workflows, each of which performs different steps. For example, one workflow is used to build and test pull requests, one is used to deploy your application, and the other is automatically added when someone creates a new issue. A label.
You can also refer to another workflow in one workflow, see " Workflow ".
Events
An event refers to a specific behavior that the warehouse triggers to run a workflow, such as creating a pull request, creating a new issue, or pushing a commit. You can also trigger a workflow using a schedule, or by requesting a REST API, or manually.
For a complete list of events, you can view " triggers the workflow".
Tasks (Jobs)
A task is a set of steps (steps) executed on the same runner. A step (steps) is either a shell script (script) or an action (action). The steps are executed sequentially and are independent of each other. Because each step is executed on the same runner, you can pass data from one step to another.
You can configure a task to depend on other tasks. By default, tasks have no dependencies and are executed in parallel. When a task requires another task, it will wait until the dependent task is completed before executing it.
Actions
Action is a custom application of the GitHub Actions platform, which will perform a complex but frequently repeated task. Using actions can reduce repetitive code. For example, an action can pull your git repository from GitHub and create a suitable tool chain for your build environment.
You can write your own actions, or find an already implemented action in the GitHub market.
Runners
A runner is a service that can run a workflow. Each runner only runs a single task at a time. GitHub provides Ubuntu Linux, Microsoft Windows and macOS runners, and each workflow runs in a newly created virtual machine. If you need a different operating system, you can customize the runner. Please check " custom runner ".
Create a workflow
GitHub Actions uses YAML syntax to define workflows. Each workflow is saved as a separate YAML file, the directory is .github/workflows
.
Now we create a sample workflow in the code repository. When the code is pushed, a series of commands will be automatically executed. In this example workflow, GitHub Actions will check out the submitted code, install dependencies, and run bats -v
:
- In your warehouse, create a
.github/workflows/
directory In the
.github/workflows/
directory, create a file namedlearn-github-actions.yml
and add the following code:name: learn-github-actions on: [push] jobs: check-bats-version: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: '14' - run: npm install -g bats - run: bats -v
- Submit these changes and push them to your GitHub repository.
Your new GitHub Actions workflow file will be installed in your repository, and the workflow will be executed automatically when someone submits the code. For the execution history of a task, see the "161c2fe378b1d4 view workflow activity ".
Understand the workflow file
To help you understand the YAML syntax, this section will explain each line of code in the example:
name: learn-github-actions
Optionally, the name of the workflow will appear in the Actions option bar of the GitHub repository.
on: [push]
Specify the trigger event of the workflow. In this example, the push
event is used. When someone submits a code modification or merges a pull request, the workflow will be triggered. Submit to each branch will be triggered, if you want to specify the branch, path, label, check " GitHub Actions Workflow Syntax "
jobs:
Group all tasks running in the learn-github-actions
check-bats-version:
A task named check-bats-version
is defined, and the child key defines the properties of the task.
runs-on: ubuntu-latest
The configuration task runs on the latest Ubuntu Linux runner.
steps:
check-bats-version
all the steps under the 061c2fe378b2f7 task into one group, and each nested item is an independent action or shell script.
- uses: actions/checkout@v2
uses
keyword specifies that this step runs the v2
version of the actions/checkout
This is an action that can check out the warehouse code to the runner, which allows you to run scripts or other actions to invade your code (such as build or test tools).
- uses: actions/setup-node@v2
with:
node-version: '14'
This step will use actions/setup-node@v2
action Nodejs install the specified version, which will be on your PATH
plus node
and npm
command.
- run: npm install -g bats
run
keyword tells the task to execute a command on the runner. In this example, you are using npm
install the bats
software test package.
- run: bats -v
Finally, you run the bats
command and pass in a parameter that can print the software version.
Visual workflow file
In this diagram, you can see the workflow file you just created and how these GitHub Actions components are organized. Each step will execute an independent action or script file. Tasks 1 and 2 are running commands, and tasks 3 and 4 are running script files. To find more pre-built actions, see " Find and Custom Action ".
View workflow activities
Once your workflow starts running, you can see a visual progress chart on GitHub to view the execution of each step.
- On GitHub.com, navigate to the main repository homepage
- Under your warehouse name, click
Actions
.
- On the left sidebar, click on the workflow you want to view
- In
Workflow runs
, click the name of the running record you want to view:
- In Jobs or in the visualization chart, click on the task you want to see:
- View the results of each step:
Code analysis of the previous article
Now let's look at the code of GitHub syncing Gitee, is it a lot clearer:
name: syncToGitee
on:
push:
branches:
- gh-pages
jobs:
repo-sync:
runs-on: ubuntu-latest
steps:
- name: Mirror the Github organization repos to Gitee.
uses: Yikun/hub-mirror-action@master
with:
src: 'github/mqyqingfeng'
dst: 'gitee/mqyqingfeng'
dst_key: ${{ secrets.GITEE_PRIVATE_KEY }}
dst_token: ${{ secrets.GITEE_TOKEN }}
static_list: "learn-typescript"
force_update: true
debug: true
In this example, we define a syncToGitee
, and specify the code to be submitted to the branch gh-pages
to trigger the workflow.
The next task is only called repo-sync
task, run ubuntu-latest
, under specific steps, only one named Mirror the GitHub organization repos to Gitee.
step, using Yikun/hub-mirror-action@master
action, and with
the contents are some of the parameters of the required actions.
Series of articles
Series article directory address: https://github.com/mqyqingfeng/Blog
WeChat: "mqyqingfeng", add me to the only reader group in Kongyu.
If there are mistakes or not rigorous, please correct me, thank you very much. If you like or have some inspiration, star is welcome, which is also an encouragement to the author.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。