This commit is contained in:
Chu Văn Hải committed 2025-12-07 00:03:32 +07:00
1 parent f0d13cd510
commit 0aeeaafca0
1 file changed
+94 -4
+94 -4
View File
@@ -79,10 +79,99 @@ on:
jobs: jobs:
# ============================================================================ # ============================================================================
# JOB 1: BUILD AND PUSH DOCKER IMAGE # JOB 1: PREPARE - AUTO-DETECT FRAMEWORK AND SETTINGS
# ============================================================================
prepare:
runs-on: alpine-latest
outputs:
node_version: ${{ steps.detect.outputs.NODE_VERSION }}
framework: ${{ steps.detect.outputs.FRAMEWORK }}
package_manager: ${{ steps.detect.outputs.PACKAGE_MANAGER }}
container_port: ${{ steps.detect.outputs.CONTAINER_PORT }}
build_command: ${{ steps.detect.outputs.BUILD_COMMAND }}
start_command: ${{ steps.detect.outputs.START_COMMAND }}
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Auto-detect settings
id: detect
run: |
echo "🔍 Starting auto-detection process..."
NODE_VERSION="18" # Default Node.js version
if [ -f ".nvmrc" ]; then
NODE_VERSION=$(cat .nvmrc | tr -d 'v' | tr -d '\n' | head -1)
elif [ -f "package.json" ] && grep -q '.engines.node' package.json; then
RAW_VERSION=$(grep '.engines.node' package.json | sed -E 's/.*"node": "([^"]+)".*/\1/')
NODE_VERSION=$(echo "$RAW_VERSION" | grep -oE '[0-9]+' | head -n 1)
fi
echo "✓ Detected Node.js version: ${NODE_VERSION}"
echo "NODE_VERSION=${NODE_VERSION}" >> $GITEA_OUTPUT
FRAMEWORK="nodejs"
CONTAINER_PORT="3000"
if [ -f "package.json" ]; then
if grep -q '"@nestjs/core"' package.json; then FRAMEWORK="nestjs";
elif grep -q '"next"' package.json; then FRAMEWORK="nextjs";
elif grep -q '"react-scripts"' package.json; then FRAMEWORK="reactjs";
elif grep -q '"vite"' package.json && grep -q '"react"' package.json; then FRAMEWORK="reactjs-vite";
elif grep -q '"express"' package.json; then FRAMEWORK="expressjs";
fi
fi
echo "✓ Detected framework: ${FRAMEWORK}"
echo "FRAMEWORK=${FRAMEWORK}" >> $GITEA_OUTPUT
echo "CONTAINER_PORT=${CONTAINER_PORT}" >> $GITEA_OUTPUT
PACKAGE_MANAGER="npm"
if [ -f "pnpm-lock.yaml" ]; then PACKAGE_MANAGER="pnpm";
elif [ -f "yarn.lock" ]; then PACKAGE_MANAGER="yarn";
fi
echo "✓ Detected package manager: ${PACKAGE_MANAGER}"
echo "PACKAGE_MANAGER=${PACKAGE_MANAGER}" >> $GITEA_OUTPUT
BUILD_CMD=""
START_CMD=""
if [ -f "package.json" ]; then
BUILD_CMD=$(grep -o '"build": "[^"]*"' package.json | sed 's/"build": "\(.*\)"/\1/' || echo "")
START_CMD=$(grep -o '"start": "[^"]*"' package.json | sed 's/"start": "\(.*\)"/\1/' || echo "")
fi
echo "✓ Detected build command: ${BUILD_CMD}"
echo "✓ Detected start command: ${START_CMD}"
echo "BUILD_COMMAND=${BUILD_CMD}" >> $GITEA_OUTPUT
echo "START_COMMAND=${START_CMD}" >> $GITEA_OUTPUT
# ============================================================================
# JOB 2: GENERATE DOCKERFILE
# ============================================================================
generate-dockerfile:
runs-on: alpine-latest
needs: prepare
outputs:
dockerfile_path: steps.generate.outputs.dockerfile_path
steps:
- name: Checkout Ansible Repo for templates
uses: actions/checkout@v4
with:
repository: ${{ github.repository_owner }}/ansible-gitea
path: .ansible
- name: Generate Dockerfile
id: generate
run: |
echo "🔧 Generating Dockerfile for ${{ needs.prepare.outputs.framework }} framework..."
# In a real scenario, you would have a script here to generate the Dockerfile
# from a template based on the framework.
# For this example, we'll just point to a placeholder.
# cp .ansible/templates/dockerfiles/${{ needs.prepare.outputs.framework }}.j2 Dockerfile
# For now, we assume a Dockerfile exists in the calling repo.
echo "dockerfile_path=./Dockerfile" >> $GITEA_OUTPUT
# ============================================================================
# JOB 3: BUILD AND PUSH DOCKER IMAGE
# ============================================================================ # ============================================================================
build: build:
runs-on: alpine-latest runs-on: alpine-latest
needs: [prepare, generate-dockerfile]
# Grant permissions for the workflow to request an OIDC token. # Grant permissions for the workflow to request an OIDC token.
permissions: permissions:
id-token: write id-token: write
@@ -128,7 +217,7 @@ jobs:
uses: docker/build-push-action@v5 uses: docker/build-push-action@v5
with: with:
context: . context: .
file: ./Dockerfile file: ${{ needs.generate-dockerfile.outputs.dockerfile_path }}
push: true push: true
tags: ${{ steps.meta.outputs.tags }} tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }} labels: ${{ steps.meta.outputs.labels }}
@@ -136,11 +225,11 @@ jobs:
env=${{ steps.vault.outputs.DOTENV_CONTENTS }} env=${{ steps.vault.outputs.DOTENV_CONTENTS }}
# ============================================================================ # ============================================================================
# JOB 2: DEPLOY WITH ANSIBLE # JOB 4: DEPLOY WITH ANSIBLE
# ============================================================================ # ============================================================================
deploy: deploy:
runs-on: alpine-latest runs-on: alpine-latest
needs: build # This job runs only after the 'build' job succeeds. needs: [prepare, build] # This job runs only after 'prepare' and 'build' jobs succeed.
steps: steps:
- name: Checkout Application Code - name: Checkout Application Code
uses: actions/checkout@v4 uses: actions/checkout@v4
@@ -197,6 +286,7 @@ jobs:
server_port=${{ inputs.server_port }} \ server_port=${{ inputs.server_port }} \
server_host=$SERVER_HOST \ server_host=$SERVER_HOST \
git_repo=${{ github.server_url }}/${{ github.repository }} \ git_repo=${{ github.server_url }}/${{ github.repository }} \
framework=${{ needs.prepare.outputs.framework }} \
docker_image_tag=${{ needs.build.outputs.image_tag }} \ docker_image_tag=${{ needs.build.outputs.image_tag }} \
docker_image_name=${{ needs.build.outputs.image_name }} \ docker_image_name=${{ needs.build.outputs.image_name }} \
docker_image_full=${{ needs.build.outputs.image_name }}:${{ needs.build.outputs.image_tag }} \ docker_image_full=${{ needs.build.outputs.image_name }}:${{ needs.build.outputs.image_tag }} \