领取链接:链接
secret | value | remark |
---|---|---|
DOCKER_HUB_ACCESS_TOKEN | 1234567890 | dockerhub账号私钥 |
DOCKER_HUB_USERNAME | bubaiwantong | dockerhub账号 |
SERVER_HOST | 127.0.0.1 | 服务器IP地址 |
SERVER_PASSWORD | 1234567890 | 服务器密码 |
SERVER_PORT | 22 | 服务器开放端口 |
SERVER_PRIVATE_KEY | 1234567890 | 服务器私钥 |
SERVER_USERNAME | root | 服务器账号 |
TOKEN | 1234567890 | Github的Token |
main.yml文件
# test-deploy.yml
name: Deploy with docker
# 手动构建
#on: workflow_dispatch
on:
push:
# 选择分支,我这里设置为master分支
branches: [ "master" ]
pull_request:
branches: [ "master" ]
jobs:
build:
# 运行的环境
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20] #选择你需要的nodejs版本
# 步骤
steps:
- uses: actions/checkout@v3 # git pull
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm install
- run: npm run build
# 登录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}}/front:latest
# push后,用ssh连接服务器执行脚本
- name: SSH
uses: appleboy/ssh-action@master
with:
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-front/start.sh
Dockerfile文件如下配置
# 设置基础镜像,这里使用最新的nginx镜像,前面已经拉取过了
FROM bubaiwantong/nginx:latest
#作者
MAINTAINER JavaGPT [email protected]
#执行命令,主要用来安装相关的软件
RUN rm /etc/nginx/conf.d/default.conf
ADD default.conf /etc/nginx/conf.d/
# 设置时区
RUN rm -f /etc/localtime \
&& ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
#添加文件
# 将dist文件中的内容复制到 /usr/share/nginx/html/ 这个目录下面
COPY dist/ /usr/share/nginx/html/
default.conf配置文件如下
server {
listen 80;
listen [::]:80;
server_name 106.52.239.29;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api/back/ {
proxy_pass http://106.52.239.29:8088/;
}
location /api/base/ {
proxy_pass http://106.52.239.29:8089/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
#!/bin/bash
docker pull bubaiwantong/front:latest
docker tag docker.io/bubaiwantong/front:latest front:latest
docker stop front
docker rm front
docker run --name front -p 8080:80 --restart=always -d front:latest
docker image prune -af
提交代码到master,在GitHub中的Actions即可发现项目正在自动化部署
在浏览器地址栏打开网站首页
就可以发现自动化部署成功啦!
本次Github Actions + 华为云的云耀云服务器L实例自动化部署体验非常nice!参加活动即可免费获取1个月服务器的免费使用权,如果你对Github自动化部署有什么问题,欢迎在评论下方留言喔,我看到消息会一一回复大家的问题!
如果这篇【文章】有帮助到你,希望可以给【JavaGPT】点个赞,创作不易,如果有对【后端技术】、【前端领域】感兴趣的小可爱,也欢迎关注❤️❤️❤️ 【JavaGPT】❤️❤️❤️,我将会给你带来巨大的【收获与惊喜】!