Github Action Workflow 实践
source link: https://segmentfault.com/a/1190000041102282
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Action 基础
常用Action介绍
actions/checkout@v2
actions/checkout@v2: 进入到被推送的代码下仓库内容。通常需要以来仓库内容的时候加入,比如:打包构建、读取 package.json
文件等
actions/setup-node@v2
actions/setup-node@v2: 安装设置 node 版本,接下来可以使用 with
指明版本
appleboy/ssh-action
appleboy/ssh-action: 通过SSH, 在远程主机上执行命令。仅支持 Linux Docker。
actions/upload-release-asset
actions/upload-release-asset: 上传 Release 下的文件
同一 workflow 中,一个 job 使用另一个 job 的内容
Storing workflow data as artifacts
- 上传:actions/upload-artifact@v2
- 下载:actions/download-artifact@v2
通过设置
env
为变量的值jobs: example-job: steps: - name: Connect to PostgreSQL run: node client.js env: POSTGRES_HOST: postgres POSTGRES_PORT: 5432
- 通过secrets
jobs: example-job: steps: - name: Connect to PostgreSQL run: node client.js env: POSTGRES_HOST: ${{ secrets.POSTGRES_HOST } POSTGRES_PORT: ${{ secrets.POSTGRES_PORT }
读取文件内容
读取JSON
文件: ashley-taylor/[email protected]
- name: 读取当前版本号 id: version uses: ashley-taylor/[email protected] with: path: ./package.json property: version
读取文件文本内容:juliangruber/read-file-action@v1
- name: 读取描述文件 id: description uses: juliangruber/read-file-action@v1 with: path: ./description.txt
如何使用:
- name: 创建GitHub Release id: create_release uses: actions/create-release@latest env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: tag_name: v${{steps.version.outputs.value}} release_name: v${{steps.version.outputs.value}} body: ${{steps.description.outputs.content}} draft: false prerelease: false
读取某一个 step 产生的内容
read json value on: push:[main] run-on: ubuntu-latest steps: # 读取 package.json 文件内容 - name: read version id: version uses: ashley-taylor/[email protected] with: path: ./package.json property: version # 执行 Release - name: Release uses: actions/create-release@master env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: tag_name: v${{steps.version.outputs.value}} release_name: v${{steps.version.outputs.value}} ...
- 首先使用
id
标记这一 step 然后使用
${{steps.version.outputs.xxx}}
读取结果里面xxx字段的值- 执行
bin
命令
- name: Build uses: actions/setup-node@master - run: npm install # 安装第三方包 - run: npm run build # 打包 - run: tar -zcvf release.tgz
- name: Build uses: actions/setup-node@master - run: | npm install # 安装第三方包 npm run build # 打包 tar -zcvf release.tgz
执行仓库下的文件内容:run + 文件路径
jobs: example-job: steps: - name: Run build script run: ./.github/scripts/build.sh shell: bash
一 每天发送天气邮件
参考教程: https://www.ruanyifeng.com/bl...
name: 'Beijing Weather Bot' on: push: schedule: - cron: '25 2 * * *' jobs: bot: runs-on: ubuntu-latest steps: - name: Checkout codes uses: actions/checkout@v2 # 执行命令,生成带有天气情况的 html 文件:result.html - name: Get Weather run: bash ./weather.sh - name: Get Date run: echo "REPORT_DATE=$(TZ=':Asia/Beijing' date '+%Y-%m-%d %T')" >> $GITHUB_ENV - name: Send mail uses: dawidd6/action-send-mail@v2 with: server_address: smtp.163.com server_port: 465 username: ${{secrets.MAIL_USERNAME}} password: ${{secrets.MAIL_PASSWORD}} subject: Beijing Weather Report (${{env.REPORT_DATE}}) body: file://result.html to: ${{ secrets.TARGET_MAIL }} from: Weather-Beijing content_type: text/html
weather.sh:
#!/bin/sh set -eux CITY=beijing LANGUAGE="zh-CN" UA="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36" UNIT=m curl \ -H "Accept-Language: $LANGUAGE" \ -H "User-Agent: $UA" \ -o result.html \ wttr.in/$CITY?$UNIT
二 打包构建 & 文件上传到服务器
场景:在github上提交代码之后自动打包部署到远程服务器(腾讯云/阿里云等)
实现:
# workflow名 name: deploy to tencentCloud on: # 此CI/CD触发时的事件 push: # 在代码提交时自动触发 branches: - main # 一个 CI/CD 的工作流有许多 jobs 组成,比如最典型的 job 是 lint,test,build。 jobs: build: # 构建job runs-on: ubuntu-latest # 跑workflow的服务器系统 steps: # job的一系列动作 # 切换分支获取源码 - name: Checkout # step的名称,将会在 github action 的控制台中显示 # 选择一个action,可以理解为若干 steps.run,有利于代码复用 uses: actions/checkout@v2 # 安装使用 node:14 - name: use Node.js 14 uses: actions/setup-node@v1 with: node-version: 14 # 运行命令,npm install && npm run build - name: npm install and build run: | npm install npm run build env: CI: true # 部署到腾讯云服务器 - name: 上传到腾讯云 uses: easingthemes/ssh-deploy@main env: # 本地.ssh文件下的私钥id_rsa,存在secrets的TOKEN中 SSH_PRIVATE_KEY: ${{ secrets.TOKEN }} # 复制操作的参数。"-avzr --delete"意味部署时清空服务器目标目录下的文件 ARGS: "-avzr --delete" # 源目录,相对于仓库内容根目录的路径 SOURCE: "dist/" # 远程服务器地址 REMOTE_HOST: ${{ secrets.REMOTE_HOST }} # 远程服务器用户名 REMOTE_USER: "root" # 目标目录(远程服务器路径) TARGET: "/data/www"
- 在 main 分支上提交代码的时候会触发 workflow
- 使用node v14 下打包构建,待完成之后将打包产物上传到远程服务器
三 push 之后自动 release
场景:Release 之后,可以使用jsDelivr 实现免费CDN的功能。 可以参考这里:免费CDN:jsDelivr+Github 使用方法
name: release CI # Controls when the workflow will run on: # Triggers the workflow on push or pull request events but only for the main branch push: branches: - main # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: # This workflow contains a single job called "build" build: # The type of runner that the job will run on runs-on: ubuntu-latest # Steps represent a sequence of tasks that will be executed as part of the job steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - name: Checkout uses: actions/checkout@v2 # 读取 package.json 文件内容 - name: read version id: version uses: ashley-taylor/[email protected] with: path: ./package.json property: version # 执行 Release - name: Release uses: actions/create-release@master env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: tag_name: v${{steps.version.outputs.value}} release_name: v${{steps.version.outputs.value}} body: Release v${{steps.version.outputs.value}} draft: false prerelease: false
- 在 main 分支上提交代码的时候会触发 workflow
- 每次部署都是正式版本
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK