75 lines
3.1 KiB
YAML
75 lines
3.1 KiB
YAML
# https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions
|
|
name: Pull Images and Redeploy
|
|
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
|
|
on: [push]
|
|
|
|
jobs:
|
|
Update-and-Redeploy-Stack:
|
|
runs-on: synology
|
|
steps:
|
|
- name: Prefetch Docker Image(s)
|
|
if: contains(github.event.head_commit.message, 'run_redeploy')
|
|
run: |
|
|
docker pull linuxserver/bookstack #optional? probably faster if done first...
|
|
|
|
- name: Fetch stack ID from Portainer
|
|
if: contains(github.event.head_commit.message, 'run_redeploy')
|
|
env:
|
|
STACK_NAME: bookstack
|
|
PORTAINER_TOKEN: ${{ secrets.PORTAINER_TOKEN }}
|
|
PORTAINER_API_URL: https://portainer.dev.nervesocket.com/api
|
|
run: |
|
|
CURL=$(curl -s -H "X-API-Key: $PORTAINER_TOKEN" "$PORTAINER_API_URL/stacks")
|
|
echo $CURL
|
|
STACK_DATA=$(curl -s -H "X-API-Key: $PORTAINER_TOKEN" "$PORTAINER_API_URL/stacks" | jq -r ".[] | select(.Name==\"$STACK_NAME\") | {Id, EndpointId}")
|
|
|
|
STACK_ID=$(echo "$STACK_DATA" | jq -r ".Id")
|
|
ENDPOINT_ID=$(echo "$STACK_DATA" | jq -r ".EndpointId")
|
|
echo "STACK_ID=$STACK_ID" >> $GITHUB_ENV
|
|
echo "ENDPOINT_ID=$ENDPOINT_ID" >> $GITHUB_ENV
|
|
echo "Got stack ID: $STACK_ID and Endpoint ID: $ENDPOINT_ID"
|
|
|
|
- name: Fetch stack configuration
|
|
if: contains(github.event.head_commit.message, 'run_redeploy')
|
|
env:
|
|
STACK_NAME: bookstack
|
|
PORTAINER_TOKEN: ${{ secrets.PORTAINER_TOKEN }}
|
|
PORTAINER_API_URL: https://portainer.dev.nervesocket.com/api
|
|
run: |
|
|
# Get the stack details (including stack file content)
|
|
curl -s -H "X-API-Key: $PORTAINER_TOKEN" "$PORTAINER_API_URL/stacks/$STACK_ID/file" \
|
|
| jq -r '.StackFileContent' > stack.yml
|
|
|
|
echo "Fetched stack configuration and saved it to stack.yml"
|
|
|
|
- name: Redeploy stack in Portainer
|
|
if: contains(github.event.head_commit.message, 'run_redeploy')
|
|
env:
|
|
STACK_NAME: bookstack
|
|
PORTAINER_TOKEN: ${{ secrets.PORTAINER_TOKEN }}
|
|
PORTAINER_API_URL: https://portainer.dev.nervesocket.com/api
|
|
run: |
|
|
# Read stack file content
|
|
# STACK_FILE_CONTENT=$(jq -Rs . < stack.yml)
|
|
STACK_FILE_CONTENT=$(echo "$(<stack.yml )")
|
|
|
|
|
|
# Prepare JSON payload
|
|
JSON_PAYLOAD=$(jq -n --arg stackFileContent "$STACK_FILE_CONTENT" --argjson pullImage true \
|
|
'{stackFileContent: $stackFileContent, pullImage: $pullImage}')
|
|
|
|
echo "About to push the following JSON payload:"
|
|
echo $JSON_PAYLOAD
|
|
|
|
# Update stack in Portainer (this redeploys it)
|
|
DEPLOY_RESPONSE=$(curl -X PUT "$PORTAINER_API_URL/stacks/$STACK_ID?endpointId=$ENDPOINT_ID" \
|
|
-H "X-API-Key: $PORTAINER_TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
--data "$JSON_PAYLOAD")
|
|
|
|
echo "Redeployed stack in Portainer. Response:"
|
|
echo $DEPLOY_RESPONSE
|
|
|
|
- name: Status check
|
|
run: |
|
|
echo "📋 This job's status is ${{ job.status }}." |