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、相关文章


相关文章
|
7月前
|
存储 运维 安全
Github Action:让静态网站实现定时发布
本文探讨了静态网站实现定时发布的解决方案,针对静态博客缺乏原生定时发布功能的问题,作者基于Zola工具构建的静态网站,最终选择通过GitHub Action实现定时发布。文章对比了几种实现方式,包括人力、自建服务及平台原生功能等,指出最理想的是利用平台能力但多数平台缺乏该功能。文中详细介绍了GitHub Action方案的原理、部署过程和使用流程,并分享了高级配置如推送通知。同时分析了方案的局限性,如时间颗粒度粗、设置繁琐等,并提出改进建议。总结中展望了未来优化方向,旨在提升静态网站维护体验。
103 0
|
12月前
|
JavaScript API 开发工具
使用GitHub Actions自动发布electron多端安装程序
使用GitHub Actions自动发布electron多端安装程序
210 8
|
12月前
|
对象存储
一个通过 GitHub Action 将 GitHub 仓库与阿里云 OSS 完全同步的脚本
一种将 GitHub 仓库完全同步到阿里云 OSS 的方法。
利用 GitHub Actions 自动化你的软件开发流程
在现代软件开发中,自动化是提升效率与质量的关键。GitHub Actions 作为 GitHub 的强大自动化工具,允许你在仓库中自动执行多种任务,如测试、打包、部署代码及自动合并 Pull Requests。本文介绍了 GitHub Actions 的核心概念、设置方法及其实用示例,帮助你快速上手并优化开发流程。通过 YAML 文件定义的工作流程可显著提高工作效率和代码质量。
利用 GitHub Actions 自动化你的软件开发流程
GitHub Actions 是由 GitHub 提供的自动化工具,可让你在仓库中触发和执行自动化工作流程,如自动运行测试和部署应用。其核心概念包括工作流程(定义在 YAML 文件中的一系列自动化步骤)、作业和步骤。本文将指导你如何设置和使用 GitHub Actions,并提供实用的自动化示例,帮助你提高开发效率和代码质量。通过简单的配置文件,你可以实现自动运行测试、部署应用甚至自动合并 Pull Requests。
利用 GitHub Actions 自动化你的软件开发流程
在快速发展的软件开发环境中,自动化对于提升效率与质量至关重要。GitHub Actions 作为一款强大的工具,能帮助开发者实现从自动运行测试到部署应用等工作的自动化。本文详细介绍了 GitHub Actions 的核心概念、设置方法及实际应用示例,如自动测试、部署和合并 Pull Requests,助力提升开发流程的自动化水平。
利用 GitHub Actions 自动化你的软件开发流程
GitHub Actions 是 GitHub 提供的自动化工具,可在仓库中触发和执行工作流程,包括自动运行测试、部署应用等。其核心概念包括工作流程(Workflow)、作业(Job)和步骤(Step),均定义在 YAML 文件中。本文将指导你如何设置和使用 GitHub Actions,并提供自动运行测试、部署应用及合并 Pull Requests 的示例,帮助提高开发效率和代码质量。
|
Java Spring 传感器
AI 浪潮席卷,Spring 框架配置文件管理与环境感知,为软件稳定护航,你还在等什么?
【8月更文挑战第31天】在软件开发中,配置文件管理至关重要。Spring框架提供强大支持,便于应对不同环境需求,如电商项目的开发、测试与生产环境。它支持多种格式的配置文件(如properties和YAML),并能根据环境加载不同配置,如数据库连接信息。通过`@Profile`注解可指定特定环境下的配置生效,同时支持通过命令行参数或环境变量覆盖配置值,确保应用稳定性和可靠性。
169 0
|
Devops 持续交付 开发者
.NET自动化之旅:是Azure DevOps还是GitHub Actions能够打造完美的CI/CD流水线?
【8月更文挑战第28天】在现代软件开发中,持续集成(CI)与持续部署(CD)是提升代码质量和加速交付的关键实践。对于 .NET 应用,Azure DevOps 和 GitHub Actions 等工具可高效构建 CI/CD 流水线,提升开发效率并确保软件稳定可靠。Azure DevOps 提供一站式全流程管理,支持 YAML 定义的自动化构建、测试和部署;GitHub Actions 则以简洁灵活著称,适用于 .NET 项目的自动化流程。选择合适的工具可显著提高开发效率并确保高质量标准。
112 0
|
Linux C++ Docker
【Azure Developer】在Github Action中使用Azure/functions-container-action@v1配置Function App并成功部署Function Image
【Azure Developer】在Github Action中使用Azure/functions-container-action@v1配置Function App并成功部署Function Image
104 0