GitHub Actions:从使用action操作到自定义action操作

简介: GitHub Actions:从使用action操作到自定义action操作

利用GitHub Actions可以自动完成很多流水线任务,本文将介绍从使用action操作到自定义action操作

目录

1、使用action操作

文档

.github/workflows/github-actions-demo.yml

name: GitHub Actions Demo
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
on: [push]
jobs:
  Explore-GitHub-Actions:
    runs-on: ubuntu-latest
    steps:
      - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
      - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
      - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
      - name: Check out repository code
        uses: actions/checkout@v3
      - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
      - run: echo "🖥️ The workflow is now ready to test your code on the runner."
      - name: List files in the repository
        run: |
          ls ${{ github.workspace }}
      - run: echo "🍏 This job's status is ${{ job.status }}."

定义了一个action操作,当代码推送的时候,触发运行,打印了一些环境信息

2、自定义action操作

文档

项目结构

$ tree -I node_modules
.
├── README.md
├── action.yml
├── dist
│   ├── index.js
│   └── licenses.txt
├── index.js
├── package-lock.json
├── package.json
└── pnpm-lock.yaml

1、准备好Node.js环境

$ node -v
v16.14.0

2、初始化npm项目

npm init -y

3、安装依赖

pnpm install @actions/core @actions/github @vercel/ncc

package.json

{
  "name": "github-action",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "build": "ncc build index.js --license licenses.txt"
  },
  "dependencies": {
    "@actions/core": "^1.10.0",
    "@actions/github": "^5.1.1",
    "@vercel/ncc": "^0.36.1"
  }
}

4、元数据文件 action.yml

name: 'Hello World'
description: 'Greet someone and record the time'
inputs:
  who-to-greet:  # id of input
    description: 'Who to greet'
    required: true
    default: 'World'
outputs:
  time: # id of output
    description: 'The time we greeted you'
runs:
  using: 'node16'
  main: 'dist/index.js'

以上代码,定义了元数据,指定了出入参数和输出参数,指定了运行入口,相当于一下两个步骤

(1)声明action接口

// ./dist/index.js
function {time} action ({who-to-greet}) {
    // 执行操作
}

(2)执行action操作

$ node16 ./dist/index.js

5、操作代码 index.js

上面定义接口的实现

const core = require("@actions/core");
const github = require("@actions/github");
try {
  // `who-to-greet` input defined in action metadata file
  const nameToGreet = core.getInput("who-to-greet");
  console.log(`Hello ${nameToGreet}!`);
  const time = new Date().toTimeString();
  core.setOutput("time", time);
  // Get the JSON webhook payload for the event that triggered the workflow
  const payload = JSON.stringify(github.context.payload, undefined, 2);
  console.log(`The event payload: ${payload}`);
} catch (error) {
  core.setFailed(error.message);
}

6、代码发布

提交到本地仓库后,添加标签,发布到github

# 打包编译
npm run build
# 提交代码 
git add .
git commit -m 'add code'
# 打标签发布
git tag v1.1
git push --tag

7、测试使用

.github/workflows/main.yml

on: [push]
jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to say hello
    steps:
      - name: Hello world action step
        id: hello
        uses: mouday/github-action@v1.1
        with:
          who-to-greet: 'Mona the Octocat'
      # Use the output from the `hello` step
      - name: Get the output time
        run: echo "The time was ${{ steps.hello.outputs.time }}"

示例代码仓库地址:https://github.com/mouday/github-action

8、action模板

可以基于github提供的模板快速开发

可以参考他提供的项目模板进行开发

$ tree
.
├── CODEOWNERS
├── LICENSE
├── README.md
├── __tests__
│   └── main.test.ts
├── action.yml
├── dist
│   ├── index.js
│   ├── index.js.map
│   ├── licenses.txt
│   └── sourcemap-register.js
├── jest.config.js
├── package-lock.json
├── package.json
├── src
│   ├── main.ts
│   └── wait.ts
└── tsconfig.json

9、问题

问题1:Error: Cannot find module ‘@actions/core’

需要打包发布,不然会提示找不到模块

3、相关文章


相关文章
|
6月前
|
监控 安全 机器人
通过GitHub Actions给微信公众测试号和钉钉群定时推送消息(Python)
通过GitHub Actions给微信公众测试号和钉钉群定时推送消息(Python)
97 0
|
5月前
|
开发工具 git
小白必须要会的Github操作 确定不进来看看?
小白必须要会的Github操作 确定不进来看看?
|
8月前
|
存储 Ubuntu 持续交付
GitHub Actions 指南(上)
GitHub Actions 指南
108 0
|
10月前
|
程序员 网络安全 开发工具
git介绍,安装(手把手),命令与使用(包含IDEA操作,github,gitee的万字图文详述)(三)
git介绍,安装(手把手),命令与使用(包含IDEA操作,github,gitee的万字图文详述)
161 0
git介绍,安装(手把手),命令与使用(包含IDEA操作,github,gitee的万字图文详述)(三)
|
2天前
|
存储 JavaScript 测试技术
github action
github action
17 0
|
2天前
|
存储 安全 Serverless
用 Github Actions 自动部署阿里云函数计算 FC
介绍了如何配置阿里云函数计算(FC)与GitHub Actions集成以实现自动部署。首先在阿里云创建函数,然后在项目根目录创建`s.yaml`文件配置Serverless Devs。接着在GitHub仓库中设置 Secrets 存储阿里云账号信息,并创建名为`aliyun-fc-deploy.yaml`的工作流文件来定义GitHub Actions。当代码推送到`master`分支时,Actions会自动部署到函数计算。最后,成功配置后,提交代码会触发自动部署,并可在GitHub Actions和阿里云控制台查看部署状态。
443 3
|
8月前
|
Linux 持续交付 iOS开发
GitHub Actions 指南(下)
GitHub Actions 指南
48 0
|
2天前
【操作宝典】GitHub玩转攻略:GitHub与GitHub Desktop详细教程大揭秘!
【操作宝典】GitHub玩转攻略:GitHub与GitHub Desktop详细教程大揭秘!
52 0
|
2天前
|
JavaScript Ubuntu 网络安全
使用github actions,将私有仓库的构建文件发布到另一个公开仓库,并同步到gitee
使用github actions,将私有仓库的构建文件发布到另一个公开仓库,并同步到gitee
122 0
|
2天前
|
供应链 安全 jenkins