Skip to content

update toolset config #3

update toolset config

update toolset config #3

name: Deploy to Server
on:
push:
branches:
- main
workflow_dispatch:
env:
REGISTRY: ${{ vars.REGISTRY_URL }}
REGISTRY_USERNAME: ${{ vars.REGISTRY_USERNAME }}
IMAGE_NAME: ${{ vars.REGISTRY_URL }}/${{ vars.IMAGE_NAME }}
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Private Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ env.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Output image tag
run: |
echo "Image pushed: ${{ steps.meta.outputs.tags }}"
deploy:
needs: build-and-push
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
steps:
- name: Deploy to server
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
port: ${{ secrets.SERVER_PORT || 22 }}
script: |
# 设置变量
REGISTRY="${{ env.REGISTRY }}"
IMAGE_NAME="${{ env.IMAGE_NAME }}"
IMAGE_TAG="latest"
CONTAINER_NAME="toolset-app"
echo "开始部署..."
echo "镜像: ${IMAGE_NAME}:${IMAGE_TAG}"
# 拉取最新镜像
echo "拉取最新镜像..."
docker pull ${IMAGE_NAME}:${IMAGE_TAG}
# 停止并删除旧容器
echo "停止旧容器..."
if [ "$(docker ps -q -f name=${CONTAINER_NAME})" ]; then
docker stop ${CONTAINER_NAME}
docker rm ${CONTAINER_NAME}
fi
# 启动新容器
echo "启动新容器..."
docker run -d \
--name ${CONTAINER_NAME} \
--restart unless-stopped \
-p 8380:80 \
${IMAGE_NAME}:${IMAGE_TAG}
echo "部署完成!"