Update .gitlab-ci.yml file

This commit is contained in:
kris
2025-11-10 08:22:49 +00:00
parent 8912a82dba
commit 90de8f4e4d
+28 -19
View File
@@ -2,62 +2,71 @@ stages:
- build - build
- deploy - deploy
# ====== template untuk build job (DRY) ====== # ====== TEMPLATE: BUILD STATIC NEXT.JS ======
.build_template: &build_template .build_template: &build_template
stage: build stage: build
image: node:20 image: node:20-alpine
cache: cache:
key: npm-cache key: npm-cache
paths: paths:
- node_modules/ - node_modules/
variables: variables:
NPM_CONFIG_PRODUCTION: "false" # install devDeps (tailwind/postcss) NPM_CONFIG_PRODUCTION: "false"
NODE_ENV: "" # kosongin supaya devDeps ikut NODE_ENV: ""
script: script:
- echo "🧩 Installing dependencies..."
- npm ci --no-audit --no-fund - npm ci --no-audit --no-fund
- npm install && npm run build # hasil export -> ./out - echo "⚙️ Building Next.js static export..."
- npx next build
artifacts: artifacts:
name: "out-$CI_COMMIT_SHORT_SHA" name: "out-$CI_COMMIT_SHORT_SHA"
paths: ["out/"] paths:
- out/
expire_in: 1 week expire_in: 1 week
# ====== template untuk deploy job (DRY) ====== # ====== TEMPLATE: DEPLOY KE S3 + CLOUDFRONT ======
.deploy_template: &deploy_template .deploy_template: &deploy_template
stage: deploy stage: deploy
image: image:
name: amazon/aws-cli:2.15.39 name: amazon/aws-cli:latest
entrypoint: [""] # matiin entrypoint "aws" entrypoint: ["/bin/sh", "-c"]
script: script:
- aws --version - aws --version
- echo "Deploying to s3://$S3_BUCKET in region $AWS_REGION" - echo "🚀 Deploying to s3://$S3_BUCKET in region $AWS_REGION"
- aws s3 sync ./out "s3://$S3_BUCKET" --delete - aws s3 sync ./out "s3://$S3_BUCKET" --delete --region "$AWS_REGION"
- | - |
if [ -n "$CLOUDFRONT_DISTRIBUTION_ID" ]; then if [ -n "$CLOUDFRONT_DISTRIBUTION_ID" ]; then
echo "📦 Invalidating CloudFront cache..."
aws cloudfront create-invalidation \ aws cloudfront create-invalidation \
--distribution-id "$CLOUDFRONT_DISTRIBUTION_ID" \ --distribution-id "$CLOUDFRONT_DISTRIBUTION_ID" \
--paths "/*" --paths "/*"
else
echo "️ No CloudFront distribution specified — skipping invalidation"
fi fi
# ====== DEVELOPMENT ====== # ====== DEVELOPMENT (Branch devops-s3) ======
build:development: build:dev:
<<: *build_template <<: *build_template
rules: rules:
- if: '$CI_COMMIT_BRANCH == "development"' - if: '$CI_COMMIT_BRANCH == "devops-s3"'
environment: environment:
name: development name: devops-s3
variables:
NEXT_PUBLIC_API_BASE_URL: "https://dev-api-lti.mbugroup.id"
NEXT_PUBLIC_SSO_LOGIN_URL: "https://dev-api-sso.mbugroup.id"
deploy:development: deploy:dev:
<<: *deploy_template <<: *deploy_template
needs: ["build:devops-s3"] needs: ["build:devops-s3"]
rules: rules:
- if: '$CI_COMMIT_BRANCH == "devops-s3"' - if: '$CI_COMMIT_BRANCH == "devops-s3"'
variables: variables:
# pakai variable scoped ke job ini S3_BUCKET: "web-lti-dev"
S3_BUCKET: "dev-lti-erp.mbugroup.id" AWS_DEFAULT_REGION: "ap-southeast-1"
CLOUDFRONT_DISTRIBUTION_ID: "E1Z8XTA8XF1GIV" CLOUDFRONT_DISTRIBUTION_ID: "E1Z8XTA8XF1GIV"
environment: environment:
name: devops-s3 name: devops-s3
# url: https://<cloudfront-dev-domain-atau-staging-domain-kamu> url: dev-lti-erp.mbugroup.id.s3.ap-southeast-3.amazonaws.com
# ====== PRODUCTION ====== # ====== PRODUCTION ======
build:production: build:production: