diff --git a/.drone.yml b/.drone.yml index 3470ba5b8..b4450b57a 100644 --- a/.drone.yml +++ b/.drone.yml @@ -144,10 +144,10 @@ steps: from_secret: netlify_auth_token NETLIFY_SITE_ID: from_secret: netlify_site_id + GITEA_TOKEN: + from_secret: gitea_token commands: - - ./node_modules/.bin/netlify link --id $NETLIFY_SITE_ID - - ./node_modules/.bin/netlify deploy - - env + - node ./scripts/deploy-preview-netlify.js depends_on: - build-prod diff --git a/package.json b/package.json index a4c58617f..29058ea20 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "rollup": "2.59.0", "rollup-plugin-visualizer": "5.5.2", "sass": "1.43.4", + "slugify": "^1.6.2", "ts-jest": "27.0.7", "typescript": "4.4.4", "vite": "2.6.14", diff --git a/scripts/deploy-preview-netlify.js b/scripts/deploy-preview-netlify.js new file mode 100644 index 000000000..3b2554bff --- /dev/null +++ b/scripts/deploy-preview-netlify.js @@ -0,0 +1,54 @@ +const slugify = require('slugify') +const {exec} = require('child_process') +const axios = require('axios') + +const giteaToken = process.env.GITEA_TOKEN +const siteId = process.env.NETLIFY_SITE_ID +const branchSlug = slugify(process.env.DRONE_SOURCE_BRANCH) +const prNumber = process.env.DRONE_PULL_REQUEST + +const prIssueUrl = `https://kolaente.dev/api/v1/repos/vikunja/frontend/issues/${prNumber}/comments` +const alias = `${prNumber}-${branchSlug}` +const fullPreviewUrl = `https://${alias}--vikunja-frontend-preview.netlify.app` + +const promiseExec = cmd => { + return new Promise((resolve, reject) => { + exec(cmd, (error, stdout, stderr) => { + if (error) { + reject(error) + return + } + + resolve(stdout) + }) + }) +} + +(async function() { + let stdout = await promiseExec(`./node_modules/.bin/netlify link --id ${siteId}`) + console.log(stdout) + stdout = await promiseExec(`./node_modules/.bin/netlify deploy --alias ${alias}`) + console.log(stdout) + + await axios.post(prIssueUrl, { + body: ` +Hi! + +I've deployed the changes of this PR on a preview environment under this URL: ${fullPreviewUrl} + +You can use this url to view the changes live and test them out. +You will need to manually connect this to an api running somehwere. The easiest to use is https://try.vikunja.io/. + +Have a nice day! + +> Beep boop, I'm a bot. +`, + }, { + headers: { + 'Content-Type': 'application/json', + 'accept': 'application/json', + 'Authorization': `token ${giteaToken}`, + }, + }) + .catch(err => console.log('Error sending comment:', err)) +})() \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 42a9c146f..9b7e14861 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11849,6 +11849,11 @@ slice-ansi@^4.0.0: astral-regex "^2.0.0" is-fullwidth-code-point "^3.0.0" +slugify@^1.6.2: + version "1.6.2" + resolved "https://registry.yarnpkg.com/slugify/-/slugify-1.6.2.tgz#4cb97220a2278e1b86fb017c6da668e6a865354c" + integrity sha512-XMtI8qD84LwCpthLMBHlIhcrj10cgA+U/Ot8G6FD6uFuWZtMfKK75JO7l81nzpFJsPlsW6LT+VKqWQJW3+6New== + snake-case@3.0.4: version "3.0.4" resolved "https://registry.yarnpkg.com/snake-case/-/snake-case-3.0.4.tgz#4f2bbd568e9935abdfd593f34c691dadb49c452c"