|
| 1 | +/** |
| 2 | + * GitLab CI/CD Pipeline Generator |
| 3 | + * Generate CI/CD pipelines for GitLab |
| 4 | + */ |
| 5 | + |
| 6 | +export interface GitLabCIOptions { |
| 7 | + stages?: string[] |
| 8 | + environments?: string[] |
| 9 | + awsRegion?: string |
| 10 | + dockerImage?: string |
| 11 | + bunVersion?: string |
| 12 | + deployCommand?: string |
| 13 | + testCommand?: string |
| 14 | + buildCommand?: string |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Generate deployment pipeline |
| 19 | + */ |
| 20 | +export function generateDeploymentPipeline(options: GitLabCIOptions = {}): string { |
| 21 | + const { |
| 22 | + stages = ['test', 'build', 'deploy'], |
| 23 | + environments = ['production'], |
| 24 | + awsRegion = 'us-east-1', |
| 25 | + dockerImage = 'oven/bun:latest', |
| 26 | + deployCommand = 'bun run cloud deploy', |
| 27 | + testCommand = 'bun test', |
| 28 | + buildCommand = 'bun run build', |
| 29 | + } = options |
| 30 | + |
| 31 | + return `stages: |
| 32 | +${stages.map(s => ` - ${s}`).join('\n')} |
| 33 | +
|
| 34 | +variables: |
| 35 | + AWS_DEFAULT_REGION: ${awsRegion} |
| 36 | + AWS_REGION: ${awsRegion} |
| 37 | +
|
| 38 | +default: |
| 39 | + image: ${dockerImage} |
| 40 | + cache: |
| 41 | + paths: |
| 42 | + - node_modules/ |
| 43 | + - .bun/ |
| 44 | +
|
| 45 | +before_script: |
| 46 | + - bun install |
| 47 | +
|
| 48 | +test: |
| 49 | + stage: test |
| 50 | + script: |
| 51 | + - ${testCommand} |
| 52 | + only: |
| 53 | + - merge_requests |
| 54 | + - main |
| 55 | +
|
| 56 | +build: |
| 57 | + stage: build |
| 58 | + script: |
| 59 | + - ${buildCommand} |
| 60 | + artifacts: |
| 61 | + paths: |
| 62 | + - dist/ |
| 63 | + expire_in: 1 day |
| 64 | + only: |
| 65 | + - merge_requests |
| 66 | + - main |
| 67 | +
|
| 68 | +deploy: |
| 69 | + stage: deploy |
| 70 | + script: |
| 71 | + - ${deployCommand} |
| 72 | + environment: |
| 73 | + name: ${environments[0]} |
| 74 | + url: https://\${CI_PROJECT_NAME}.example.com |
| 75 | + only: |
| 76 | + - main |
| 77 | + when: manual |
| 78 | +` |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Generate multi-environment pipeline |
| 83 | + */ |
| 84 | +export function generateMultiEnvPipeline(options: { |
| 85 | + environments: Array<{ name: string; branch: string; manual?: boolean }> |
| 86 | + awsRegion?: string |
| 87 | +}): string { |
| 88 | + const { environments, awsRegion = 'us-east-1' } = options |
| 89 | + |
| 90 | + const deployJobs = environments.map(env => ` |
| 91 | +deploy:${env.name}: |
| 92 | + stage: deploy |
| 93 | + script: |
| 94 | + - bun run cloud deploy --env=${env.name} |
| 95 | + environment: |
| 96 | + name: ${env.name} |
| 97 | + url: https://${env.name}.example.com |
| 98 | + only: |
| 99 | + - ${env.branch} |
| 100 | + ${env.manual ? 'when: manual' : ''} |
| 101 | +`).join('\n') |
| 102 | + |
| 103 | + return `stages: |
| 104 | + - test |
| 105 | + - build |
| 106 | + - deploy |
| 107 | +
|
| 108 | +variables: |
| 109 | + AWS_DEFAULT_REGION: ${awsRegion} |
| 110 | +
|
| 111 | +default: |
| 112 | + image: oven/bun:latest |
| 113 | + cache: |
| 114 | + paths: |
| 115 | + - node_modules/ |
| 116 | +
|
| 117 | +test: |
| 118 | + stage: test |
| 119 | + script: |
| 120 | + - bun install |
| 121 | + - bun test |
| 122 | +
|
| 123 | +build: |
| 124 | + stage: build |
| 125 | + script: |
| 126 | + - bun install |
| 127 | + - bun run build |
| 128 | + artifacts: |
| 129 | + paths: |
| 130 | + - dist/ |
| 131 | +${deployJobs} |
| 132 | +` |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Generate PR/MR preview pipeline |
| 137 | + */ |
| 138 | +export function generatePreviewPipeline(options: { |
| 139 | + awsRegion?: string |
| 140 | + ttl?: number |
| 141 | +} = {}): string { |
| 142 | + const { awsRegion = 'us-east-1', ttl = 24 } = options |
| 143 | + |
| 144 | + return `stages: |
| 145 | + - build |
| 146 | + - deploy |
| 147 | + - cleanup |
| 148 | +
|
| 149 | +variables: |
| 150 | + AWS_DEFAULT_REGION: ${awsRegion} |
| 151 | + TTL_HOURS: ${ttl} |
| 152 | +
|
| 153 | +default: |
| 154 | + image: oven/bun:latest |
| 155 | +
|
| 156 | +deploy:preview: |
| 157 | + stage: deploy |
| 158 | + script: |
| 159 | + - bun install |
| 160 | + - | |
| 161 | + bun run cloud env:preview \\ |
| 162 | + --branch=\${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME} \\ |
| 163 | + --pr=\${CI_MERGE_REQUEST_IID} \\ |
| 164 | + --commit=\${CI_COMMIT_SHA} \\ |
| 165 | + --ttl=\${TTL_HOURS} |
| 166 | + - PREVIEW_URL=\$(bun run cloud env:preview --get-url \${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}) |
| 167 | + - echo "Preview URL: \$PREVIEW_URL" |
| 168 | + environment: |
| 169 | + name: preview/\${CI_MERGE_REQUEST_IID} |
| 170 | + url: \${PREVIEW_URL} |
| 171 | + on_stop: cleanup:preview |
| 172 | + only: |
| 173 | + - merge_requests |
| 174 | +
|
| 175 | +cleanup:preview: |
| 176 | + stage: cleanup |
| 177 | + script: |
| 178 | + - bun install |
| 179 | + - | |
| 180 | + bun run cloud env:preview --destroy \\ |
| 181 | + --branch=\${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME} \\ |
| 182 | + --pr=\${CI_MERGE_REQUEST_IID} |
| 183 | + environment: |
| 184 | + name: preview/\${CI_MERGE_REQUEST_IID} |
| 185 | + action: stop |
| 186 | + when: manual |
| 187 | + only: |
| 188 | + - merge_requests |
| 189 | +` |
| 190 | +} |
| 191 | + |
| 192 | +/** |
| 193 | + * Generate scheduled pipeline |
| 194 | + */ |
| 195 | +export function generateScheduledPipeline(options: { |
| 196 | + environment: string |
| 197 | + awsRegion?: string |
| 198 | +}): string { |
| 199 | + const { environment, awsRegion = 'us-east-1' } = options |
| 200 | + |
| 201 | + return `stages: |
| 202 | + - deploy |
| 203 | +
|
| 204 | +variables: |
| 205 | + AWS_DEFAULT_REGION: ${awsRegion} |
| 206 | + ENVIRONMENT: ${environment} |
| 207 | +
|
| 208 | +default: |
| 209 | + image: oven/bun:latest |
| 210 | +
|
| 211 | +deploy:scheduled: |
| 212 | + stage: deploy |
| 213 | + script: |
| 214 | + - bun install |
| 215 | + - bun run cloud deploy --env=\${ENVIRONMENT} |
| 216 | + environment: |
| 217 | + name: \${ENVIRONMENT} |
| 218 | + only: |
| 219 | + - schedules |
| 220 | +` |
| 221 | +} |
| 222 | + |
| 223 | +/** |
| 224 | + * Generate manual deployment pipeline |
| 225 | + */ |
| 226 | +export function generateManualPipeline(options: { |
| 227 | + environments: string[] |
| 228 | + awsRegion?: string |
| 229 | +}): string { |
| 230 | + const { environments, awsRegion = 'us-east-1' } = options |
| 231 | + |
| 232 | + const deployJobs = environments.map(env => ` |
| 233 | +deploy:${env}: |
| 234 | + stage: deploy |
| 235 | + script: |
| 236 | + - bun install |
| 237 | + - bun run cloud deploy --env=${env} |
| 238 | + environment: |
| 239 | + name: ${env} |
| 240 | + when: manual |
| 241 | + only: |
| 242 | + - main |
| 243 | +`).join('\n') |
| 244 | + |
| 245 | + return `stages: |
| 246 | + - deploy |
| 247 | +
|
| 248 | +variables: |
| 249 | + AWS_DEFAULT_REGION: ${awsRegion} |
| 250 | +
|
| 251 | +default: |
| 252 | + image: oven/bun:latest |
| 253 | +${deployJobs} |
| 254 | +` |
| 255 | +} |
0 commit comments