前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >趣玩Github Action

趣玩Github Action

作者头像
XRSec
发布2022-03-15 15:59:35
6110
发布2022-03-15 15:59:35
举报
文章被收录于专栏:XRSec.Blog

趣玩Github Action

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

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

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

Actions secrets

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

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

代码语言:javascript
复制
# 推荐变量名
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文件

代码语言:javascript
复制
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

代码语言:javascript
复制
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 是怎么发出的呢?

代码语言:javascript
复制
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

代码语言:javascript
复制
jobs:
	laowang_fasong_tools: # 任务的名称,建议使用下划线
    runs-on: ubuntu-latest
    steps: # 步骤

steps

代码语言:javascript
复制
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

代码语言:javascript
复制
jobs:
	cha_hu_kou:
    needs: [zhangshan, zhangshan_cunweihui]
    name: zhangshan_xian
    runs-on: ubuntu-latest

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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-10-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 趣玩Github Action
    • Actions secrets
      • Action.yml
        • on
        • jobs
        • steps
        • needs
      • 比较复杂
        • action.yml
        • dockerfile
      • 思考
      相关产品与服务
      容器镜像服务
      容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档