跳至主要內容

github构建docker镜像并推送

brinishness...大约 1 分钟Dockerdockerbuildgithub

github构建docker镜像并推送

1.新建github仓库

新建github仓库

2.新建Dockerfile

FROM golang:alpine AS builder
RUN mkdir /app
ADD . /app/
WORKDIR /app
ENV GO111MODULE=off
RUN go build -o hello .
FROM alpine
RUN mkdir /app
WORKDIR /app
COPY --from=builder /app/hello .
CMD ["./hello"]

3.新建.github/workflows/docker.yml

name: CD

on:
  push:
    tags:
      - 'v*.*.*'  # 推送tag时触发
jobs:
  push_to_registries:
    name: 发布多平台docker镜像
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read
    steps:
      - name: 拉取代码
        uses: actions/checkout@v3
      
      - name: 登录docker
        uses: docker/login-action@v2.1.0
        with:
          username: ${{ secrets.DOCKER_USERNAME }}  #dockerhub用户名
          password: ${{ secrets.DOCKER_PASSWORD }}  #dockerhub密码
      
      - name: 登录github仓库
        uses: docker/login-action@v2.1.0
        with:
          registry: ghcr.io
          username: ${{ github.actor }}  #github用户名
          password: ${{ secrets.DOCKER_TOKEN }}  #github token
      
      - name: 推送标记
        id: meta
        uses: docker/metadata-action@v4.4.0
        with:
          images: |
            brinishness/go-test  #dockerhub仓库
            ghcr.io/${{ github.repository }}  #ghcr.io仓库
            
      - name: 配置buildx
        uses: docker/setup-buildx-action@v2.5.0
        with:
          use: true
          platforms: linux/amd64,linux/arm64,linux/arm64/v8,linux/arm/v7  # 构建平台

      - name: 构建并发布镜像
        uses: docker/build-push-action@v4.0.0
        with:
          context: .
          no-cache: true
          push: true
          platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

4.配置dockerhub用户密码和github token

配置dockerhub用户密码

5.拉取代码

git clone https://github.com/brinishness/go-test.git

6.添加文件到git并推送

git add .
git commit -m "add"
git push origin master

7.创建tag并推送

git tag v1.0.0
git push origin v1.0.0

8.查看github actions

查看github actions

9.查看github镜像

查看github镜像

10.查看dockerhub

查看dockerhub

11.查看例子 githubopen in new window

https://github.com/brinishness/go-test.git
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.5