feat: Add five points #6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Environment Deployment Approval | |
on: | |
workflow_dispatch: | |
inputs: | |
services: | |
description: '选择需要部署的服务' | |
type: choice | |
options: | |
- all | |
- frontend | |
- backend | |
required: true | |
default: 'all' | |
push: | |
branches: [ master ] | |
paths: | |
- 'src/**' | |
- 'backend/**' | |
permissions: | |
contents: read | |
deployments: write | |
actions: write | |
pull-requests: read | |
jobs: | |
detect-changes: | |
runs-on: ubuntu-latest | |
outputs: | |
affected_services: ${{ steps.check-changes.outputs.services }} | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: Check Changed Services | |
id: check-changes | |
run: | | |
SERVICES=() | |
echo "Checking for changes..." | |
echo "Comparing: ${{ github.event.before }} -> ${{ github.sha }}" | |
if git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep -q "^src/"; then | |
echo "Found changes in frontend (src/)" | |
SERVICES+=("frontend") | |
fi | |
if git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep -q "^backend/"; then | |
echo "Found changes in backend (backend/)" | |
SERVICES+=("backend") | |
fi | |
echo "Detected services: ${SERVICES[*]}" | |
if [ ${#SERVICES[@]} -eq 0 ]; then | |
echo "No services affected" | |
echo "services=[]" >> $GITHUB_OUTPUT | |
else | |
JSON_ARRAY=$(printf '%s\n' "${SERVICES[@]}" | jq -R . | jq -s . | jq -c .) | |
echo "Final JSON output: ${JSON_ARRAY}" | |
echo "services=${JSON_ARRAY}" >> $GITHUB_OUTPUT | |
fi | |
deployment-approval: | |
needs: detect-changes | |
runs-on: ubuntu-latest | |
environment: production-approval | |
timeout-minutes: 300 | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
steps: | |
- name: Debug Event Info | |
run: | | |
echo "Event name: ${{ github.event_name }}" | |
echo "Ref: ${{ github.ref }}" | |
echo "SHA: ${{ github.sha }}" | |
echo "Actor: ${{ github.actor }}" | |
echo "Affected services: ${{ needs.detect-changes.outputs.affected_services }}" | |
- name: Create Service Selection | |
uses: actions/github-script@v6 | |
id: create-selection | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
// 获取检测到的服务 | |
const detectedServices = ${{ needs.detect-changes.outputs.affected_services || '[]' }}; | |
console.log('Detected services:', detectedServices); | |
// 处理手动触发的情况 | |
const isManualTrigger = '${{ github.event_name }}' === 'workflow_dispatch'; | |
const manualSelection = '${{ github.event.inputs.services }}'; | |
let selectedServices; | |
if (isManualTrigger) { | |
// 如果是手动触发 | |
selectedServices = manualSelection === 'all' ? detectedServices : [manualSelection]; | |
} else { | |
// 如果是自动触发(push 事件) | |
selectedServices = detectedServices; | |
} | |
if (selectedServices.length === 0 && '${{ github.event_name }}' === 'push') { | |
core.setFailed('No service changes detected'); | |
return; | |
} | |
console.log('Selected services for deployment:', selectedServices); | |
core.setOutput('selected_services', JSON.stringify(selectedServices)); | |
- name: Trigger Deploy Workflow | |
if: success() | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.WORKFLOW_PAT }} | |
script: | | |
const selectedServices = JSON.parse('${{ steps.create-selection.outputs.selected_services }}'); | |
console.log('Triggering deployment for services:', selectedServices); | |
await github.rest.actions.createWorkflowDispatch({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
workflow_id: 'deploy.yml', | |
ref: 'master', | |
inputs: { | |
services: JSON.stringify(selectedServices) | |
} | |
}); |