如何在GitHubActions工作流中实现Docker镜像的语义版本控制



我想用GitHub Actions工作流实现以下CI管道。

Pull Request已合并->正在触发GitHub操作->docker镜像的语义版本增加了一,或者版本基于GitHub标记——如果可以以某种方式标记pull请求合并的话。

如何实现这一点,还是更好的方法?

我试过保守秘密,但没有成功。如何在GitHub Actions工作流中实现语义版本控制?

name: Docker Image CI
on:
push:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build the Docker image
run: docker build . --file Dockerfile --tag my-image-name:${{github.ref_name}}

${{github.ref_name}}为您提取标签,或者像上一步中的git describe --abbrev=0一样运行git命令来获取最新的标签,并将其附加到图像名称中,然后像一样使用它

- name: Get Tag
id: vars
run: echo ::set-output name=tag::${git describe --abbrev=0}
- name: Build the Docker image
run: docker build . --file Dockerfile --tag my-image-name:${{ steps.vars.outputs.tag }}

您可以在市场上使用许多semver操作。例如,我尝试过使用这个-Semver动作

这将提升您的repo版本,您可以使用gitbash命令在下一个作业中获得提升的版本。

因此,结合docker构建,您可以执行以下操作:

jobs:
update-semver:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: haya14busa/action-update-semver@v1
id: version
with:
major_version_tag_only: true  # (optional, default is "false")
Build:
name: Build Image
needs: [update-semver]
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Build image
run: |
tag_v=$(git describe --tags $(git rev-list --tags --max-count=1))
tag=$(echo $tag_v | sed 's/v//')
docker build -t my_image_${tag} .

这是我的公共解决方案:一个或多或少微不足道的docker映像,在pull请求期间构建和运行,在推送到main时基于提交来提升版本。所有这些都是基于其他人的出色工作,即使用现有的GitHub操作。

  • docker QEMU,buildx,构建推送操作
  • https://github.com/addnab/docker-run-action运行/测试映像
  • https://github.com/anothrNick/github-tag-action在构建和推送之前创建新标记
  • a";释放";依赖于成功构建并且仅在main上触发的工作流

详细信息:https://github.com/DrPsychick/docker-githubtraffic/tree/main/.github/workflows

最新更新