Github Springng实现Actions Boot自动化部署(第二弹)序言
今天,让我们来谈谈如何使用GitHub结合Actions实现Spring Boot程序从提交代码到包装、容器化和部署的全过程自动化。首先,我们必须有一个Spring,可以在本地运行 Boot程序,Github上有一个仓库。
1.1.1.1自动化部署 将代码提交到Githubb1.2 设置服务器密钥、GitHub私钥、DockerHub账号maven.yml文件
name: Deploy with dockeron: push: # 分支 branches: [ "master" ] pull_request: branches: [ "master" ]jobs: compile: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up JDK 21 uses: actions/setup-java@v2 with: java-version: '21' distribution: 'adopt' # maven缓存,如果不加,每次都会去重新拉,会影响速度 - name: Dependies Cache uses: actions/cache@v2 with: path: ~/.m2/repository key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} restore-keys: | ${{ runner.os }}-maven- # 编译打包 - name: Build with Maven run: | mvn package -Dmaven.test.skip=true # 登录Docker Hub - name: Login to Docker Hub uses: docker/login-action@v1 with: username: ${{ secrets.DOCKER_HUB_USERNAME }} password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} - name: Set up Docker Buildx id: buildx uses: docker/setup-buildx-action@v1 #build镜像和push到中央仓库 - name: Build and push id: docker_build uses: docker/build-push-action@v2 with: context: ./ file: ./Dockerfile push: true tags: ${{secrets.DOCKER_HUB_USERNAME}}/back:latest #push之后,脚本用ssh连接服务器执行 - name: SSH# uses: fifsky/ssh-action@master uses: appleboy/ssh-action@master with:# command: |# cd /develop/work/education-back-server# sh start.sh host: ${{secrets.SERVER_HOST}} username: ${{secrets.SERVER_USERNAME}} port: ${{secrets.SERVER_PORT }}# key: ${{secrets.SERVER_PRIVATE_KEY}} password: ${{secrets.SERVER_PASSWORD}} script: sh /develop/work/education-back-server/start.sh# sh start.sh
1.4 新增Dockerfile文件dockerfile文件
#FROM基本镜像 bubaiwantong/openjdk:21-jdk-alpine#安装字体RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && apk add --update ttf-dejavu fontconfig && rm -rf /var/cache/apk/* && mkfontscale && mkfontdir && fc-cacheRUN apk add --update ttf-dejavu fontconfig && rm -rf /var/cache/apk/*#添加文件ADDD添加文件 education-back/target/back-0.0.1-SNAPSHOT.jar /usr/localRUN chmod u+x /usr/local/back-0.0.1-SNAPSHOT.jar#设置时区RUN rm -f /etc/localtime \&& ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo "Asia/Shanghai" > /etc/timezone#将目录挂载到容器中#VOLUME ["/data"]#环境变量设置#ENV #EXPOSE开放端口 8088#启动时执行的命令CMD ["/bin/bash"]#ENTRYPOINT启动时执行的命令 ["java","-jar","/usr/local/back-0.0.1-SNAPSHOT.jar"]
1.5 编写服务器启动脚本start.sh文件
#!/bin/bashdocker pull bubaiwantong/back:latestdocker tag docker.io/bubaiwantong/back:latest back:latestdocker stop backdocker rm backdocker run --name back -p 8088:8088 -d back:latestdocker image prune -
2.7 打开自动化部署将代码提交给master,在GitHub中的Actions可以发现项目正在自动化部署
在浏览器地址栏打开网站主页 可以发现自动化部署成功!
三、写在最后如果这篇文章对你有帮助,我希望它能给你【JavaGPT】点个赞