diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index 6e1b50a..826f71e 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -33,25 +33,39 @@ jobs: run: | docker tag condado-newsletter:latest ${REGISTRY}/${IMAGE_NAME}:latest docker tag condado-newsletter:latest ${REGISTRY}/${IMAGE_NAME}:${{ github.sha }} - - # - name: Log in to Gitea container registry - # run: | - # if [ -z "$REGISTRY_USERNAME" ]; then - # REGISTRY_USERNAME="${{ github.actor }}" - # echo "REGISTRY_USERNAME secret is empty; falling back to github.actor: $REGISTRY_USERNAME" - # fi + - name: Deploy stack via Portainer API + env: + STACK_NAME: condado-newsletter + PORTAINER_URL: http://portainer.lab + PORTAINER_API_KEY: ${{ secrets.PORTAINER_API_KEY }} + PORTAINER_ENDPOINT_ID: ${{ secrets.PORTAINER_ENDPOINT_ID }} + run: | + STACK_ID=$(curl -sf \ + "${PORTAINER_URL}/api/stacks" \ + -H "X-API-Key: ${PORTAINER_API_KEY}" \ + | jq -r --arg stack_name "$STACK_NAME" '.[] | select(.Name == $stack_name) | .Id' \ + | head -n 1) - # if [ -z "$REGISTRY_PASSWORD" ]; then - # echo "REGISTRY_PASSWORD secret is empty; cannot log in to ${REGISTRY}." - # exit 1 - # fi + if [ -n "$STACK_ID" ]; then + PAYLOAD=$(jq -n \ + --rawfile stack_file docker-compose.prod.yml \ + '{StackFileContent: $stack_file, Env: [], Prune: false, PullImage: false}') - # echo "$REGISTRY_PASSWORD" | docker login "$REGISTRY" -u "$REGISTRY_USERNAME" --password-stdin + curl -sf -X PUT \ + "${PORTAINER_URL}/api/stacks/${STACK_ID}?endpointId=${PORTAINER_ENDPOINT_ID}" \ + -H "X-API-Key: ${PORTAINER_API_KEY}" \ + -H "Content-Type: application/json" \ + -d "$PAYLOAD" + else + PAYLOAD=$(jq -n \ + --arg name "$STACK_NAME" \ + --rawfile stack_file docker-compose.prod.yml \ + '{Name: $name, StackFileContent: $stack_file, Env: [], FromAppTemplate: false}') - # - name: Tag and push registry images - # run: | - # docker tag condado-newsletter:latest ${REGISTRY}/${IMAGE_NAME}:latest - # docker tag condado-newsletter:latest ${REGISTRY}/${IMAGE_NAME}:${{ github.sha }} - # docker push ${REGISTRY}/${IMAGE_NAME}:latest - # docker push ${REGISTRY}/${IMAGE_NAME}:${{ github.sha }} + curl -sf -X POST \ + "${PORTAINER_URL}/api/stacks/create/standalone/string?endpointId=${PORTAINER_ENDPOINT_ID}" \ + -H "X-API-Key: ${PORTAINER_API_KEY}" \ + -H "Content-Type: application/json" \ + -d "$PAYLOAD" + fi