-
-
Notifications
You must be signed in to change notification settings - Fork 0
198 lines (185 loc) · 7.71 KB
/
test-action-different-workflow.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
name: 'Test GH action: share data on different workflow'
on:
workflow_run:
workflows: [
"Test GH action: share data on current workflow",
]
types:
- completed
defaults:
run:
shell: pwsh
env:
# Need to checkout the same commit from the workflow run that triggered this workflow.
# By default the checkout step gets the latest from master but that's not what we want.
# Without checking out the workflow run head sha the errors that we get into are like:
# - Create a PR, that will run the workflows that trigger this one but then, instead of
# checking commit from the PR, we get the commit from master. This would mean we would
# run this workflow which builds the docker image and runs tests using the incorrect commit.
WORKFLOW_HEAD_SHA : ${{ github.event.workflow_run.head_sha }}
jobs:
trigger-info:
name: Trigger info
permissions:
contents: read
runs-on: ubuntu-latest
steps:
- name: Dump github context for debug purposes
env:
GITHUB_CONTEXT: ${{ toJSON(github) }}
run: $env:GITHUB_CONTEXT
- name: Trigger info
run: |
Write-Output "::notice::This workflow was triggered by a 'workflow_run' from '${{ github.event.workflow_run.name }}'."
read-data-different-workflow-with-github-step-json-output:
name: read-data-different-workflow command with github-step-json output
permissions:
contents: read
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ env.WORKFLOW_HEAD_SHA }}
- name: Run read-data-different-workflow command
id: read
uses: ./action-local
with:
command: read-data-different-workflow
run-id: ${{ github.event.workflow_run.id }}
artifact-name: from-set-with-github-step-json-output
# output: github-step-json # this is the default
- name: Dump outputs from previous step
env:
STEP_OUTPUT: ${{ toJSON(steps.read.outputs) }}
run: $env:STEP_OUTPUT
- name: Verify read-data-current-workflow command output
run: |
$street = @'
400 Mockingbird Lane
address line 2
'@
if('${{ steps.read.outputs.name }}' -eq 'George Washington' `
-and '${{ steps.read.outputs.age }}' -eq '89' `
-and '${{ steps.read.outputs.height_in_inches }}' -eq '5.75' `
-and '${{ steps.read.outputs.addresses_home_street }}' -eq $street `
-and '${{ steps.read.outputs.addresses_home_city }}' -eq 'Louaryland' `
-and '${{ steps.read.outputs.addresses_home_state }}' -eq 'Hawidaho' `
-and '${{ steps.read.outputs.addresses_home_zip }}' -eq '99970' `
-and '${{ steps.read.outputs.addresses_home_list_0_ }}' -eq 'first' `
-and '${{ steps.read.outputs.addresses_home_list_1_ }}' -eq 'second' `
-and '${{ steps.read.outputs.addresses_home_list_2_ }}' -eq 'third')
{
Write-Output "Action produced expected output values."
Exit 0
}
Write-Output "::error::Action didn't produce expected github-step-json output."
Exit 1
read-data-different-workflow-with-strict-json-output:
name: read-data-different-workflow command with strict-json output
permissions:
contents: read
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ env.WORKFLOW_HEAD_SHA }}
- name: Run read-data-different-workflow command
id: read
uses: ./action-local
with:
command: read-data-different-workflow
run-id: ${{ github.event.workflow_run.id }}
artifact-name: from-set-with-strict-json-output
output: strict-json
- name: Dump outputs from previous step
env:
STEP_OUTPUT: ${{ toJSON(steps.read.outputs) }}
run: $env:STEP_OUTPUT
- name: Verify read-data-current-workflow command output
run: |
# verify using github fromJson function
$street = @'
400 Mockingbird Lane
address line 2
'@
if('${{ fromJson(steps.read.outputs.data).name }}' -eq 'George Washington' `
-and '${{ fromJson(steps.read.outputs.data).age }}' -eq '89' `
-and '${{ fromJson(steps.read.outputs.data).height_in_inches }}' -eq '5.75' `
-and '${{ fromJson(steps.read.outputs.data).addresses.home.street }}' -eq $street `
-and '${{ fromJson(steps.read.outputs.data).addresses.home.city }}' -eq 'Louaryland' `
-and '${{ fromJson(steps.read.outputs.data).addresses.home.state }}' -eq 'Hawidaho' `
-and '${{ fromJson(steps.read.outputs.data).addresses.home.zip }}' -eq '99970' `
-and '${{ fromJson(steps.read.outputs.data).addresses.home.list[0] }}' -eq 'first' `
-and '${{ fromJson(steps.read.outputs.data).addresses.home.list[1] }}' -eq 'second' `
-and '${{ fromJson(steps.read.outputs.data).addresses.home.list[2] }}' -eq 'third')
{
Write-Output "Action produced expected output values."
Exit 0
}
Write-Output "::error::Action didn't produce expected github-step-json output."
Exit 1
- name: Verify read-data-current-workflow command output 2
run: |
# verify using powershell
$data = '${{ steps.read.outputs.data }}' | ConvertFrom-Json
$street = @'
400 Mockingbird Lane
address line 2
'@
if($data.name -eq 'George Washington' `
-and $data.age -eq '89' `
-and $data.height_in_inches -eq '5.75' `
-and $data.addresses.home.street -eq $street `
-and $data.addresses.home.city -eq 'Louaryland' `
-and $data.addresses.home.state -eq 'Hawidaho' `
-and $data.addresses.home.zip -eq '99970' `
-and $data.addresses.home.list[0] -eq 'first' `
-and $data.addresses.home.list[1] -eq 'second' `
-and $data.addresses.home.list[2] -eq 'third')
{
Write-Output "Action produced expected output values."
Exit 0
}
Write-Output "::error::Action didn't produce expected github-step-json output."
Exit 1
pr-status:
name: Set PR status
needs: [
read-data-different-workflow-with-github-step-json-output,
read-data-different-workflow-with-strict-json-output,
]
if: github.event.workflow_run.event == 'pull_request' && always()
permissions:
statuses: write
runs-on: ubuntu-latest
steps:
- name: Dump github context for debug purposes
env:
GITHUB_CONTEXT: ${{ toJSON(github) }}
run: $env:GITHUB_CONTEXT
- name: Create GitHub status on PR
run: |
$read1JobResult = '${{ needs.read-data-different-workflow-with-github-step-json-output.result }}'
$read2JobResult = '${{ needs.read-data-different-workflow-with-strict-json-output.result }}'
if ($read1JobResult -eq 'success' -and $read2JobResult -eq 'success')
{
$description = 'Successful'
$state = 'success'
}
else
{
$description = 'Error'
$state = 'error'
}
$uri = "https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.event.workflow_run.head_sha }}"
$headers = @{
Accept = "application/vnd.github.v3+json"
Authorization = "Bearer ${{ secrets.GITHUB_TOKEN }}"
}
$body = @{
context = '${{ github.workflow }} / read-data-different-workflow command (${{ github.event.workflow_run.event }})' # mimic format from github
description = $description
target_url = 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
state = $state
} | ConvertTo-Json -compress
Invoke-RestMethod -Method 'Post' -Uri $uri -Headers $headers -Body $body