ci-1
This commit is contained in:
		
							parent
							
								
									06d7e193ea
								
							
						
					
					
						commit
						8fbf05ed69
					
				
							
								
								
									
										78
									
								
								.gitea/workflows/docker_build.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								.gitea/workflows/docker_build.yml
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,78 @@
 | 
				
			|||||||
 | 
					name: Build and Push Docker Image
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					on:
 | 
				
			||||||
 | 
					  push:
 | 
				
			||||||
 | 
					    branches:
 | 
				
			||||||
 | 
					      - "**" # 匹配所有分支
 | 
				
			||||||
 | 
					    paths-ignore: # 忽略一些不必要的文件
 | 
				
			||||||
 | 
					      - ".gitignore"
 | 
				
			||||||
 | 
					      - "README.md"
 | 
				
			||||||
 | 
					      - ".vscode/**"
 | 
				
			||||||
 | 
					  pull_request:
 | 
				
			||||||
 | 
					    branches: ["main"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					env:
 | 
				
			||||||
 | 
					  REGISTRY: ghcr.io  # 直接使用GHCR
 | 
				
			||||||
 | 
					  IMAGE_NAME: catface_backend_go   # 对应您要推送的镜像名称
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					jobs:
 | 
				
			||||||
 | 
					  build-and-push:
 | 
				
			||||||
 | 
					    runs-on: ubuntu-latest
 | 
				
			||||||
 | 
					    permissions:
 | 
				
			||||||
 | 
					      contents: read
 | 
				
			||||||
 | 
					      packages: write
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    steps:
 | 
				
			||||||
 | 
					      - name: 打印调试信息
 | 
				
			||||||
 | 
					        run: |
 | 
				
			||||||
 | 
					          echo "Registry: ${{ env.REGISTRY }}"
 | 
				
			||||||
 | 
					          echo "Image: ${{ env.GHCR_USER }}/${{ env.IMAGE_NAME }}"
 | 
				
			||||||
 | 
					          echo "Gitea Actor: ${{ gitea.actor }}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      - name: 检出代码
 | 
				
			||||||
 | 
					        uses: actions/checkout@v4
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      - name: 获取分支名称
 | 
				
			||||||
 | 
					        id: branch_name
 | 
				
			||||||
 | 
					        run: |
 | 
				
			||||||
 | 
					          branch=${GITHUB_REF#refs/heads/}
 | 
				
			||||||
 | 
					          echo "branch=${branch}" >> $GITHUB_OUTPUT
 | 
				
			||||||
 | 
					          # 主分支用latest,其他分支用dev-分支名
 | 
				
			||||||
 | 
					          if [ "${branch}" = "main" ]; then
 | 
				
			||||||
 | 
					            echo "tag=latest" >> $GITHUB_OUTPUT
 | 
				
			||||||
 | 
					          else
 | 
				
			||||||
 | 
					            # 替换非法字符为-
 | 
				
			||||||
 | 
					            safe_branch=$(echo "${branch}" | sed 's/[^a-zA-Z0-9]/-/g')
 | 
				
			||||||
 | 
					            echo "tag=dev-${safe_branch}" >> $GITHUB_OUTPUT
 | 
				
			||||||
 | 
					          fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      - name: 登录到 GitHub Container Registry
 | 
				
			||||||
 | 
					        uses: docker/login-action@v3
 | 
				
			||||||
 | 
					        with:
 | 
				
			||||||
 | 
					          registry: ${{ env.REGISTRY }}
 | 
				
			||||||
 | 
					          username: ${{ secrets.GHCR_USER }}
 | 
				
			||||||
 | 
					          password: ${{ secrets.GHCR_TOKEN }} # 在Gitea仓库设置中添加此secret
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      - name: 构建并推送 Docker 镜像
 | 
				
			||||||
 | 
					        uses: docker/build-push-action@v5
 | 
				
			||||||
 | 
					        with:
 | 
				
			||||||
 | 
					          context: .
 | 
				
			||||||
 | 
					          push: true
 | 
				
			||||||
 | 
					          tags: ${{ env.REGISTRY }}/${{ secrets.GHCR_USER }}/${{ env.IMAGE_NAME }}:${{ steps.branch_name.outputs.tag }}
 | 
				
			||||||
 | 
					          labels: |
 | 
				
			||||||
 | 
					            org.opencontainers.image.source=${{ gitea.server_url }}/${{ gitea.repository }}
 | 
				
			||||||
 | 
					          build-args: |
 | 
				
			||||||
 | 
					            BUILDKIT_INLINE_CACHE=1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      # - name: 通过SSH部署到服务器
 | 
				
			||||||
 | 
					      #   if: ${{ steps.branch_name.outputs.branch == 'main' }} # 仅main分支触发部署
 | 
				
			||||||
 | 
					      #   run: |
 | 
				
			||||||
 | 
					      #     ssh -o StrictHostKeyChecking=no \
 | 
				
			||||||
 | 
					      #         -i "${{ secrets.SSH_PRIVATE_KEY }}" \
 | 
				
			||||||
 | 
					      #         ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'EOF'
 | 
				
			||||||
 | 
					      #     # 拉取最新镜像
 | 
				
			||||||
 | 
					      #     docker pull ${{ env.REGISTRY }}/${{ env.GHCR_USER }}/${{ env.IMAGE_NAME }}:${{ steps.branch_name.outputs.tag }}
 | 
				
			||||||
 | 
					      #     # 重启服务(根据实际情况修改)
 | 
				
			||||||
 | 
					      #     docker-compose down || true
 | 
				
			||||||
 | 
					      #     docker-compose up -d
 | 
				
			||||||
 | 
					      #     EOF
 | 
				
			||||||
@ -13,9 +13,9 @@ jobs:
 | 
				
			|||||||
      - name: SSH 连接测试
 | 
					      - name: SSH 连接测试
 | 
				
			||||||
        uses: appleboy/ssh-action@master
 | 
					        uses: appleboy/ssh-action@master
 | 
				
			||||||
        with:
 | 
					        with:
 | 
				
			||||||
          host: 120.27.198.238
 | 
					          host: '113.44.68.213'
 | 
				
			||||||
          username: root
 | 
					          username: root
 | 
				
			||||||
          password: Havocantelope420#
 | 
					          password: Havocantelope420
 | 
				
			||||||
          port: '22'
 | 
					          port: '22'
 | 
				
			||||||
          script: |
 | 
					          script: |
 | 
				
			||||||
            echo "Running command on remote server"
 | 
					            echo "Running command on remote server"
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user