forked from ubuntu-robotics/snap_workflows
-
Notifications
You must be signed in to change notification settings - Fork 2
150 lines (130 loc) · 4.93 KB
/
generic-upstream-monitor.yaml
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
name: generic-upstream-monitor
on:
workflow_call:
inputs:
git-ref:
default: '${{ github.ref }}'
description: The branch to checkout. Defaults to the current git ref.
required: false
type: string
issue-assignee:
default: ''
description: The issue assignee in the form '@name'.
required: false
type: string
script-compare-versions:
description: A bash script to compare versions.
required: true
type: string
script-get-upstream-version:
description: A bash script to retrieve the upstream version.
required: true
type: string
snapcraft-source-subdir:
default: '.'
description: The directory of the snapcraft project.
required: false
type: string
jobs:
compare:
runs-on: 'ubuntu-latest'
permissions:
issues: write
steps:
- name: Install dependencies
run: sudo snap install yq
- name: Checkout
uses: actions/checkout@v4
with:
ref: '${{ inputs.git-ref }}'
- name: Find and parse snapcraft.yaml to retrieve the version
id: parse
env:
project_root: ${{ inputs.snapcraft-source-subdir }}
run: |
# If no project path is specified, default to top-level of repo
project_root="${project_root:-.}"
valid_paths=(
"${project_root}/.snapcraft.yaml"
"${project_root}/build-aux/snap/snapcraft.yaml"
"${project_root}/snap/snapcraft.yaml"
"${project_root}/snapcraft.yaml"
)
for file in "${valid_paths[@]}"; do
if [[ -f "$file" ]]; then
yaml_path="$file"
fi
done
if [[ -z "${yaml_path}" ]]; then
echo "No snapcraft.yaml found"
exit 1
fi
if yq '. | has("adopt-info")' "${yaml_path}" && yq '.parts[.adopt-info] | has("source-tag")' "${yaml_path}"; then
version="$(yq '.parts[.adopt-info].source-tag' ${yaml_path})"
else
version="$(yq -r '.version' ${yaml_path})"
fi
if [ -z "${version}" ]; then exit 1; fi
echo "snap-version=${version}" >> "$GITHUB_OUTPUT"
- name: Create get-upstream-version script
shell: bash
run: |
cat > get-upstream-version << 'EOF'
${{ inputs.script-get-upstream-version }}
EOF
chmod +x get-upstream-version
- name: Run get-upstream-version script
id: get-upstream-version
shell: bash
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euxo pipefail
version="$(./get-upstream-version)"
if [ -z "${version}" ]; then exit 1; fi
echo "upstream-version=${version}" >> "$GITHUB_OUTPUT"
- name: Create script-compare-versions script
shell: bash
run: |
cat > compare-versions << 'EOF'
${{ inputs.script-compare-versions }}
EOF
chmod +x compare-versions
- name: Compare versions
id: compare
env:
SNAP_VERSION: "${{ steps.parse.outputs.snap-version }}"
UPSTREAM_VERSION: "${{ steps.get-upstream-version.outputs.upstream-version }}"
run: |
set -euxo pipefail
NEW_RELEASE="$(./compare-versions ${UPSTREAM_VERSION} ${SNAP_VERSION})"
echo "new-release=${NEW_RELEASE}" >> "$GITHUB_OUTPUT"
- name: Open issue
if: steps.compare.outputs.new-release == '1'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
search_issue="$(gh issue --repo ${{ github.repository }} list --state open \
--search '${{ steps.get-upstream-version.outputs.upstream-version }} in:title' --json number)"
if [[ ! "${search_issue}" =~ "number" ]]; then
echo "Creating a new issue."
extra_args=""
if [ ! -z "${{ inputs.issue-assignee }}" ]; then
extra_args="${extra_args} --assignee ${{ inputs.issue-assignee }}"
fi
echo -e "## Monitoring report\n\n" \
"- Author: @${{ github.triggering_actor }}\n" \
"- Branch: '${{ github.ref }}'\n" \
"- Commit: ${{ github.sha }}\n" \
"- Workflow Path: '${{ github.workflow_ref }}'\n\n" \
"The workflow found a new version upstream: '${{ steps.get-upstream-version.outputs.upstream-version }}'.\n" \
"Please consider upgrading the snap.\n\n" \
"Workflow details at: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" \
| \
gh issue --repo ${{ github.repository }} create \
--title "[CI] Found version '${{ steps.get-upstream-version.outputs.upstream-version }}' upstream" \
${extra_args} \
--body-file -
else
echo "Issue already opened."
fi