趣玩Github Action

在 GitHub Actions 的仓库中自动化、自定义和执行软件开发工作流程。 您可以发现、创建和共享操作以执行您喜欢的任何作业(包括 CI/CD),并将操作合并到完全自定义的工作流程中。

Action 能干啥?都能干,前段时间还有人挖矿

下面所有的用户名和仓库用 laowangzhangshan 代替

Actions secrets

一些不方便的隐私的东西可以放进去,然后用环境变量代替

https://github.com/laowang/zhangshan/settings/secrets/actions

# 推荐变量名
DOCKERHUB_PASSWORD 123456 ${{ secrets.DOCKERHUB_PASSWORD }}
DOCKERHUB_TOKEN 123456 ${{ secrets.DOCKERHUB_TOKEN }}
DOCKERHUB_USERNAME laowang ${{ secrets.DOCKERHUB_USERNAME }}
TOKEN_GITHUB 123456 ${{ secrets.TOKEN_GITHUB }}

Action.yml

这是一个简单的action文件

name: laowang de action

on:
push

jobs:
laowang_de_action:
name: laowang de action
runs-on: ubuntu-latest
steps:
-
name: Private Actions Checkout
uses: actions/checkout@v2.3.4
-
name: Docker Setup QEMU
uses: docker/setup-qemu-action@v1.2.0
run: echo "hello word"

on

on:
push:
branches:
- main # 当仓库分支 main 有更新的时候 运行action
repository_dispatch:
types: [laowang] # 当仓库接收到 laowang 信号的时候 运行action
schedule:
- cron: '0 0 * * MON' # 定时任务 运行action
# https://docs.github.com/cn/actions/learn-github-actions/workflow-syntax-for-github-actions

那么 laowang 是怎么发出的呢?

jobs:
laowang_fasong_tools:
runs-on: ubuntu-latest
steps:
-
name: laowang fasong tools
uses: peter-evans/repository-dispatch@v1
with:
token: ${{ secrets.TOKEN_GITHUB }} # github tocken
repository: ${{ secrets.DOCKERHUB_USERNAME }}/Code-Server-Update # 发送到那个仓库呢?
# 老王觉得自己的用户名是个密码,所以用 ${{ secrets.DOCKERHUB_USERNAME }} 代替
event-type: laowang # 给上面这个仓库发送 laowang 信号
client-payload: '{"ref": "${{ github.ref }}", "sha": "${{ github.sha }}"}'

jobs

jobs:
laowang_fasong_tools: # 任务的名称,建议使用下划线
runs-on: ubuntu-latest
steps: # 步骤

steps

jobs:
laowang_fasong_tools:
runs-on: ubuntu-latest
steps:
-
name: Private Actions Checkout
uses: actions/checkout@v2.3.4 # 这是 下载 当前仓库源码的 action
-
name: Docker Setup QEMU
uses: docker/setup-qemu-action@v1.2.0 # 这是 qemu 模拟的 action
-
name: Docker Setup Buildx
uses: docker/setup-buildx-action@v1.6.0 # 这是 docker buildx的 action
-
name: Docker Login
uses: docker/login-action@v1.10.0 # 这是 docker 登录的 action
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push Docker images # 这是 docekr 部署的 action
uses: docker/build-push-action@v2.7.0
with:
context: .
platforms: linux/arm64,linux/amd64 # 这里选择你要编译的系统架构
push: true
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/code-server:init # 这里设置镜像的名称,并推送到hub
file: .github/workflows/Dockerfile.init # 这里手动选择 dockerfile
cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/code-server:init.cache # 这里使用来自 hub 的缓存
cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/code-server:init.cache,mode=max # 这里将缓存写入 hub

看了这么多,有点累了,上点才艺吧

img

越看越累😂,来把英雄联盟手游,打完了继续

对了,万一我设置了几个任务,但是他们是同步进行的,会报错,必须一个一个来怎么办?

needs

jobs:
cha_hu_kou:
needs: [zhangshan, zhangshan_cunweihui]
name: zhangshan_xian
runs-on: ubuntu-latest

那如果我想运行自己的命令怎么办呢?

jobs:
kaigong:
needs: kaigong
name: Docker Build PHP56
runs-on: ubuntu-latest
steps:
-
name: dayin hello
# run: echo "hello word"
run: |
echo "hello word"
echo "word hello" # | 这样就能运行多条命令啦!

比较复杂

action.yml

jobs:
kaigong:
needs: kaigong
name: Docker Build PHP56
runs-on: ubuntu-latest
steps:
-
name: Code Server Download
# 如果需要编译多个系统,但是软件包只能手动选择,那就下载不同系统的软件包到不同文件夹
run: |
mkdir -p linux/arm64 linux/amd64
wget -O linux/amd64/code-server.rpm `curl https://api.github.com/repos/cdr/code-server/releases/latest | grep "browser_download_url" | cut -d '"' -f 4 | grep amd64.rpm` --no-cookie --no-check-certificate
wget -O linux/arm64/code-server.rpm `curl https://api.github.com/repos/cdr/code-server/releases/latest | grep "browser_download_url" | cut -d '"' -f 4 | grep arm64.rpm` --no-cookie --no-check-certificate
-
name: Docker Login
uses: docker/login-action@v1.10.0
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push Docker images
uses: docker/build-push-action@v2.7.0
with:
context: .
platforms: linux/arm64,linux/amd64
push: true
tags: |
laowang/code-server:latest
cache-from: type=gha
cache-to: type=gha,mode=max

dockerfile

FROM xrsec/php:latest
LABEL maintainer="xrsec"
LABEL mail="troy@zygd.site"

ARG TARGETPLATFORM # 定义这个变量,如果系统架构是arm 则 TARGETPLATFORM ==linux/arm64

# Copy File
COPY ${TARGETPLATFORM}/code-server.rpm /www/ # 上面我们创建并下载了软件包,所以可以直接复制

RUN rpm -ivh /www/code-server.rpm

还有一些复杂度挺高的,欢迎大家来我 github 参观 PHP_Docker

思考

怎么用 action 做到监控别的仓库更新自己也运行?除了发送信号还有别的方案吗?

XRSec has the right to modify and interpret this article. If you want to reprint or disseminate this article, you must ensure the integrity of this article, including all contents such as copyright notice. Without the permission of the author, the content of this article shall not be modified or increased or decreased arbitrarily, and it shall not be used for commercial purposes in any way