GitHub Actions 持續集成 - 3. 構建 Docker 鏡像並推至 Docker Hub

GitHub Actions 持續集成 - 3. 構建 Docker 鏡像並推至 Docker Hub

本文地址:blog.lucien.ink/archives/498

0. 摘要

之前挖了一個,慢慢補上。

上篇文章介紹瞭如何藉助 GitHub Actions 在 Release 時自動上傳打包好的工程。本篇文章旨在介紹將 Dockerfile 構建出來的鏡像上傳至 Docker Hub。

1. 現成的輪子

使用此 Action 可以將通過 Dockerfile 生成的鏡像上傳至 Docker Hub。

github.com/elgohr/Publish-Docker-Github-Action

1.1 稍作修改

原作支持將 release 的 tag 當作 docker image 的 tag。

但是我之前都是使用的阿里雲的命名規則,假設 release 的 tag 爲 release-v3.1,那麼 docker image 的 tag 就會是 3.1

爲了保持命名風格的統一,我對原作進行了修改

2. GitHub Actions 配置

.github/workflows/upload-to-release.yml

name: Go Release

on:
  release:
    types: [published]

jobs:

  release:
    if: github.repository == 'PasteUs/HelloCI'
    name: Build with go ${{ matrix.go_version }} on ${{ matrix.os }} and upload
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        go_version: [1.13]
        os: [ubuntu-latest]

    steps:

      - name: Set up Go ${{ matrix.go_version }}
        uses: actions/setup-go@v1
        with:
          go-version: ${{ matrix.go_version }}
        id: go

      - name: Check out code into the Go module directory
        uses: actions/checkout@v1

      - name: Get dependencies
        run: |
          go get -v -t -d ./...
          if [ -f Gopkg.toml ]; then
              curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
              dep ensure
          fi

      - name: Build
        run: |
          go build -v -o main .

      - name: Publish Docker
        uses: LucienShui/Publish-Docker-Github-[email protected]
        with:
          name: registry.cn-hangzhou.aliyuncs.com/pasteus/hello-ci # docker image 的名字
          username: ${{ secrets.DOCKER_USERNAME }} # 用戶名
          password: ${{ secrets.DOCKER_PASSWORD }} # 密碼
          registry: registry.cn-hangzhou.aliyuncs.com # 阿里巴巴的 Docker Hub
          dockerfile: Dockerfile # 指定 Dockerfile 的位置
          tag_names: true # 是否將 release 的 tag 作爲 docker image 的 tag

2.1 Dockerfile

Dockerfile

FROM alpine:3
LABEL maintainer="Lucien Shui" \
      email="[email protected]"
WORKDIR /root/
COPY main ./app
CMD ["./app"]
發佈了391 篇原創文章 · 獲贊 101 · 訪問量 32萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章