github action 使用
大约 2 分钟
确保 GitHub Actions 有权限操作 Releases。在仓库的
Settings
>Actions
>Permissions
中,确保Read and write permissions
被开启。
name: Example Workflow
on: push
permissions:
contents: write # 允许工作流对仓库内容进行写入操作
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Modify a file
run: |
echo "Hello, World!" > hello.txt
git config --global user.name "github-actions"
git config --global user.email "github-actions@github.com"
git add hello.txt
git commit -m "Add hello.txt"
git push
on 事件
on:
push:
tags:
- 'v*' # 匹配以 'v' 开头的标签,例如 v1.0.0
on:
push:
tags:
- 'v*' # 监听以 v 开头的标签
pull_request:
branches:
- master # 监听 master 分支的 PR
jobs
指定环境
jobs:
runs-on: ubuntu-latest
steps步骤
选择分支
https://github.com/actions/checkout
jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
nodejs
jobs:
steps:
- name: 设置 Node.js
uses: actions/setup-node@v3
with:
node-version: 20
cache: npm
发布tag包
https://github.com/softprops/action-gh-release
目录结构
│ release-notes.md # 添加本次发布的更新说明
├─.github
│ └─workflows
│ deploy-release.yml
eploy-release.yml
name: Release
on:
push:
tags:
- 'v*' # 匹配以 'v' 开头的标签,例如 v1.0.0
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Create Release
uses: softprops/action-gh-release@v2
with:
token: ${{ secrets.GITHUB_TOKEN }} # GITHUB_TOKEN 默认的账号
tag_name: ${{ github.ref_name }} # 使用推送的 tag 名称
name: ${{ github.ref_name }} # Release 名称
draft: false # 是否为草稿
prerelease: false # 是否为预发布版本
body_path: ./release-notes.md # 当前项目根目录的 Markdown 文件路径,下面方式二选一
body: |
发布说明
# 示例:
tips: 需要开启当前仓库的action read and write 权限
编译上传dockerhub
https://github.com/docker/build-push-action
此action会发布一个tag的版本号跟一个latest
1、初始化git仓库
2、创建tag标签进行发布才会触发更新
git tag v1.0.0
git push origin|github v1.0.0
.github/workflows/docker-images.yml
name: 发布docker镜像
on:
push:
tags:
- 'v*' # 监听以 v 开头的标签
pull_request:
branches:
- master # 监听 master 分支的 PR
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }} # Docker Hub 用户名
password: ${{ secrets.DOCKER_PASSWORD }} # Docker Hub 密码
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: orangbus/movie-cloud # 镜像的名称
tags: |
type=ref,event=tag
type=ref,event=branch
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
file: ./Dockerfile # Dockerfile 的路径
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
1、开启仓库的actions的读写权限
2、添加环境变量
go打包发布
将go编译后的文件打包后发布出来