-
Notifications
You must be signed in to change notification settings - Fork 1
106 lines (86 loc) · 3.27 KB
/
cd.yml
File metadata and controls
106 lines (86 loc) · 3.27 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
name: CD
on:
schedule:
- cron: '0 2 * * *'
workflow_dispatch:
permissions:
contents: read
actions: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout main
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: main
- name: Find last successful CD run on main
id: last_success
uses: actions/github-script@v7
with:
script: |
const workflowFile = 'cd.yml';
const branch = 'main';
const runs = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: workflowFile,
branch,
status: 'completed',
per_page: 50
});
const currentRunId = context.runId;
const previousSuccess = runs.data.workflow_runs.find(run =>
run.id !== currentRunId && run.conclusion === 'success'
);
if (!previousSuccess) {
core.setOutput('found', 'false');
core.setOutput('sha', '');
core.info('No previous successful run found.');
return;
}
core.setOutput('found', 'true');
core.setOutput('sha', previousSuccess.head_sha);
core.info(`Last successful run SHA: ${previousSuccess.head_sha}`);
- name: Check whether main changed since last successful run
id: changes
run: |
CURRENT_SHA=$(git rev-parse HEAD)
LAST_SHA="${{ steps.last_success.outputs.sha }}"
echo "current_sha=$CURRENT_SHA" >> "$GITHUB_OUTPUT"
if [ "${{ steps.last_success.outputs.found }}" != "true" ]; then
echo "changed=true" >> "$GITHUB_OUTPUT"
echo "No previous successful run found, so this run will publish."
elif [ "$CURRENT_SHA" = "$LAST_SHA" ]; then
echo "changed=false" >> "$GITHUB_OUTPUT"
echo "No changes since last successful run."
else
echo "changed=true" >> "$GITHUB_OUTPUT"
echo "Changes detected since last successful run."
fi
- name: Stop when there are no changes
if: steps.changes.outputs.changed != 'true'
run: |
echo "Nothing to publish."
exit 0
- name: Set version number
if: steps.changes.outputs.changed == 'true'
run: echo "PACKAGE_VERSION=1.0.$GITHUB_RUN_NUMBER" >> $GITHUB_ENV
- name: Setup .NET
if: steps.changes.outputs.changed == 'true'
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x
- name: Restore dependencies
if: steps.changes.outputs.changed == 'true'
run: dotnet restore
- name: Build
if: steps.changes.outputs.changed == 'true'
run: dotnet build --no-restore -c Release
- name: Pack
if: steps.changes.outputs.changed == 'true'
run: dotnet pack -p:PackageVersion=$PACKAGE_VERSION -c Release
- name: Push generated package to NuGet registry
if: steps.changes.outputs.changed == 'true'
run: dotnet nuget push src/DependencyUpdated/bin/Release/*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_API_KEY }}