diff --git a/.env.example b/.env.example index 0d543c6..020c594 100644 --- a/.env.example +++ b/.env.example @@ -6,8 +6,8 @@ NEXT_PUBLIC_SANITY_DATASET= // roll-out feature HOST= -GITHUB_REPO_ID= -GITHUB_REPO_PRODUCTION_BRANCH= +TEAM_GITHUB_REPO_ID= +TEAM_GITHUB_REPO_PRODUCTION_BRANCH= REPO_TYPE= SANITY_ORGANIZATION_ID= diff --git a/.github/workflows/import-dataset.yml b/.github/workflows/import-dataset.yml index 87e09a5..8d855e1 100644 --- a/.github/workflows/import-dataset.yml +++ b/.github/workflows/import-dataset.yml @@ -12,6 +12,9 @@ on: vercel-project-name: required: true type: string + vercel-project-id: + required: true + type: string jobs: upload-data: @@ -22,14 +25,16 @@ jobs: name: Upload data steps: - uses: actions/checkout@v4 - # - name: Install dependencies - # run: npm install - # - name: Export dataset - # run: | - # SANITY_AUTH_TOKEN="${{ secrets.SANITY_AUTH_TOKEN }}" \ - # npx sanity dataset import \ - # prod-copy.tar.gz production - - name: Curl to add vercel deploy hook to sanity + - name: Install dependencies + run: npm install + - name: Export dataset + run: | + SANITY_AUTH_TOKEN="${{ secrets.SANITY_AUTH_TOKEN }}" \ + npx sanity dataset import \ + prod-copy.tar.gz production + # curls commands should be run after dataset is uploaded + # to make sure initial production deployment has correct data + - name: Curl command to add vercel deploy hook to sanity run: | curl -X POST "https://api.sanity.io/v2021-10-04/hooks/projects/${{ inputs.sanity-project-id }}" \ -H "Authorization: Bearer ${{ secrets.SANITY_AUTH_TOKEN }}" \ @@ -49,19 +54,18 @@ jobs: "Authorization": "Bearer ${{ secrets.VERCEL_PERSONAL_AUTH_TOKEN }}" } }' - # - name: curl - # uses: enflo/curl-action@master - # with: - # curl: ${{ CURL ARGUMENTS }} - - # # correct CLI command (env sanity project should be with empty dataset): - # # sanity dataset import prod-copy.tar.gz production - # args: . dataset import prod-copy.tar.gz production - # - name: Add sanity webhook - # run: ls - - # working code - # run: | - # SANITY_AUTH_TOKEN="${{ secrets.SANITY_AUTH_TOKEN }}" \ - # npx sanity dataset import \ - # prod-copy.tar.gz production + - name: Curl command to triger vercel initial deployment + run: | + curl -X POST "https://api.vercel.com/v13/deployments?teamId=${{ secrets.VERCEL_FR_TEAM_ID }}" \ + -H "Authorization: Bearer ${{ secrets.VERCEL_PERSONAL_AUTH_TOKEN }}" \ + -H "Content-Type: application/json" \ + -d '{ + "name": "${{ inputs.vercel-project-name }}", + "project": "${{ inputs.vercel-project-id }}", + "target": "production", + "gitSource": { + "repoId": ${{ secrets.TEAM_GITHUB_REPO_ID }}, + "ref": ${{ secrets.TEAM_GITHUB_REPO_PRODUCTION_BRANCH }}, + "type": ${{ secrets.REPO_TYPE }}, + }, + }' diff --git a/app/(frontend)/roll-out/github-api.ts b/app/(frontend)/roll-out/github-api.ts index 04c60a5..a69e4d4 100644 --- a/app/(frontend)/roll-out/github-api.ts +++ b/app/(frontend)/roll-out/github-api.ts @@ -1,8 +1,14 @@ -export async function trigerWorkflowtoFillDataset( - sanityProjectId: string, - sanityProjectDataset: string, - vercelProjectName: string, -) { +export async function trigerWorkflowtoFillDataset({ + sanityProjectId, + sanityProjectDataset, + vercelProjectId, + vercelProjectName, +}: { + sanityProjectId: string; + sanityProjectDataset: string; + vercelProjectId: string; + vercelProjectName: string; +}) { try { console.log('triger github workflow'); await fetch( @@ -18,6 +24,7 @@ export async function trigerWorkflowtoFillDataset( inputs: { 'sanity-project-id': sanityProjectId, 'sanity-project-dataset': sanityProjectDataset, + 'vercel-project-id': vercelProjectId, 'vercel-project-name': vercelProjectName, }, }), diff --git a/app/(frontend)/roll-out/page.tsx b/app/(frontend)/roll-out/page.tsx index 3f0f93d..18191a1 100644 --- a/app/(frontend)/roll-out/page.tsx +++ b/app/(frontend)/roll-out/page.tsx @@ -1,14 +1,8 @@ import { styled } from '@linaria/react'; -import { - createSanityProject, - createCorsEntry, - addUserEmailToMembers, - // createDocumentWebhook, - createDataset, -} from './sanity-api'; +import { createSanityProject, createCorsEntry, addUserEmailToMembers, createDataset } from './sanity-api'; import { trigerWorkflowtoFillDataset } from './github-api'; -import { createVercelProject, addVercelProjectEnvs, createVercelProjectDeployment } from './vercel-api'; +import { createVercelProject, addVercelProjectEnvs } from './vercel-api'; export default function RollOutPage() { const triggerDeploy = async (data: FormData) => { @@ -19,7 +13,6 @@ export default function RollOutPage() { const sanityProjectId = await createSanityProject(cleanUsername); - // dataset should be created before vercel deployment if (sanityProjectId) { const projectData = await createVercelProject({ username: cleanUsername, @@ -30,10 +23,13 @@ export default function RollOutPage() { if (projectData) { await addVercelProjectEnvs(projectData); await Promise.all([ - createVercelProjectDeployment(projectData), createDataset(sanityProjectId, 'production'), - // wdwd - trigerWorkflowtoFillDataset(sanityProjectId, 'production', projectData.projectName), + trigerWorkflowtoFillDataset({ + sanityProjectId, + sanityProjectDataset: 'production', + vercelProjectId: projectData.projectId, + vercelProjectName: projectData.projectName, + }), createCorsEntry({ projectId: sanityProjectId, deploymentUrl: projectData.deploymentUrl, @@ -42,14 +38,8 @@ export default function RollOutPage() { projectId: sanityProjectId, email, }), - // createDocumentWebhook({ - // sanityProjectId: sanityProjectId, - // vercelProjectName: projectData.projectName, - // }), ]); } - - // todo: send notification to slack chennel that project is created for user with email: real user email } }; diff --git a/app/(frontend)/roll-out/sanity-api.ts b/app/(frontend)/roll-out/sanity-api.ts index fd89afa..5a74f09 100644 --- a/app/(frontend)/roll-out/sanity-api.ts +++ b/app/(frontend)/roll-out/sanity-api.ts @@ -97,45 +97,3 @@ export async function addUserEmailToMembers({ projectId, email }: { projectId: s console.log('error adding member :('); } } - -export async function createDocumentWebhook({ - sanityProjectId, - vercelProjectName, -}: { - sanityProjectId: string; - vercelProjectName: string; -}) { - console.log('adding webhook 🏎'); - - try { - const result = await fetch(`https://api.sanity.io/v2021-10-04/hooks/projects/${sanityProjectId}`, { - method: 'POST', - headers: { - Authorization: `Bearer ${process.env.SANITY_PERSONAL_AUTH_TOKEN}`, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - type: 'document', - name: 'Sanity Studio', - url: `https://${vercelProjectName}.vercel.app/api/sanity-deploy`, - httpMethod: 'POST', - apiVersion: 'v2021-03-25', - includeDrafts: false, - dataset: '*', - rule: { - on: ['create', 'update', 'delete'], - }, - headers: { - Authorization: `Bearer ${process.env.VERCEL_PERSONAL_AUTH_TOKEN}`, - }, - }), - }); - - const data = await result.json(); - - console.log('webhook added ✅✅✅'); - // console.log(data); - } catch (e) { - console.log(e); - } -} diff --git a/app/(frontend)/roll-out/vercel-api.ts b/app/(frontend)/roll-out/vercel-api.ts index d0a94db..9e8d44e 100644 --- a/app/(frontend)/roll-out/vercel-api.ts +++ b/app/(frontend)/roll-out/vercel-api.ts @@ -29,13 +29,13 @@ export async function createVercelProject({ sanityProjectId, sanityDatasetName, type: 'encrypted', }, { - key: 'GITHUB_REPO_ID', + key: 'TEAM_GITHUB_REPO_ID', value: '684968839', target: ['production', 'preview', 'development'], type: 'encrypted', }, { - key: 'GITHUB_REPO_PRODUCTION_BRANCH', + key: 'TEAM_GITHUB_REPO_PRODUCTION_BRANCH', value: 'auto-roll-out', target: ['production', 'preview', 'development'], type: 'encrypted', @@ -326,8 +326,8 @@ export const createVercelProjectDeployment = async ({ name: projectName, project: projectId, gitSource: { - repoId: process.env.GITHUB_REPO_ID, - ref: process.env.GITHUB_REPO_PRODUCTION_BRANCH, + repoId: process.env.TEAM_GITHUB_REPO_ID, + ref: process.env.TEAM_GITHUB_REPO_PRODUCTION_BRANCH, type: process.env.REPO_TYPE, }, target: 'production', diff --git a/app/api/sanity-deploy/route.ts b/app/api/sanity-deploy/route.ts index cf75f05..7280583 100644 --- a/app/api/sanity-deploy/route.ts +++ b/app/api/sanity-deploy/route.ts @@ -11,9 +11,9 @@ export async function POST() { const deploymentData = { projectId: process.env.VERCEL_PROJECT_ID || '', projectName: process.env.VERCEL_PROJECT_NAME || '', - repoId: Number(process.env.GITHUB_REPO_ID), + repoId: Number(process.env.TEAM_GITHUB_REPO_ID), type: process.env.REPO_TYPE || '', - productionBranch: process.env.GITHUB_REPO_PRODUCTION_BRANCH || '', + productionBranch: process.env.TEAM_GITHUB_REPO_PRODUCTION_BRANCH || '', }; if (token === process.env.VERCEL_PERSONAL_AUTH_TOKEN) { diff --git a/test-curl-to-add-webhook.js b/test-curl-to-add-webhook.js deleted file mode 100644 index b9507df..0000000 --- a/test-curl-to-add-webhook.js +++ /dev/null @@ -1,50 +0,0 @@ -curl \ - 'https://api.sanity.io/v2021-10-04/hooks/projects/${projectId}' \ - --request POST \ - -H 'Authorization: Bearer ' \ - -H 'Content-Type: application/json' \ -{ - // body: JSON.stringify({ - // type: 'document', - // name: 'Sanity Studio', - // url: `https://${vercelProjectName}.vercel.app/api/sanity-deploy`, - // httpMethod: 'POST', - // apiVersion: 'v2021-03-25', - // includeDrafts: false, - // dataset: '*', - // rule: { - // on: ['create', 'update', 'delete'], - // }, - // headers: { - // Authorization: `Bearer ${process.env.VERCEL_PERSONAL_AUTH_TOKEN}`, - // }, - // }), - -} -EOF - - - - -// curl -X POST "https://api.sanity.io/v2021-10-04/hooks/projects/${projectId}" \ -// -H "Authorization: Bearer " \ -// -H "Content-Type: application/json" \ - -// -d '{ -// "type":"document", -// "name": "Sanity Studio" -// // vercel-project-name -// "url": `https://${vercelProjectName}.vercel.app/api/sanity-deploy`, -// "httpMethod":"POST", -// "apiVersion":"v2021-03-25", -// "includeDrafts":false, -// "dataset":"*", -// "rule": { -// "on": ['create', 'update', 'delete'], -// }, -// "description":"description", -// "headers":{}, -// "headers": { -// "Authorization": `Bearer ${process.env.VERCEL_PERSONAL_AUTH_TOKEN}`, -// }, -// }' \ No newline at end of file