preface
In the previous post, we used GitHub Actions to solve the problem of GitHub code automatically synchronizing With Gitee. In this post, I translated the official document of GitHub Actions. I will introduce you to GitHub Actions and explain the Actions code for synchronizing Gitee from the previous article.
GitHub Actions
GitHub Actions is a Continuous integration and Continuous delivery platform that automates build, test, and deployment. You can create workflows, build and test each pull request, or deploy the combined code into production.
GitHub Actions allows you to run a workflow when an event occurs in your repository. For example, when someone creates a new issue for your repository, you can run a workflow to automatically add the appropriate tag.
GitHub provides Linux, Windows, and macOS virtual machines to run your workflows, but you can also customize the environment.
Making Actions component
You can configure a GitHub Actions workflow that is triggered when an event occurs in your repository, such as when a pull Request or issue is created.
Your workflow consists of one or more tasks (Jobs) that can be executed in parallel or sequentially. Each job runs on its own virtual machine runner. A job can have one or more steps, can run a custom script, or can run an action. An Action is a reusable extension that simplifies your workflow.
Workflows
Workflow is a configurable, automated process. To create a workflow, you need to define a YAML file that will run when your repository triggers an event, either manually or by defining a schedule.
A repository can create multiple workflows, each performing a different step, for example, one for building and testing a pull request, one for deploying your application, and one for automatically adding a label when someone creates an issue.
You can also reference another workflow within one workflow to see “Reusable workflow”.
Events
Events are actions that the repository triggers to run a workflow, such as creating a pull Request, creating an issue, or pushing a COMMIT. You can also trigger a workflow using a schedule, by requesting a REST API, or manually.
For a complete list of events, see Events that Trigger workflow.
Jobs
A task is a set of steps that are executed on the same runner. A step is either a shell script or an action. The steps are executed sequentially and independently of each other. Because each step is executed on the same runner, you can pass data from one step to another.
You can configure one task to depend on other tasks. By default, tasks are executed in parallel without dependencies. When a task needs another task, it waits until the dependent task completes.
Actions
Actions is a custom application of the GitHub Actions platform that performs a complex but frequently repeated job. Use actions to reduce repetitive code. For example, an action can pull your Git repository from GitHub, create the appropriate toolchain for your build environment, etc.
You can write your own moves, or find already implemented moves in the GitHub marketplace.
Runners
A runner is a service that can run a workflow. Each runner runs a single task at a time. GitHub offers Ubuntu Linux, Microsoft Windows and macOS runners, and each workflow runs in a separate, newly created virtual machine. If you need a different operating system, you can customize the runner. See “Custom Runner.”
Create a workflow
GitHub Actions uses YAML syntax to define workflows. Each workflow is saved as a separate YAML file under the.github/workflows directory.
Now let’s create a sample workflow in the code repository that automatically executes a series of commands when the code is pushed. In this sample workflow, GitHub Actions checks out the submitted code, installs dependencies, and runs BATS-V:
- In your repository, create one
.github/workflows/
directory - in
.github/workflows/
Directory to create a file namedlearn-github-actions.yml
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
Copy the code
- Commit these changes and push them to your GitHub repository.
Your new GitHub Actions workflow file will be installed in your repository and automatically executed when someone submits code. For the execution history of a task, see the “View Workflow Activities” section.
Understanding workflow files
To help you understand YAML syntax, this section explains each line of code in the example:
name: learn-github-actions
Copy the code
Optionally, the name of the workflow will appear in the GitHub repository’s Actions bar.
on: [push]
Copy the code
Specifies the triggering event for the workflow. In this example, the push event is used, and the workflow is triggered when someone submits a code change or merges a pull request. Submission to each branch will be triggered. If you want to specify branches, paths, and tags, check the GitHub Actions workflow syntax.
jobs:
Copy the code
Group together all the tasks running in the Learn-Github – Actions workflow.
check-bats-version:
Copy the code
Defines a task named check-BATs-version whose attributes are defined by the child key.
runs-on: ubuntu-latest
Copy the code
Configuration tasks run on the latest Ubuntu Linux runtime.
steps:
Copy the code
Group all the steps under the check-BATs-version task, and each nested entry is a separate action or shell script.
- uses: actions/checkout@v2
Copy the code
The USES keyword specifies that this step runs the v2 larger version of the Actions /checkout action. This is an action that checks out repository code to the runtime, allowing you to run scripts or other actions to hack into your code (such as build or test tools).
- uses: actions/setup-node@v2
with:
node-version: '14'
Copy the code
This step installs the specified version of Nodejs using the Actions /setup-node@v2 action, which adds node and NPM commands to your PATH.
- run: npm install -g bats
Copy the code
The run keyword tells the task to execute a command on the runner. In this example, you are installing the BATS software test package using NPM.
- run: bats -v
Copy the code
Finally, you run the BATS command, passing in a parameter that prints the version of the software.
Visual workflow file
In this diagram, you can see the workflow file you just created and how the GitHub Actions components are organized. Each step executes a separate action or script file. Tasks 1 and 2 are running commands, tasks 3 and 4 are running script files. To find more pre-built actions, see Find and Customize Actions.
View workflow activities
Once your workflow is up and running, you can see a visual progress chart on GitHub to see how each step is being executed.
- At GitHub.com, navigate to the warehouse main page
- Under your warehouse name, click
Actions
.
- In sidebar on the left, click the workflow you want to view
- in
Workflow runs
Click the name of the run record you want to view:
- In Jobs or in the visual diagram, click on the task you want to see:
- View the results of each step:
Last article code parsing
Now let’s take a look at GitHub syncing Gitee’s code and see if it’s much 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: The ${{ secrets.GITEE_PRIVATE_KEY }}
dst_token: The ${{ secrets.GITEE_TOKEN }}
static_list: "learn-typescript"
force_update: true
debug: true
Copy the code
In this example, we define a workflow named syncToGitee, specifying that the workflow is triggered only when the code is submitted to the branch gh-Pages.
There is only one task named repo-sync, which runs in Ubuntu-latest, and only one task named Mirror the GitHub organization repos to Gitee. “, using the Yikun/hub-mirror-action@master action, and the contents in with are the parameters needed for that action.
series
Catalogue address: github.com/mqyqingfeng…
Wechat: “MQyqingfeng”, add me Into Hu Yu’s only readership group.
If there is any mistake or not precise place, please be sure to give correction, thank you very much. If you like or are inspired by it, welcome star and encourage the author.