name: CI and Docker Build on: push: branches: - main - master tags: - 'v*' pull_request: branches: - main - master jobs: build: name: Build Binaries runs-on: ubuntu container: image: golang:1.24 env: # actions/checkout 是 JS action,需要 node,golang 镜像里没有,所以直接用 git clone GOPROXY: https://goproxy.cn,direct steps: - name: Checkout repository run: | rm -rf /tmp/repo git clone https://admin:${{ secrets.TOKEN }}@git.viaeon.com/admin/nps.git /tmp/repo cd /tmp/repo git checkout ${{ github.sha }} || true git log --oneline -1 - name: Build nps and npc run: | cd /tmp/repo go mod download echo "Building nps..." CGO_ENABLED=0 go build -v -o /tmp/nps ./cmd/nps/nps.go echo "Building npc..." CGO_ENABLED=0 go build -v -o /tmp/npc ./cmd/npc/npc.go ls -la /tmp/nps /tmp/npc docker: name: Build and Push Docker Images runs-on: ubuntu needs: [build] if: github.event_name == 'push' env: RUNNER_TOOL_CACHE: /toolcache steps: - name: Install Docker CLI run: | if ! command -v docker &> /dev/null; then if command -v apk &> /dev/null; then apk add --no-cache docker-cli elif command -v apt-get &> /dev/null; then apt-get update && apt-get install -y docker.io else curl -fsSL https://download.docker.com/linux/static/stable/x86_64/docker-24.0.7.tgz | tar xz -C /tmp mv /tmp/docker/docker /usr/local/bin/ chmod +x /usr/local/bin/docker fi fi docker --version - name: Checkout repository run: | rm -rf /tmp/repo git clone https://admin:${{ secrets.TOKEN }}@git.viaeon.com/admin/nps.git /tmp/repo cd /tmp/repo git checkout ${{ github.ref_name }} || true git log --oneline -1 - name: Login to Gitea Container Registry run: | echo "${{ secrets.TOKEN }}" | docker login git.viaeon.com -u "${{ github.actor }}" --password-stdin 2>&1 - name: Build and push nps run: | cd /tmp/repo docker build \ --label "org.opencontainers.image.source=https://git.viaeon.com/admin/nps" \ --label "org.opencontainers.image.revision=${{ github.sha }}" \ --build-arg GOPROXY=https://goproxy.cn,direct \ -f Dockerfile.nps \ -t git.viaeon.com/admin/nps:${{ github.ref_name }} \ -t git.viaeon.com/admin/nps:latest . 2>&1 docker push git.viaeon.com/admin/nps:${{ github.ref_name }} 2>&1 docker push git.viaeon.com/admin/nps:latest 2>&1 - name: Build and push npc run: | cd /tmp/repo docker build \ --label "org.opencontainers.image.source=https://git.viaeon.com/admin/nps" \ --label "org.opencontainers.image.revision=${{ github.sha }}" \ --build-arg GOPROXY=https://goproxy.cn,direct \ -f Dockerfile.npc \ -t git.viaeon.com/admin/npc:${{ github.ref_name }} \ -t git.viaeon.com/admin/npc:latest . 2>&1 docker push git.viaeon.com/admin/npc:${{ github.ref_name }} 2>&1 docker push git.viaeon.com/admin/npc:latest 2>&1 deploy: name: Deploy to Remote Server runs-on: ubuntu needs: docker if: github.event_name == 'push' && github.ref == 'refs/heads/master' steps: - name: Install SSH Client run: | if command -v apk &> /dev/null; then apk add --no-cache openssh-client sshpass elif command -v apt-get &> /dev/null; then apt-get update && apt-get install -y openssh-client sshpass else echo "Cannot install sshpass" exit 1 fi ssh -V - name: Deploy via SSH run: | sshpass -p '${{ secrets.DEPLOY_PASSWORD }}' ssh -o StrictHostKeyChecking=no -p ${{ secrets.DEPLOY_PORT || '22' }} ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }} << ENDSSH set -e cd "${{ secrets.DEPLOY_PATH }}" echo "Current directory: \$(pwd)" ls -la echo "Pulling latest image..." docker pull git.viaeon.com/admin/nps:latest echo "Restarting containers..." docker compose down || true docker compose up -d echo "Cleaning up old images..." docker image prune -f echo "Deployment completed!" ENDSSH