-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathazure-pipelines.yml
More file actions
124 lines (112 loc) · 4.76 KB
/
Copy pathazure-pipelines.yml
File metadata and controls
124 lines (112 loc) · 4.76 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
# Azure DevOps pipeline: diff the NEW codegen (committed over the old one)
# against the OLD codegen (the previous git revision), and ship an HTML report
# alongside the generated code.
#
# How it works
# - OLD: on a PR build, the merge-base with the target branch; on a CI build,
# the previous commit (HEAD~1). Checked out with `git worktree add`, so no
# extra artifact or snapshot has to be stored anywhere.
# - NEW: the current working tree (the regenerated code, already committed).
# - The tool runs with --exit-zero: codegen changing is normal here, so the
# pipeline does not fail on it. The report is what the reviewer reads
# (summary + ARXML interfaces added/removed).
# - The report is then:
# 1. published as build artifact `codegen`, together with the code;
# 2. on CI builds (not PRs) committed back into the repo with [skip ci].
#
# One-time setup
# - Edit the 2 places marked <<EDIT>> below (the tool repo name, the codegen
# path).
# - If compare_tool already lives INSIDE the codegen repo: delete the
# `resources` block and both checkout steps, replace them with a single
# `- checkout: self` (keeping fetchDepth / persistCredentials), and drop
# the TOOL_DIR variable (point PYTHONPATH at $(Build.SourcesDirectory)).
# - For the commit-report step to be allowed to push: Project Settings >
# Repositories > <codegen repo> > Security > "<Project> Build Service" ->
# Contribute = Allow.
parameters:
- name: codegenPath # <<EDIT>> codegen folder in the repo (relative path)
type: string
default: 'codegen'
trigger:
branches:
include: [main]
pr:
branches:
include: ['*']
pool:
vmImage: ubuntu-latest # windows-latest works too (the steps are bash tasks)
variables:
CODE_DIR: $(Pipeline.Workspace)/s/code
TOOL_DIR: $(Pipeline.Workspace)/s/tool
OLD_DIR: $(Agent.TempDirectory)/old_code
resources:
repositories:
- repository: compare_tool_repo
type: git
name: MyProject/code-review # <<EDIT>> project/repo holding compare_tool
ref: refs/heads/main
steps:
- checkout: self
fetchDepth: 0 # history is needed to reach the previous revision
persistCredentials: true # needed by the commit-report step
path: s/code
- checkout: compare_tool_repo
path: s/tool
# OLD = the previous git revision
- bash: |
set -e
cd "$(CODE_DIR)"
if [ "$BUILD_REASON" = "PullRequest" ]; then
TARGET="${SYSTEM_PULLREQUEST_TARGETBRANCH#refs/heads/}"
git fetch origin "$TARGET"
BASE=$(git merge-base HEAD "origin/$TARGET")
else
BASE=$(git rev-parse HEAD~1 2>/dev/null || git rev-parse HEAD)
fi
echo "OLD = $BASE ($(git log -1 --format='%s' $BASE))"
# hand the compare step something to call the BASELINE side: it reads
# from a temp worktree, so without this the report header names the
# scratch folder instead of the revision it holds
echo "##vso[task.setvariable variable=BASE_NAME]$(git log -1 --format='%h %s' $BASE)"
git worktree add "$(OLD_DIR)" "$BASE"
# on the very first run the old revision has no codegen folder yet ->
# create an empty one so every file counts as Added
mkdir -p "$(OLD_DIR)/${{ parameters.codegenPath }}"
displayName: 'Check out the previous codegen from git'
# Diff, and write the report straight into the codegen folder
- bash: |
set -e
python3 -m compare_tool \
"$(OLD_DIR)/${{ parameters.codegenPath }}" \
"$(CODE_DIR)/${{ parameters.codegenPath }}" \
--report "$(CODE_DIR)/${{ parameters.codegenPath }}/compare_report.html" \
--baseline-name "$(BASE_NAME)" \
--current-name "build $(Build.BuildNumber)" \
--exclude compare_report.html \
--exit-zero
displayName: 'Diff old vs new codegen'
env:
PYTHONPATH: $(TOOL_DIR)
# code and report published as one artifact
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(CODE_DIR)/${{ parameters.codegenPath }}'
ArtifactName: 'codegen'
displayName: 'Publish codegen + report'
# CI build: commit the report back into the repo. [skip ci] keeps this commit
# from triggering the pipeline again.
- bash: |
set -e
cd "$(CODE_DIR)"
git config user.name "Azure Pipeline"
git config user.email "pipeline@dev.azure.com"
git add "${{ parameters.codegenPath }}/compare_report.html"
if git diff --cached --quiet; then
echo "Report unchanged, nothing to commit."
else
git commit -m "Update codegen compare report [skip ci]"
git push origin "HEAD:${BUILD_SOURCEBRANCH#refs/heads/}"
fi
displayName: 'Commit the report into the repo'
condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))