Aller au contenu

Sauvegarde Gitea

Découvrir l’introspection par l’API n8n et des “workflows” complexes

Le “workflow” doit réaliser à interval régulière (quotidien) la sauvegarde de tous les workflows vers Gitea.

Le script nettoie les fichiers temporaires et réalise 2 tâches en parallèle : la récupération du dépôt distant et un vidage par API d’introspection de tous les workflows en JSON. Les données sont ensuite copiées dans le dépôt git précédemment tiré afin de tester ensuite si des modifications sont encore à l’état “staged”. Un boucle permet de s’assurer que les modification sont bien “staged” et un commit est ensuite réalisé si nécessaire.

les informations suivantes sont envoyées par courriel après sauvegarde vers Git de l’ensemble des workflows :

  • Hash du dernier commit
  • Liste des fichiers sauvegardés
Sauvegarde Gitea
{
  "name": "Sauvegarde Gitea",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {}
          ]
        }
      },
      "id": "99853d4c-6cd9-4632-b2e7-8477b61a927e",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -720,
        160
      ]
    },
    {
      "parameters": {
        "filters": {},
        "requestOptions": {}
      },
      "id": "53eab512-36c6-4b94-90d4-01ea91065bcb",
      "name": "n8n",
      "type": "n8n-nodes-base.n8n",
      "typeVersion": 1,
      "position": [
        -520,
        240
      ],
      "credentials": {
        "n8nApi": {
          "id": "LPFILq2KSvbWK8vk",
          "name": "n8n account"
        }
      }
    },
    {
      "parameters": {
        "operation": "toJson",
        "mode": "each",
        "options": {
          "fileName": "={{ $json.name }}"
        }
      },
      "id": "8640e0b6-dc5a-45d2-8fab-6d2713e4b896",
      "name": "Convert to File",
      "type": "n8n-nodes-base.convertToFile",
      "typeVersion": 1.1,
      "position": [
        -380,
        240
      ]
    },
    {
      "parameters": {
        "operation": "write",
        "fileName": "=/tmp/comp/{{ $binary.data.fileName }}.json",
        "options": {}
      },
      "id": "d1908c71-5bfe-43fc-a109-7dc79bd844b4",
      "name": "Read/Write Files from Disk",
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1,
      "position": [
        -220,
        240
      ]
    },
    {
      "parameters": {
        "command": "mkdir -p /tmp/comp && rm -rf /tmp/repo /tmp/comp/* "
      },
      "id": "5645af60-d3af-459d-9e3f-32429ff63b4e",
      "name": "Execute Command1",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        -520,
        80
      ]
    },
    {
      "parameters": {
        "authentication": "gitPassword",
        "operation": "clone",
        "repositoryPath": "/tmp/repo",
        "sourceRepository": "https://gitea.ia86.cc/perso/n8n-sauvegarde"
      },
      "id": "df61e886-fc5c-4f04-ac03-325c3534c22a",
      "name": "Git",
      "type": "n8n-nodes-base.git",
      "typeVersion": 1,
      "position": [
        -220,
        80
      ],
      "credentials": {
        "gitPassword": {
          "id": "7JHhvGSTGUGgQb6S",
          "name": "gitea.ia86.cc account"
        }
      }
    },
    {
      "parameters": {
        "mode": "chooseBranch",
        "output": "empty"
      },
      "id": "7da55c97-6b1e-425b-ad42-9221e2db8869",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 2.1,
      "position": [
        -40,
        160
      ]
    },
    {
      "parameters": {
        "operation": "status",
        "repositoryPath": "/tmp/repo"
      },
      "id": "08ff2375-45f6-4d20-8e27-c1cc3743f0a0",
      "name": "Git1",
      "type": "n8n-nodes-base.git",
      "typeVersion": 1,
      "position": [
        240,
        160
      ]
    },
    {
      "parameters": {
        "operation": "add",
        "repositoryPath": "/tmp/repo",
        "pathsToAdd": "*"
      },
      "id": "f3efec1e-b45a-40b6-b233-2fbee5170890",
      "name": "Git2",
      "type": "n8n-nodes-base.git",
      "typeVersion": 1,
      "position": [
        560,
        340
      ]
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "options": {}
      },
      "id": "08ea4b96-dbb7-4e67-9c83-5e9413b886f4",
      "name": "Aggregate",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        560,
        160
      ]
    },
    {
      "parameters": {
        "errorMessage": "ERREUR DANS GIT"
      },
      "id": "f87a3c10-0adf-4004-8bf9-455305556c36",
      "name": "Stop and Error",
      "type": "n8n-nodes-base.stopAndError",
      "typeVersion": 1,
      "position": [
        560,
        0
      ]
    },
    {
      "parameters": {
        "operation": "commit",
        "repositoryPath": "/tmp/repo",
        "message": "=auto: {{ $json.currentDate }}",
        "options": {}
      },
      "id": "d08883f3-bb02-469e-bb68-b123e6048413",
      "name": "Git3",
      "type": "n8n-nodes-base.git",
      "typeVersion": 1,
      "position": [
        1120,
        160
      ]
    },
    {
      "parameters": {
        "operation": "status",
        "repositoryPath": "/tmp/repo"
      },
      "id": "fc6cf06a-5ac9-4bb2-a058-5f7744c2a2af",
      "name": "Git4",
      "type": "n8n-nodes-base.git",
      "typeVersion": 1,
      "position": [
        1260,
        160
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "f62f6c9f-3ef3-4b85-bfca-ae123526cbcc",
              "leftValue": "={{ $json.ahead }}",
              "rightValue": 1,
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "e2bb2b3c-93ea-4475-9b10-3c65dc464464",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1400,
        160
      ]
    },
    {
      "parameters": {
        "operation": "push",
        "repositoryPath": "/tmp/repo",
        "options": {}
      },
      "id": "84745a3f-a552-47cd-a671-6f9bb8e12565",
      "name": "Git5",
      "type": "n8n-nodes-base.git",
      "typeVersion": 1,
      "position": [
        1560,
        40
      ]
    },
    {
      "parameters": {},
      "id": "8b718e16-64e2-4c47-a5ce-ec09bf60ac4d",
      "name": "No Operation, do nothing",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1560,
        280
      ]
    },
    {
      "parameters": {
        "fromEmail": "nico@ia86.cc",
        "toEmail": "nicolas@palon.fr",
        "subject": "=[N8N-SAVE]: {{ $json.date }} - {{ $('Git1').item.json.files.length }} modifs",
        "html": "={{ $('ListFiles').item.json.stdout }}<br>\nHash: {{ $json.hash }}",
        "options": {
          "replyTo": "noreply@n8n.ia86.cc"
        }
      },
      "id": "e9a821a8-0018-4363-93c9-6f2251de3bec",
      "name": "Send Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        2120,
        40
      ],
      "credentials": {
        "smtp": {
          "id": "d4ZAN48vF9GTpVON",
          "name": "SMTP account"
        }
      }
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "1facf71d-e3ea-47bc-b5fd-eb537ff32ebf",
      "name": "DateTime",
      "type": "n8n-nodes-base.dateTime",
      "typeVersion": 2,
      "position": [
        980,
        160
      ]
    },
    {
      "parameters": {
        "repositoryPath": "/tmp/repo",
        "options": {}
      },
      "id": "18dc9799-fea1-4e42-a5eb-16ca5c9a289e",
      "name": "Git6",
      "type": "n8n-nodes-base.git",
      "typeVersion": 1,
      "position": [
        1700,
        40
      ]
    },
    {
      "parameters": {
        "sortFieldsUi": {
          "sortField": [
            {
              "fieldName": "date",
              "order": "descending"
            }
          ]
        },
        "options": {}
      },
      "id": "4e76c8a4-db9a-449a-8df1-de12814654b3",
      "name": "Sort",
      "type": "n8n-nodes-base.sort",
      "typeVersion": 1,
      "position": [
        1840,
        40
      ]
    },
    {
      "parameters": {
        "keep": "lastItems"
      },
      "id": "cc455ab0-bfc1-437c-a9f2-e5a49dbc9a93",
      "name": "Limit",
      "type": "n8n-nodes-base.limit",
      "typeVersion": 1,
      "position": [
        1980,
        40
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.staged.length }}",
                    "rightValue": 0,
                    "operator": {
                      "type": "number",
                      "operation": "gt"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "staged"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "ce17ff24-9901-4413-b722-5af58c23ae75",
                    "leftValue": "={{ $json.conflicted.length }}",
                    "rightValue": 0,
                    "operator": {
                      "type": "number",
                      "operation": "gt"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "conflicted"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "007665fd-3964-405b-a211-8b5cf45fca1c",
                    "leftValue": "={{ $json.created.length }}",
                    "rightValue": 0,
                    "operator": {
                      "type": "number",
                      "operation": "gt"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "created"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "4eb5a697-cf77-4962-a9d3-c016ec13ca61",
                    "leftValue": "={{ $json.deleted.length }}",
                    "rightValue": 0,
                    "operator": {
                      "type": "number",
                      "operation": "gt"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "deleted"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "e567e8c9-98df-463b-9df4-deffcab85d8a",
                    "leftValue": "={{ $json.modified.length }}",
                    "rightValue": 0,
                    "operator": {
                      "type": "number",
                      "operation": "gt"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "modifled"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "75f6edd0-8245-4cae-815c-b394776c6258",
                    "leftValue": "={{ $json.renamed.length }}",
                    "rightValue": 0,
                    "operator": {
                      "type": "number",
                      "operation": "gt"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "renamed"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "30f3c8d2-a595-4e0b-a25f-b6f75a2d4061",
                    "leftValue": "={{ $json.not_added.length }}",
                    "rightValue": 0,
                    "operator": {
                      "type": "number",
                      "operation": "gt"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "not_added"
            },
            {
              "renameOutput": true,
              "outputKey": "RIEN"
            }
          ]
        },
        "options": {}
      },
      "id": "1134c729-2994-4230-ab71-e24fb094c550",
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [
        380,
        120
      ]
    },
    {
      "parameters": {
        "command": "cp /tmp/comp/* /tmp/repo && cd /tmp/repo && echo -en \"# Sauvegarde<br>\" > ./README.md && ls *.json|sed ':a;N;$!ba;s/\\n/<br>/g' >> ./README.md && cat ./README.md"
      },
      "id": "0b343682-5b1e-423a-ae0f-50910c9f5729",
      "name": "ListFiles",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        100,
        160
      ]
    },
    {
      "parameters": {
        "operation": "addConfig",
        "repositoryPath": "/tmp/repo",
        "key": "user.email",
        "value": "noreply@n8n.ia86.cc",
        "options": {}
      },
      "id": "2ffc6f60-87d8-4341-aa4a-52e8bc578334",
      "name": "Git7",
      "type": "n8n-nodes-base.git",
      "typeVersion": 1,
      "position": [
        700,
        160
      ]
    },
    {
      "parameters": {
        "operation": "addConfig",
        "repositoryPath": "/tmp/repo",
        "key": "user.name",
        "value": "Nicolas Hordé",
        "options": {}
      },
      "id": "6c316384-3a81-4478-9688-8e09b9257618",
      "name": "Git8",
      "type": "n8n-nodes-base.git",
      "typeVersion": 1,
      "position": [
        840,
        160
      ]
    }
  ],
  "pinData": {},
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Execute Command1",
            "type": "main",
            "index": 0
          },
          {
            "node": "n8n",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "n8n": {
      "main": [
        [
          {
            "node": "Convert to File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert to File": {
      "main": [
        [
          {
            "node": "Read/Write Files from Disk",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Command1": {
      "main": [
        [
          {
            "node": "Git",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Git": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read/Write Files from Disk": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "ListFiles",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Git1": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Git2": {
      "main": [
        [
          {
            "node": "Git1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate": {
      "main": [
        [
          {
            "node": "Git7",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Git3": {
      "main": [
        [
          {
            "node": "Git4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Git4": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Git5",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Operation, do nothing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "DateTime": {
      "main": [
        [
          {
            "node": "Git3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Git6": {
      "main": [
        [
          {
            "node": "Sort",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Git5": {
      "main": [
        [
          {
            "node": "Git6",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sort": {
      "main": [
        [
          {
            "node": "Limit",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Limit": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Stop and Error",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Git2",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Git2",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Git2",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Git2",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Git2",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Operation, do nothing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ListFiles": {
      "main": [
        [
          {
            "node": "Git1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Git7": {
      "main": [
        [
          {
            "node": "Git8",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Git8": {
      "main": [
        [
          {
            "node": "DateTime",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "2d60742d-7e58-4cde-bfdb-8d3b571c081b",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "366d92472788798a2a8c91dff5608f9b248c880970d12dcd224edfaeeb4a3df8"
  },
  "id": "kZvM4K7vwhKvtIkw",
  "tags": []
}