diff --git a/.gitea/workflows/init-stack.yaml b/.gitea/workflows/init-stack.yaml index 2de02c2..00d1396 100644 --- a/.gitea/workflows/init-stack.yaml +++ b/.gitea/workflows/init-stack.yaml @@ -2,10 +2,12 @@ name: Gitea Actions Demo run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀 on: - pull_request: + push: + branches: + - 'stack-init' paths: - 'stack.yml' - + jobs: Create-Stack: runs-on: synology @@ -14,7 +16,7 @@ jobs: - name: Checkout uses: https://github.com/actions/checkout@v4 with: - ref: init-stack + ref: stack-init - name: Get the endpoint ID # Usually ID is 1, but you can get it from the API. Only skip this if you are VERY sure. diff --git a/.gitea/workflows/tcaddiemaybe.yaml b/.gitea/workflows/tcaddiemaybe.yaml new file mode 100644 index 0000000..b499c2a --- /dev/null +++ b/.gitea/workflows/tcaddiemaybe.yaml @@ -0,0 +1,142 @@ +name: TournamentCaddie Prod Deploy +on: + push: + branches: + - master +jobs: + build_and_deploy: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to Docker registry + uses: docker/login-action@v1 + with: + registry: reg.dev.nervesocket.com + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} # This is a secret field, add your Docker Hub password here as a GitHub secret + + - name: Build and push docker image + id: build + run: | + docker buildx build --tag reg.dev.nervesocket.com/tournamentcaddie:latest . + docker push reg.dev.nervesocket.com/tournamentcaddie:latest # This will tag and push the image to your Docker registry + + - name: Update docker stack + run: | + docker pull reg.dev.nervesocket.com/tournamentcaddie:latest # This is just an example, you would need to replace this with the correct command for updating your docker stack + +name: Deploy TournamentCaddie + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: self-hosted + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Log in to private registry + run: | + echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login reg.dev.nervesocket.com -u "${{ secrets.REGISTRY_USERNAME }}" --password-stdin + + - name: Build and push Docker image + run: | + IMAGE_TAG="reg.dev.nervesocket.com/tournamentcaddie:latest" + docker build -t $IMAGE_TAG . + docker push $IMAGE_TAG + + - name: Fetch stack ID from Portainer + run: | + STACK_NAME="TournamentCaddie" + PORTAINER_API_URL="http://portainer:9000/api" + PORTAINER_TOKEN="${{ secrets.PORTAINER_API_KEY }}" + + STACK_ID=$(curl -s -H "Authorization: Bearer $PORTAINER_TOKEN" "$PORTAINER_API_URL/stacks" | jq -r ".[] | select(.Name==\"$STACK_NAME\") | .Id") + echo "STACK_ID=$STACK_ID" >> $GITHUB_ENV + + - name: Trigger stack update in Portainer + run: | + PORTAINER_API_URL="http://portainer:9000/api" + PORTAINER_TOKEN="${{ secrets.PORTAINER_API_KEY }}" + + curl -X POST "$PORTAINER_API_URL/stacks/$STACK_ID/redeploy" \ + -H "Authorization: Bearer $PORTAINER_TOKEN" \ + -H "Content-Type: application/json" \ + --data '{"pullImage": true}' + + +name: Deploy TournamentCaddie + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: self-hosted + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Log in to private registry + run: | + echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login reg.dev.nervesocket.com -u "${{ secrets.REGISTRY_USERNAME }}" --password-stdin + + - name: Build and push Docker image + run: | + IMAGE_TAG="reg.dev.nervesocket.com/tournamentcaddie:latest" + docker build -t $IMAGE_TAG . + docker push $IMAGE_TAG + + - name: Fetch stack ID from Portainer + run: | + STACK_NAME="TournamentCaddie" + PORTAINER_API_URL="http://portainer:9000/api" + PORTAINER_TOKEN="${{ secrets.PORTAINER_API_KEY }}" + + STACK_ID=$(curl -s -H "Authorization: Bearer $PORTAINER_TOKEN" "$PORTAINER_API_URL/stacks" | jq -r ".[] | select(.Name==\"$STACK_NAME\") | .Id") + + if [ -z "$STACK_ID" ]; then + echo "Error: Stack ID not found!" + exit 1 + fi + + echo "STACK_ID=$STACK_ID" >> $GITHUB_ENV + + - name: Fetch stack configuration + run: | + PORTAINER_API_URL="http://portainer:9000/api" + PORTAINER_TOKEN="${{ secrets.PORTAINER_API_KEY }}" + + # Get the stack details (including stack file content) + curl -s -H "Authorization: Bearer $PORTAINER_TOKEN" "$PORTAINER_API_URL/stacks/$STACK_ID/file" \ + | jq -r '.StackFileContent' > stack.yml + + - name: Redeploy stack in Portainer + run: | + PORTAINER_API_URL="http://portainer:9000/api" + PORTAINER_TOKEN="${{ secrets.PORTAINER_API_KEY }}" + + # Read stack file content + STACK_FILE_CONTENT=$(jq -Rs . < stack.yml) + + # Prepare JSON payload + JSON_PAYLOAD=$(jq -n --arg stackFileContent "$STACK_FILE_CONTENT" --argjson pullImage true \ + '{stackFileContent: $stackFileContent, pullImage: $pullImage}') + + # Update stack in Portainer (this redeploys it) + curl -X PUT "$PORTAINER_API_URL/stacks/$STACK_ID" \ + -H "Authorization: Bearer $PORTAINER_TOKEN" \ + -H "Content-Type: application/json" \ + --data "$JSON_PAYLOAD"