方法 & 步骤
一:预先配置
1.1 DockerHub配置私有仓库用户名以及密钥
- 站点创建仓库
- 站点配置私钥:
在:AccountSettings --> Security --> Personal access tokens --> Generate new token生成访问token
❗️:复制你的密钥,然后记录你的账号,下面配置GitHub变量时候会用到
1.2 配置GitHub仓库密钥:
点击进入你GitHub仓库找到目标项目中Settings的Tab页
二: 项目添加脚本
2.1 DockerFile (用于创建Docker容器配置)
FROM openjdk:8-jre
WORKDIR /data
# 这里修改成你的项目路径
COPY boot270/target/*.jar /data/app.jar
#暴露容器的端口
EXPOSE 9000
ENV TZ "Asia/Shanghai"
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime
ENTRYPOINT ["java","-jar","/data/app.jar"]
2.2 GitHub Workflow工作流脚本 (用于Actions的CI)
在 GitHub项目中的action中配置添加 或者 直接在项目的根目录创建.github/workflows/action.ymll文件
- 官方模版:
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: Java CI with Maven
on:
push:
branches: [ "5.0.x" ]
pull_request:
branches: [ "5.0.x" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml
# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
- name: Update dependency graph
uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6
-
这是修改后的模版 (action.yml文件)
name: Java CI with Maven # *1,修改成你的分支 on: push: branches: [ boot270-docker ] pull_request: branches: [ boot270-docker ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up JDK 1.8 uses: actions/setup-java@v1 with: java-version: 1.8 - name: Build with Maven run: mvn -B package --file pom.xml # 2 Docker相关操作 # *2.1 --file默认在action的同目录寻找,如果不在相同目录要指定Dockerfile的目录 --tag后可以修改成你的镜像名称 - name: BuildDockerImage run: docker build . --file boot270/Dockerfile --tag my-image-name:$(date +%s) # 2.2 发布到DockerHub (uses固定使用elgohr大神的action不需要修改) - name: Publish to Docker Repository uses: elgohr/Publish-Docker-Github-Action@v5 # *2.3 name改成你DockerHub仓库地址:username和password这里改成你GitHub中配置的敏感环境变量 with: name: keygenee/priv-demo:iamtagname password: ${{ secrets.INPUT_PASSWORD }} username: ${{ secrets.INPUT_USERNAME }} dockerfile: ./boot270/Dockerfile
三:提交代码触发Actions
3.1 可以查看到Action已经成功
3.2 DockerHub已经push成功
参考资料 & 致谢
[1] Github文档-管理部署环境