-
Notifications
You must be signed in to change notification settings - Fork 86
[5294] Git Integration in a DevOps Pipeline #7435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f1770e2
Add username and caCertificate fields to GitToken with migration
n-lark d32529c
Add generic Git backend for arbitrary HTTPS servers
n-lark c99cc43
Wire generic Git provider through dispatch and UI
n-lark ece6465
Merge remote-tracking branch 'origin/main' into 5294-generic-git
n-lark b84fcdf
Wire generic Git provider, fix types, fix vue warn for type prop
n-lark 9fc8247
Setup for CA certs
n-lark ef65384
Harden all git invocations against shell injection by switching to ex…
n-lark ee2e0a3
Store null and fix validator
n-lark 43da1e5
Fix migration file name
n-lark bacbba1
Add proxyquire as a dev dep for new specs to cover generic upload case
n-lark c63f9aa
Update stale copy for github/azure only
n-lark b150e53
Merge branch 'main' into 5294-generic-git
n-lark 6aafe8b
Merge branch 'main' into 5294-generic-git
n-lark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
forge/db/migrations/20260626-01-EE-extend-gittoken-generic.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| const { DataTypes } = require('sequelize') | ||
|
|
||
| module.exports = { | ||
| /** | ||
| * upgrade database | ||
| * @param {QueryInterface} context Sequelize.QueryInterface | ||
| */ | ||
| up: async (context, Sequelize) => { | ||
| await context.addColumn('GitTokens', 'username', { | ||
| type: DataTypes.STRING, | ||
| allowNull: true | ||
| }) | ||
| await context.addColumn('GitTokens', 'caCertificate', { | ||
| type: DataTypes.TEXT, | ||
| allowNull: true | ||
| }) | ||
| }, | ||
| down: async (context, Sequelize) => { } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| const { execFile } = require('node:child_process') | ||
| const { existsSync } = require('node:fs') | ||
| const fs = require('node:fs/promises') | ||
| const os = require('node:os') | ||
| const path = require('node:path') | ||
| const { promisify } = require('node:util') | ||
| const execFilePromised = promisify(execFile) | ||
|
|
||
| const { encryptValue, decryptValue } = require('../../../../db/utils') | ||
|
|
||
| const { cloneRepository } = require('./utils') | ||
|
|
||
| module.exports.init = async function (app) { | ||
| /** | ||
| * Push a snapshot to a git repository | ||
| * @param {Object} repoOptions | ||
| * @param {String} repoOptions.token | ||
| * @param {String} repoOptions.url | ||
| * @param {String} repoOptions.branch | ||
| * @param {String} repoOptions.username | ||
| * @param {Object} snapshot | ||
| * @param {Object} options | ||
| * @param {Object} options.sourceObject what produced the snapshot | ||
| * @param {Object} options.user who triggered the pipeline | ||
| * @param {Object} options.pipeline details of the pipeline | ||
| */ | ||
| async function pushToRepository (repoOptions, snapshot, options) { | ||
| let workingDir | ||
| let caFile | ||
| try { | ||
| const branch = repoOptions.branch || 'main' | ||
| if (!/^https:\/\//i.test(repoOptions.url)) { | ||
| throw new Error('Only HTTPS git URLs are supported') | ||
| } | ||
| const url = new URL(repoOptions.url) | ||
| url.username = repoOptions.username ? repoOptions.username : 'x-access-token' | ||
| url.password = repoOptions.token | ||
|
|
||
| workingDir = await fs.mkdtemp(path.join(os.tmpdir(), 'flowfuse-git-repo-')) | ||
|
|
||
| let gitEnv = process.env | ||
| if (repoOptions.caCertificate) { | ||
| caFile = `${workingDir}-ca.pem` | ||
| await fs.writeFile(caFile, repoOptions.caCertificate) | ||
| gitEnv = { ...process.env, GIT_SSL_CAINFO: caFile } | ||
| } | ||
|
|
||
| // 3. clone repo | ||
| await cloneRepository(url, branch, workingDir, gitEnv) | ||
|
|
||
| // 4. set username/email | ||
| await execFilePromised('git', ['config', 'user.email', 'no-reply@flowfuse.com'], { cwd: workingDir, env: gitEnv }) | ||
| await execFilePromised('git', ['config', 'user.name', 'FlowFuse'], { cwd: workingDir, env: gitEnv }) | ||
| // For local dev - disable gpg signing in case its set in global config | ||
| await execFilePromised('git', ['config', 'commit.gpgsign', 'false'], { cwd: workingDir, env: gitEnv }) | ||
|
|
||
| // 5. export snapshot | ||
| const exportOptions = { | ||
| credentialSecret: repoOptions.credentialSecret, | ||
| components: { | ||
| flows: true, | ||
| credentials: true | ||
| } | ||
| } | ||
| const result = await app.db.controllers.Snapshot.exportSnapshot(snapshot, exportOptions) | ||
| const snapshotExport = app.db.views.ProjectSnapshot.snapshotExport(result) | ||
| if (snapshotExport.settings?.settings?.palette?.npmrc) { | ||
| const enc = encryptValue(repoOptions.credentialSecret, snapshotExport.settings.settings?.palette?.npmrc) | ||
| snapshotExport.settings.settings.palette.npmrc = { $: enc } | ||
| } | ||
| const snapshotFile = path.join(workingDir, repoOptions.path || 'snapshot.json').replace(/"/g, '') | ||
| await fs.writeFile(snapshotFile, JSON.stringify(snapshotExport, null, 4)) | ||
|
|
||
| // 6. stage file | ||
| await execFilePromised('git', ['add', snapshotFile], { cwd: workingDir, env: gitEnv }) | ||
|
|
||
| // 7. commit | ||
| const commitMessage = `Update snapshot\n\nSnapshot updated by FlowFuse Pipeline '${options.pipeline.name}', triggered by ${options.user.username}` | ||
| await execFilePromised('git', ['commit', '-m', commitMessage], { cwd: workingDir, env: gitEnv }) | ||
|
|
||
| try { | ||
| // 8. push | ||
| await execFilePromised('git', ['push'], { cwd: workingDir, env: gitEnv }) | ||
| } catch (err) { | ||
| const output = err.stdout + err.stderr | ||
| if (/unable to access/.test(output)) { | ||
| const result = new Error('Permission denied') | ||
| result.code = 'invalid_token' | ||
| result.cause = err | ||
| throw result | ||
| } | ||
| let error | ||
| const m = /fatal: (.*)/.exec(output) | ||
| if (m) { | ||
| error = new Error('Failed to push repository: ' + m[1]) | ||
| } else { | ||
| error = Error('Failed to push repository') | ||
| } | ||
| error.cause = err | ||
| throw error | ||
| } | ||
| } finally { | ||
| if (workingDir) { | ||
| try { | ||
| await fs.rm(workingDir, { recursive: true, force: true }) | ||
| } catch (err) {} | ||
| } | ||
| if (caFile) { | ||
| try { | ||
| await fs.rm(caFile, { force: true }) | ||
| } catch (err) {} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Push a snapshot to a git repository | ||
| * @param {Object} repoOptions | ||
| * @param {String} repoOptions.token | ||
| * @param {String} repoOptions.url | ||
| * @param {String} repoOptions.branch | ||
| * @param {String} repoOptions.username | ||
| */ | ||
| async function pullFromRepository (repoOptions) { | ||
| let workingDir | ||
| let caFile | ||
| try { | ||
| const branch = repoOptions.branch || 'main' | ||
| if (!/^https:\/\//i.test(repoOptions.url)) { | ||
| throw new Error('Only HTTPS git URLs are supported') | ||
| } | ||
| const url = new URL(repoOptions.url) | ||
| url.username = repoOptions.username ? repoOptions.username : 'x-access-token' | ||
| url.password = repoOptions.token | ||
|
|
||
| workingDir = await fs.mkdtemp(path.join(os.tmpdir(), 'flowfuse-git-repo-')) | ||
|
|
||
| let gitEnv = process.env | ||
| if (repoOptions.caCertificate) { | ||
| caFile = `${workingDir}-ca.pem` | ||
| await fs.writeFile(caFile, repoOptions.caCertificate) | ||
| gitEnv = { ...process.env, GIT_SSL_CAINFO: caFile } | ||
| } | ||
|
|
||
| // 3. clone repo | ||
| await cloneRepository(url, branch, workingDir, gitEnv) | ||
|
|
||
| const snapshotFile = path.join(workingDir, repoOptions.path || 'snapshot.json').replace(/"/g, '') | ||
|
|
||
| if (!existsSync(snapshotFile)) { | ||
| throw new Error('Snapshot file not found in repository') | ||
| } | ||
|
|
||
| try { | ||
| const snapshotContent = await fs.readFile(snapshotFile, 'utf8') | ||
| const snapshot = JSON.parse(snapshotContent) | ||
| if (snapshot.settings?.env) { | ||
| const keys = Object.keys(snapshot.settings.env) | ||
| keys.forEach((key) => { | ||
| const env = snapshot.settings.env[key] | ||
| if (env.hidden && env.$) { | ||
| // Decrypt the value if it is encrypted | ||
| env.value = decryptValue(repoOptions.credentialSecret, env.$) | ||
| delete env.$ | ||
| } | ||
| }) | ||
| } | ||
| if (snapshot.settings?.settings?.palette?.npmrc) { | ||
| const npmrc = snapshot.settings.settings.palette.npmrc | ||
| if (typeof npmrc === 'object' && npmrc.$) { | ||
| snapshot.settings.settings.palette.npmrc = decryptValue(repoOptions.credentialSecret, npmrc.$) | ||
| } | ||
| } | ||
| return snapshot | ||
| } catch (err) { | ||
| throw new Error('Failed to read snapshot file: ' + err.message) | ||
| } | ||
| } finally { | ||
| if (workingDir) { | ||
| try { | ||
| await fs.rm(workingDir, { recursive: true, force: true }) | ||
| } catch (err) {} | ||
| } | ||
| if (caFile) { | ||
| try { | ||
| await fs.rm(caFile, { force: true }) | ||
| } catch (err) {} | ||
| } | ||
| } | ||
| } | ||
| return { | ||
| pushToRepository, | ||
| pullFromRepository | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.