feat: Add four points #5
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 services = ${{ needs.detect-changes.outputs.affected_services || '[]' }}; | |
console.log('Detected services:', services); | |
if (services.length === 0 && '${{ github.event_name }}' === 'push') { | |
core.setFailed('No service changes detected'); | |
return; | |
} | |
const selectedServices = '${{ github.event.inputs.services }}' === 'all' | |
? services | |
: ['${{ github.event.inputs.services }}']; | |
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) | |
} | |
}); |