Skip to content

Instantly share code, notes, and snippets.

@cremerfc
Created July 30, 2025 14:55
Show Gist options
  • Save cremerfc/be7ceda66a8af8d7df3a101bffe23537 to your computer and use it in GitHub Desktop.
Save cremerfc/be7ceda66a8af8d7df3a101bffe23537 to your computer and use it in GitHub Desktop.
GitLab Pipeline
stages:
- build
- test
- deploy
- notify
variables:
CORTEX_API_URL: "https://api.getcortexapp.com/api/v1/catalog"
# Global settings
image: node:18
build_job:
stage: build
script:
- echo "Building application..."
- npm ci
- npm run build
artifacts:
paths:
- dist/
expire_in: 1 hour
test_job:
stage: test
script:
- echo "Running tests..."
- npm test
dependencies:
- build_job
deploy_job:
stage: deploy
script:
- echo "Deploying to staging..."
# Your deployment commands here
- sleep 2
- echo "Deployment completed"
dependencies:
- build_job
environment:
name: staging
# This job always runs and reports pipeline status to Cortex
notify_cortex:
stage: notify
image: curlimages/curl:latest
before_script:
# Check if previous stages succeeded by examining needs
- |
if [ "$BUILD_JOB_STATUS" = "success" ] && [ "$TEST_JOB_STATUS" = "success" ] && [ "$DEPLOY_JOB_STATUS" = "success" ]; then
PIPELINE_STATUS="success"
DEPLOY_TYPE="DEPLOY"
MESSAGE="Pipeline completed successfully"
else
PIPELINE_STATUS="failed"
DEPLOY_TYPE="ROLLBACK"
MESSAGE="Pipeline failed - one or more stages failed"
fi
echo "Pipeline Status: $PIPELINE_STATUS"
echo "Deploy Type: $DEPLOY_TYPE"
echo "Message: $MESSAGE"
script:
- |
# Convert repo name to lowercase
REPO_NAME=$(echo "$CI_PROJECT_NAME" | tr '[:upper:]' '[:lower:]')
echo "Repository: $REPO_NAME"
# Get current timestamp
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
# Send notification to Cortex
curl -L \
--request POST \
--max-time 30 \
--retry 2 \
--url "$CORTEX_API_URL/$REPO_NAME/deploys" \
--header "Authorization: Bearer $CORTEX_TOKEN" \
--header "Content-Type: application/json" \
--data "{
\"customData\": {
\"pipeline_id\": \"$CI_PIPELINE_ID\",
\"job_id\": \"$CI_JOB_ID\",
\"branch\": \"$CI_COMMIT_REF_NAME\",
\"pipeline_status\": \"$PIPELINE_STATUS\",
\"message\": \"$MESSAGE\",
\"pipeline_url\": \"$CI_PIPELINE_URL\",
\"project_path\": \"$CI_PROJECT_PATH\"
},
\"deployer\": {
\"email\": \"$GITLAB_USER_EMAIL\",
\"name\": \"$GITLAB_USER_NAME\"
},
\"environment\": \"staging\",
\"sha\": \"$CI_COMMIT_SHA\",
\"timestamp\": \"$TIMESTAMP\",
\"title\": \"Pipeline $PIPELINE_STATUS - $CI_PROJECT_NAME\",
\"type\": \"$DEPLOY_TYPE\",
\"url\": \"$CI_PIPELINE_URL\"
}"
if [ $? -eq 0 ]; then
echo "Successfully notified Cortex"
else
echo "Failed to notify Cortex, but continuing..."
fi
needs:
- job: build_job
artifacts: false
- job: test_job
artifacts: false
- job: deploy_job
artifacts: false
when: always
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment