Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to master Github Actions quickly

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to master Github Actions quickly". In daily operation, I believe many people have doubts about how to master Github Actions quickly. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to master Github Actions quickly!" Next, please follow the editor to study!

Quick start

Enter the personal warehouse on github and find the tab of Actions

Actions

If you are a front-end project, you can use the Node.js template and click Set up this workflow

Actions Setup

A file .GitHub / workflows/nodejs.yaml is generated with the following modifications

Name: Git Action Test on: [push] jobs: build: runs-on: ubuntu-latest steps:-uses: actions/checkout@v1-name: Use Node.js 10.x uses: actions/setup-node@v1 with: node-version: 10.x-name: npm install, build run: | npm install npm run build-if-present env: CI: true

Perform the git push operation, and you can see the execution result on the Actions tab of github.

Actions Start

Terminology

Runner: the build server assigned by github to execute CI/CD (you can also build your own runner)

Workflow: workflow of CI/CD

Job: tasks, such as build, test, and deploy. Each workflow consists of multiple job

Step: each job consists of multiple step

Configuration

Refer to the official document: Workflow syntax for GitHub Actions [1]

The following are commonly used configurations

On

The event when the CI/CD is triggered. If you need the automatic deployment of upload code, you should choose on: push

On: push

For more events, please refer to the official document Events that trigger workflows [2]

In addition, we can use issues events to do a lot of things, such as email notifications

If it is an Issue blog, you can automatically generate a URL blog by listening to issues.opened events and calling github api.

On: issues: types: [opened, edited, milestoned]

Jobs

A CI/CD workflow consists of many jobs. For example, the most typical job is lint,test,build or deploy.

Jobs: test: build: deploy:

All job are parallel, but there are often dependencies: "you have to pass the test before you can deploy again."

Jobs..needs

Needs determines the dependency of job

Jobs: test: deploy: needs: test

Jobs..runs-on

When you use github actions, github will generously assign you a well-configured server as a build server to run your workflow

2-core CPU

7 GB of RAM memory

14 GB of SSD disk space

Yeah, better than my own server.

If you use runs-on to specify the server system, you should use ubuntu-latest most often.

Runs-on: ubuntu-latest runs-on: windows-latest

Jobs..steps

That is, a series of actions for a task, such as installing dependencies to deploy a front end, compiling code, and so on.

Jobs..steps.name

Specify a name for step, which will be displayed in the console of github action

Jobs..steps.run

Commands that the step needs to execute in shell

# single line-name: Install Dependencies run: npm install # multiple lines-name: Install and Build run: | npm install npm run build

Jobs..steps.uses

Choosing an action can be understood as several steps.run, which is conducive to code reuse. This is also the most important function of github action.

If you want to find a good github action, you can browse and search it at github marketplace.

This is an example of an action that installs node:10 in the current operating system

-name: use Node.js 10.x uses: actions/setup-node@v1 with: node-version: 10.x

Of course, how can there be no docker here? you can also use docker in actions. If you don't know much about docker, you can read my article: getting started with docker [3]

Here is an example of npm install

Jobs: build: steps:-name: Install uses: docker://node:alpine with: args: npm install

Secret and context

How to access sensitive data on github action? Such as how to maintain a password when logging in using ssh.

We can click Settings-> Secrets to set secret on github repo.

Actions Secrect

-name: setup aliyun oss uses: manyuanrong/setup-ossutil@master with: endpoint: oss-cn-beijing.aliyuncs.com access-key-id: ${secrets.OSS_KEY_ID}} access-key-secret: ${{secrets.OSS_KEY_SECRET}}

The secret here is a context that describes the contextual information in a workflow of CI/CD, expressed using the ${expression} syntax. Besides secret, there are also

Github: information about workflow, such as github.sha, can get the current commit SHA, and we can use it to enter the version number for sentry or docker image

Env: environment variabl

Job: information about the current execution of job, such as job.status indicates the execution status of the current job

Matrix: describes some build information, such as node and os version number

For more information on context, please refer to the official document Contexts and expression syntax for GitHub Actions [4].

Practice 1: automatic deployment of blog

My blog is currently hosted on Aliyun OSS, and the following action.yaml describes the process of automatic deployment. Please refer to my configuration shfshanyue/blog [5]

"for more information about hosting OSS in Aliyun, please see hosting your front-end application in Aliyun OSS [6]."

Name: deploy to aliyun oss on: [push] jobs: build: runs-on: ubuntu-latest steps: # cut into runner-uses: actions/checkout@v1 with: submodules: true # download git submodule-uses: srt32/git-actions@v0.0.3 with: args: git submodule update-- init-- recursive # use node:10- Name: use Node.js 10.x uses: actions/setup-node@v1 with: node-version: 10.x # npm install-name: npm install and build run: | npm install npm run build env: CI: true # set the id/secret of Aliyun OSS Store it in github's secrets-name: setup aliyun oss uses: manyuanrong/setup-ossutil@master with: endpoint: oss-cn-beijing.aliyuncs.com access-key-id: ${secrets.OSS_KEY_ID}} access-key-secret: ${{secrets.OSS_KEY_SECRET}}-name: cp files to aliyun run: ossutil cp-rf .vuepress / dist oss://shanyue-blog/

Deployment successful

Deployment successful

Practice 2: automatic generation of Github Readme

In August 2020, the month I resigned naked, Github launched the personal home page function to create a Repo of the same name for the Github user name, where Readme.md is his own home page. For example, I create a shfshanyue/shfshanyue repository, which can create a personal home page through Readme.md.

At this time, a variety of Github Actions emerged to customize the personal home page, the general idea is as follows:

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Github API acquires its own Github dynamics and writes Readme in the specified format

Github Actions auto-commit code

Actions of excellent Github personal home page [7]

One of the waka-readme-stats [8] can make the following beautiful personal home page

? I'm Most Productive on Sundays Monday 50 commits ███░░ 13.19% Tuesday 85 commits █░ 22.43% Wednesday 56 commits ███░░ 14.78% Thursday 44 commits ███░░ 11.61% Friday 28 commits █░░░░ 7.39% Saturday 30 commits ██░░░ 7.92% Sunday 86 commits ██ ███░ 22.69%

Practice 3: script server for scheduled tasks

"this is simply fleece!"

First, we present a tool that uses Github Actions: bilibili automatic check-in tool [9].

Automatically run tasks online every day through GitHub Actions: automatically log in, watch, share and coin videos every day, get full experience of daily tasks, easily upgrade Level 6, automatically receive major membership rights and benefits, and automatically recharge yourself at the end of the month.

At this point, the study of "how to master Github Actions quickly" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report