-
Notifications
You must be signed in to change notification settings - Fork 349
135 lines (112 loc) · 4.22 KB
/
run-setup.yml
File metadata and controls
135 lines (112 loc) · 4.22 KB
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
132
133
134
135
name: Run Setup
on:
push:
branches: [main]
paths:
- infrastructure/infrastructure-setup-bicep/**
pull_request:
branches: [main]
paths:
- infrastructure/infrastructure-setup-bicep/**
workflow_dispatch:
permissions:
contents: write
jobs:
run-setup:
runs-on: ubuntu-latest
steps:
- name: Checkout source branch
uses: actions/checkout@v3
with:
# PR: checks out the PR branch, Push: checks out main, Dispatch: checks out default branch
ref: ${{ github.head_ref || github.ref_name }}
fetch-depth: 0
- name: Install Bicep
run: |
INSTALL_PATH="$RUNNER_TEMP/bicep"
BICEP_PATH="$RUNNER_TEMP/bicep/bicep"
mkdir -p "$INSTALL_PATH"
curl -sLo bicep https://github.com/Azure/bicep/releases/latest/download/bicep-linux-x64
chmod +x ./bicep
sudo mv ./bicep "$INSTALL_PATH"
echo "BICEP_PATH=$BICEP_PATH" >> $GITHUB_ENV
$BICEP_PATH --version
- name: Determine changed main.bicep files
id: changes
run: |
set -e
cd "$GITHUB_WORKSPACE"
EVENT="${{ github.event_name }}"
echo "Event: $EVENT"
if [ "$EVENT" = "pull_request" ]; then
BASE="${{ github.event.pull_request.base.sha }}"
HEAD="${{ github.event.pull_request.head.sha }}"
elif [ "$EVENT" = "push" ]; then
BASE="${{ github.event.before }}"
HEAD="${{ github.sha }}"
else
# workflow_dispatch: use last commit as best-effort
BASE="$(git rev-parse HEAD~1 || echo '')"
HEAD="$(git rev-parse HEAD)"
fi
echo "Diff range: ${BASE}..${HEAD}"
# Only rebuild when main.bicep changes
if [ -n "$BASE" ]; then
MODIFIED=$(git diff --name-only "$BASE" "$HEAD" \
| grep -E "^infrastructure/infrastructure-setup-bicep/.*/main\.bicep$" || true)
else
MODIFIED=$(git show --name-only --pretty="" -1 \
| grep -E "^infrastructure/infrastructure-setup-bicep/.*/main\.bicep$" || true)
fi
if [ -z "$MODIFIED" ]; then
echo "No relevant Bicep changes detected."
echo "files=" >> $GITHUB_OUTPUT
exit 0
fi
echo "Changed main.bicep files:"
echo "$MODIFIED"
# Output as newline-delimited list
{
echo "files<<EOF"
echo "$MODIFIED"
echo "EOF"
} >> $GITHUB_OUTPUT
- name: Build changed Bicep files -> azuredeploy.json
if: steps.changes.outputs.files != ''
run: |
set -e
cd "$GITHUB_WORKSPACE"
while IFS= read -r BICEP_FILE; do
OUTFILE="$(dirname "$BICEP_FILE")/azuredeploy.json"
echo "Building: $BICEP_FILE -> $OUTFILE"
$BICEP_PATH build "$BICEP_FILE" --outfile "$OUTFILE"
done <<< "${{ steps.changes.outputs.files }}"
- name: Commit + push changes back to branch (PR) or main (push)
if: always()
run: |
set -e
cd "$GITHUB_WORKSPACE"
git config --global user.email "foundry-samples@noreply.github.com"
git config --global user.name "foundry-samples automation"
git add -A
if git diff-index --quiet HEAD --; then
echo "No changes to commit."
exit 0
fi
git commit -m "Automatic fixes"
EVENT="${{ github.event_name }}"
# If PR is from a fork, pushing will be rejected. Detect and skip.
if [ "$EVENT" = "pull_request" ]; then
if [ "${{ github.event.pull_request.head.repo.full_name }}" != "${{ github.repository }}" ]; then
echo "PR is from a fork; cannot push changes back to fork branch. Skipping push."
exit 0
fi
BRANCH="${{ github.head_ref }}"
echo "Pushing fixes to PR branch: $BRANCH"
git push origin "HEAD:refs/heads/$BRANCH"
exit 0
fi
# push / workflow_dispatch
BRANCH="${{ github.ref_name }}"
echo "Pushing fixes to branch: $BRANCH"
git push origin "HEAD:refs/heads/$BRANCH"