-
Notifications
You must be signed in to change notification settings - Fork 0
131 lines (114 loc) · 4.31 KB
/
env_approval.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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)
}
});