创建 workflows 文件有两种方式:
第一种是在仓库./.github/workflows/ 目录下创建 yaml 文件,
第二种方式是在github action 中创建,并用在线编辑器编写,一下使用这种方式演示
第一步先 选中 actions 选项卡,然后根据自己需求创建 yml 脚本
set up a workflow yourself
创建普通 yml 模板文件第二步使用在线编辑器编辑,编辑器页面主要使用有:
写完后通过右上角 start commit
按钮提交
创建完文件后,Action
选项卡会变成如上图界面,左边区域为所有 workflows,点击对应的 workflows(这里点中 hero-deploy 任务),右边展示对应 workflows 的执行记录和过滤器,点击对应记录可以看到各个执行记录
显示 hero-deploy 里的所有任务完成情况(这里只有一个任务)
查看详细每个任务里面的每一个step
的日志
workflows
运行于仓库 hexo-project-src
,运行产出物推送到仓库 xuzhuohao.github.io
hexo-project-src
项目下 (每次都要)xuzhuohao.github.io
hexo-project-src
workflows
后操作如下:
hexo-project-src
(通过git,或者直接 github web 提交)workflows
,让自己偷个懒吧secrets.ACCESS_TOKEN
供 step2 使用在账号设置中获取 access_token (settings -> Developer settings -> Personal access tokens)
打开仓库 hexo-project-src
,配置 ACCESS_TOKEN
workflows
中的使用方法为 ${{secrets,yourName}}
)1.1
在仓库 hexo-project-src
创建并提交 workflows文件
如下:文件 .github/workflows/hero-deploy.yml : github address
yaml# workflows name
name: hero-deploy
# 触发条件:master push
on:
push:
branches: [ master ]
# 任务
jobs:
# 任务1:deploy(job_id)
deploy:
# deploy 任务的名称(说明)
name: deploy hero project
# 运行环境(GitHub 托管的运行器)
runs-on: ubuntu-latest
# 步骤
steps:
# 步骤1:每一步可以使用不同的action或者run不同的指令
- name: checkout code
uses: actions/checkout@master
- name: install node
uses: actions/setup-node@v1
with:
node-version: 12.x
- name: cache node modules
uses: actions/cache@v1
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
- name: install hero
run: |
npm install hexo-cli gulp -g
npm install
- name: generate file
run: |
hexo clean
hexo generate
# - name: Execute gulp task
# run: gulp
- name: deploy hexo
env:
# Github 仓库
GITHUB_REPO: github.com/XuZhuohao/xuzhuohao.github.io
# 推送
run: |
cd ./public && git init && git add
git config user.name "yui_htt"
git config user.email "786725551@qq.com"
git add *
git commit -m "GitHub Actions Auto Builder at $(date +'%Y-%m-%d %H:%M:%S')"
git push --force --quiet "https://${{ secrets.ACCESS_TOKEN }}@$GITHUB_REPO" master:master
该脚本大意如下:
workflows
,名字为 hero-deploy, 触发条件为 master
分支接受到 push
请求workflows
包含一个 job
, job
步骤如下:
public
文件夹public
文件夹推送到仓库 xuzhuohao.github.io
(注意这里是另一个仓库了)secrets.ACCESS_TOKEN
在 step1 已经进行创建yamlname: the-workflows-names
on:
push:
targs:
- "*"
jobs:
job1:
name: job1-name
steps:
- name: first step
run: echo hello word
- name : second step
uses: actions/checkout@v2
with:
ref: ${{github.ref}}
Docker 和 JavaScript 操作需要元数据文件。 元数据文件名必须是 action.yml
或 action.yaml
。 元数据文件中的数据定义操作的输入、输出和主要进入点
key | Required | intro |
---|---|---|
name | Required | 在 Action Tab中显示该操作的名称 |
author | Optional | 作者名字 |
description | Required | 操作的简短描述 |
inputs | Optional | 用于在操作运行时的输入参数 ,会被github转化成环境变量,输入的 id 大写会被转换成小写,建议使用小写输入 id,驼峰法会用下划线连接 |
input.<input_id> | Required | 输入关联标识符 |
output | ||
output(composite) | ||
runs(JavaScript) | ||
runs(composite) | ||
runs(Docker) | ||
branding |
docker 镜像提交 docker hub
yamlname: Docker Image CI
on:
push:
branches:
- "master"
tags:
- 'v*'
pull_request:
branches: [ "master" ]
workflow_dispatch:
env:
# Use docker.io for Docker Hub if empty
REGISTRY: ''
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ secrets.DOCKER_USERNAME }}/${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# - name: Set up JDK 8
# uses: actions/setup-java@v3
# with:
# java-version: '8'
# distribution: 'temurin'
# cache: maven
# - name: Build with Maven
# run: mvn -B clean package --file pom.xml
- name: Build the Docker image
run: docker build . --file Dockerfile --tag ${{env.IMAGE_NAME}}
- name: echo
run: echo $GITHUB_REF_NAME
# 登录
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.IMAGE_NAME }}
tags: |
# set latest tag for default branch
type=raw,value=latest,enable={{is_default_branch}}
# tag event
type=ref,enable=true,priority=600,prefix=,suffix=,event=tag
# 推送
- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
本文作者:Yui_HTT
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!