Skip to content

Commit 8ddd14b

Browse files
Merge pull request #41 from im-open/build-refactor
ARCH-1916 - Workflow Refactor
2 parents cf68862 + 382a5ae commit 8ddd14b

5 files changed

Lines changed: 353 additions & 157 deletions

File tree

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
name: Build and Review PR
2+
run-name: 'Build and Review PR #${{ github.event.pull_request.number }}'
3+
4+
on:
5+
# https://docs.github.com/en/actions/security-guides/automatic-token-authentication#permissions-for-the-github_token
6+
#
7+
# This workflow uses the pull_request trigger which prevents write permissions on the
8+
# GH_TOKEN and secrets access from public forks. This should remain as a pull_request
9+
# trigger to minimize the access public forks have in the repository. The reduced
10+
# permissions are adequate but do mean that re-compiles and readme changes will have to be
11+
# made manually by the PR author. These auto-updates could be done by this workflow
12+
# for branches but in order to re-trigger a PR build (which is needed for status checks),
13+
# we would make the commits with a different user and their PAT. To minimize exposure
14+
# and complication we will request those changes be manually made by the PR author.
15+
pull_request:
16+
types: [opened, synchronize, reopened]
17+
# paths:
18+
# Do not include specific paths here. We always want this build to run and produce a
19+
# status check which are branch protection rules can use. If this is skipped because of
20+
# path filtering, a status check will not be created and we won't be able to merge the PR
21+
# without disabling that requirement. If we have a status check that is always produced,
22+
# we can also use that to require all branches be up to date before they are merged.
23+
24+
jobs:
25+
setup:
26+
runs-on: ubuntu-latest
27+
outputs:
28+
HAS_SOURCE_CODE_CHANGES: ${{ steps.source-code.outputs.HAS_CHANGES }}
29+
NEXT_VERSION: ${{ steps.version.outputs.NEXT_VERSION }}
30+
31+
steps:
32+
- name: Checkout
33+
uses: actions/checkout@v3
34+
with:
35+
ref: main
36+
fetch-depth: 0
37+
38+
- name: Check for code changes to the action source code
39+
id: source-code
40+
uses: im-open/did-custom-action-code-change@v1
41+
with:
42+
files-with-code: 'action.yml,package.json,package-lock.json'
43+
folders-with-code: 'src,dist'
44+
token: ${{ secrets.GITHUB_TOKEN }}
45+
46+
- name: Action Source Code Changed - ${{ steps.source-code.outputs.HAS_CHANGES }} (open for details)
47+
run: |
48+
if [ "${{ steps.source-code.outputs.HAS_CHANGES }}" == "true" ]; then
49+
echo "This PR changes the action's source code. Proceed with subsequent steps."
50+
else
51+
echo "This PR does not change the action's source code. Skipping subsequent steps."
52+
fi
53+
54+
- name: Get the next version for the repo
55+
if: steps.source-code.outputs.HAS_CHANGES == 'true'
56+
id: version
57+
uses: ./
58+
59+
- name: The next action version will be - ${{ steps.version.outputs.NEXT_VERSION || 'N/A'}}
60+
if: steps.source-code.outputs.HAS_CHANGES == 'true'
61+
run: echo "The next action version will be - ${{ steps.version.outputs.NEXT_VERSION }}"
62+
63+
build-and-review-pr:
64+
runs-on: ubuntu-latest
65+
needs: [setup]
66+
67+
env:
68+
NEXT_VERSION: ${{ needs.setup.outputs.NEXT_VERSION || 'N/A' }}
69+
HAS_CODE_CHANGES: ${{ needs.setup.outputs.HAS_SOURCE_CODE_CHANGES }}
70+
IS_FORK: ${{ github.event.pull_request.head.repo.fork }}
71+
PR_SOURCE: ${{ github.event.pull_request.head.repo.fork == true && 'fork' || 'branch' }}
72+
README: README.md
73+
HAS_BUILD_STEP: 'true'
74+
NEEDS_BUILD_COMMIT: false
75+
NEEDS_README_COMMIT: false
76+
77+
steps:
78+
- name: Action Source Code Changed (open for details)
79+
run: |
80+
if [ "${{env.HAS_CODE_CHANGES}}" == "true" ]; then
81+
echo "This PR changes the action's source code. Proceed with subsequent steps and jobs."
82+
else
83+
echo "This PR does not change the action's source code. Skipping subsequent steps and jobs."
84+
fi
85+
86+
# ----------------------------------------------------------------------------------------------------
87+
#
88+
# The remaining steps in this build will use the env.HAS_CODE_CHANGES condition. Setting it on each
89+
# step rather than the job will ensure this job always runs and that we can use it for status checks.
90+
#
91+
# ----------------------------------------------------------------------------------------------------
92+
93+
- name: PR Source - ${{ env.PR_SOURCE }}
94+
if: env.HAS_CODE_CHANGES == 'true'
95+
run: echo "PRs can come from a branch or a fork. This PR is from a ${{ env.PR_SOURCE }}."
96+
97+
- name: Checkout
98+
if: env.HAS_CODE_CHANGES == 'true'
99+
uses: actions/checkout@v3
100+
101+
# -----------------------------------
102+
# Check if action has been recompiled
103+
# -----------------------------------
104+
- name: If action has build step - Setup Node 16.x
105+
uses: actions/setup-node@v3
106+
if: env.HAS_CODE_CHANGES == 'true' && env.HAS_BUILD_STEP == 'true'
107+
with:
108+
node-version: 16.x
109+
110+
- name: If action has build step - Build the action
111+
if: env.HAS_CODE_CHANGES == 'true' && env.HAS_BUILD_STEP == 'true'
112+
run: 'npm run build'
113+
114+
- name: If action has build step - Check for unstaged build changes (open for details)
115+
if: env.HAS_CODE_CHANGES == 'true' && env.HAS_BUILD_STEP == 'true'
116+
run: |
117+
if [[ "$(git status --porcelain)" != "" ]]; then
118+
echo "There action needs to be re-built."
119+
echo "NEEDS_BUILD_COMMIT=true" >> "$GITHUB_ENV"
120+
else
121+
echo "The action has already been re-built"
122+
fi
123+
124+
# -------------------------------------
125+
# Check if README needs version updates
126+
# -------------------------------------
127+
- name: ${{ env.README }} - Update version to @${{ env.NEXT_VERSION }}
128+
if: env.HAS_CODE_CHANGES == 'true'
129+
id: update-readme
130+
uses: im-open/update-action-version-in-file@v1
131+
with:
132+
file-to-update: ./${{ env.README }} # Default: 'README.md'
133+
action-name: ${{ github.repository }}
134+
updated-version: ${{ env.NEXT_VERSION }}
135+
136+
- name: ${{ env.README }} - Check for unstaged version changes (open for details)
137+
if: env.HAS_CODE_CHANGES == 'true'
138+
run: |
139+
if [ "${{ steps.update-readme.outputs.has-changes }}" == "true" ]; then
140+
echo "README.md needs version updates."
141+
echo "NEEDS_README_COMMIT=true" >> "$GITHUB_ENV"
142+
else
143+
echo "README.md does not need version updates."
144+
fi
145+
146+
# -------------------------------------------
147+
# Fail the workflow if any updates are needed
148+
# -------------------------------------------
149+
- name: Fail the workflow if there are any outstanding changes
150+
if: env.HAS_CODE_CHANGES == 'true' && (env.NEEDS_BUILD_COMMIT == 'true' || env.NEEDS_README_COMMIT == 'true')
151+
id: summary
152+
uses: actions/github-script@v6
153+
with:
154+
script: |
155+
156+
// Setup vars for the script to use
157+
const hasBuildStep = ${{ env.HAS_BUILD_STEP }};
158+
const needsBuildChanges = hasBuildStep && ${{ env.NEEDS_BUILD_COMMIT }};
159+
const needsReadmeChanges = ${{ env.NEEDS_README_COMMIT }};
160+
161+
const contribId = '#contributing';
162+
const contributionLink = `https://github.com/${{ github.repository }}${contribId}`;
163+
const contributingTitle = contribId.replace('#', '').split('-').map(w => { return w.slice(0, 1).toUpperCase() + w.slice(1) }).join(' ');
164+
165+
const exampleId = '#usage-examples';
166+
const readmeLink = `${{ github.event.pull_request.head.repo.html_url }}/blob/${{ github.event.pull_request.head.ref }}/${{ env.README }}`;
167+
const readmeExampleLink = `${readmeLink}${exampleId}`;
168+
const readmeExampleTitle = exampleId.replace('#', '').split('-').map(w => { return w.slice(0, 1).toUpperCase() + w.slice(1) }).join(' ');
169+
170+
// Construct the instructions for fixing the PR
171+
let instructions = `Before this PR can be merged, the following item(s) should be addressed to comply with the action's ${contributingTitle} Guidelines.`
172+
if (needsReadmeChanges) {
173+
instructions += `
174+
- Please update the action's version in the ${readmeExampleTitle} section of ${{ env.README }}. Each instance of this action should be updated to:
175+
\`uses: ${{ github.repository }}@${{ env.NEXT_VERSION }}\``;
176+
}
177+
if (needsBuildChanges){
178+
instructions += `
179+
- Please ensure the action has been recompiled by running the following command from the root of the repository:
180+
\`npm run build\``;
181+
}
182+
183+
// Update the instructions with links
184+
let instructionsWithLinks = instructions
185+
.replace('of ${{ env.README }}.', `of [${{ env.README }}](${readmeLink}).`)
186+
.replace(`${contributingTitle} Guidelines`, `[${contributingTitle} Guidelines](${contributionLink})`)
187+
.replace(readmeExampleTitle, `[${readmeExampleTitle}](${readmeExampleLink})`);
188+
189+
// Comment on PR for branches. A fork's GH_TOKEN only has 'read' permission on all scopes so a comment cannot be made.
190+
if (!${{ env.IS_FORK }}) {
191+
github.rest.issues.createComment({
192+
issue_number: context.issue.number,
193+
owner: context.repo.owner,
194+
repo: context.repo.repo,
195+
body: instructionsWithLinks
196+
})
197+
}
198+
199+
// Add workflow summary & fail the build
200+
core.summary
201+
.addRaw(instructionsWithLinks)
202+
.write();
203+
core.setFailed(instructions);

.github/workflows/build.yml

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)