Skip to content

Commit

Permalink
cleanup implementation and remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
dogfrogfog committed Nov 11, 2023
1 parent e4014b9 commit e70fd28
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 147 deletions.
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
52 changes: 28 additions & 24 deletions .github/workflows/import-dataset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ on:
vercel-project-name:
required: true
type: string
vercel-project-id:
required: true
type: string

jobs:
upload-data:
Expand All @@ -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 }}" \
Expand All @@ -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 }},
},
}'
17 changes: 12 additions & 5 deletions app/(frontend)/roll-out/github-api.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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,
},
}),
Expand Down
26 changes: 8 additions & 18 deletions app/(frontend)/roll-out/page.tsx
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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
}
};

Expand Down
42 changes: 0 additions & 42 deletions app/(frontend)/roll-out/sanity-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 4 additions & 4 deletions app/(frontend)/roll-out/vercel-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions app/api/sanity-deploy/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
50 changes: 0 additions & 50 deletions test-curl-to-add-webhook.js

This file was deleted.

0 comments on commit e70fd28

Please sign in to comment.