前言

这里暂不作过多的操作,还是保持与此前一致。即通过Github Action完成Docker Image的build与push,目标仓库为阿里云容器镜像服务实例(个人版)registry.cn-hangzhou.aliyuncs.com

那么一共分为三个部分:

  • Dockerfile编写
  • 阿里云容器镜像服务配置
  • Github Action

Dockerfile编写

  • jdk17
  • gradle
FROM gradle:jdk17-alpine AS build

# 设置语言,支持中文
ENV LANG C.UTF-8

COPY --chown=gradle:gradle . /opt/gradle/src
WORKDIR /opt/gradle/src
RUN gradle clean build -x test --no-daemon

FROM eclipse-temurin:17-jdk-jammy

COPY --from=build /opt/gradle/src/build/libs/*.jar /usr/app/

WORKDIR /usr/app/

RUN sh -c 'touch micro-weather-backend-1.0.0-RELEASE.jar'

ENTRYPOINT ["java", "-jar", "micro-weather-backend-1.0.0-RELEASE.jar"]

Github Action

  1. 先在阿里云镜像服务中创建命名空间

  2. 创建仓库(可选,因为可以自动创建)

  3. 编写Github Action脚本

    micro-weather-github-action-01

    name: Micro Weather Service Image Build And Push CI
    
    on:
    push:
        branches:
        - 'main'
    jobs:
    docker:
        runs-on: ubuntu-latest
        steps:
        - name: Checkout
            uses: actions/checkout@v2
    
        # setup-qemu 如果您想使用 QEMU 添加仿真支持以便能够针对更多平台进行构建,则 action 会很有用
        - name: Set up QEMU
            uses: docker/setup-qemu-action@v1
    
        # setup-buildx-action 将默认使用docker-container 构建器驱动程序创建和引导构建器。非必需
        - name: Set up Docker Buildx
            uses: docker/setup-buildx-action@v1
    
        - name: Login to Aliyun DockerHub
            uses: docker/login-action@v1
            with:
            registry: ${{secrets.ALIYUN_DOCKERHUB_REGISTRY}}
            username: ${{ secrets.ALIYUN_DOCKERHUB_USERNAME }}
            password: ${{ secrets.ALIYUN_DOCKERHUB_TOKEN }}
    
        - name: Build and push
            uses: docker/build-push-action@v2
            with:
            push: true
            tags: registry.cn-hangzhou.aliyuncs.com/fuyi-atlas/micro-weather:latest
    
  4. 设置环境变量

    micro-weather-github-action-02

    micro-weather-github-action-03

    micro-weather-github-action-04

  5. 触发Action测试(提交代码)

    第一次失败是因为我是直接在github上面添加的action配置,通过commit提交后直接触发了action动作,但是此时还没有配置环境变量,所以失败了。所以可以在action触发前将环境变量设置好就不会失败了。

    micro-weather-github-action-05

    micro-weather-github-action-06

参考文档

💡 Github Action脚本内容来自于网络,存在少许更改。但由于记不得来源,所以没有做出记录