Skip to content

Commit 9add1f0

Browse files
author
Chris Maunder
committed
Adding GitHub Actions to auto-package modules
1 parent 43947b9 commit 9add1f0

File tree

4 files changed

+244
-48
lines changed

4 files changed

+244
-48
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Package and Upload Files
2+
3+
on:
4+
# push:
5+
# branches:
6+
# - main
7+
workflow_dispatch: # Allows manual run
8+
inputs: {} # You can define inputs if needed
9+
10+
jobs:
11+
package_and_upload:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v3
17+
18+
- name: Create module package file
19+
id: create_package
20+
run: |
21+
cd modules/ObjectDetectionYOLOv5-6.2
22+
package_file=$(curl -sL https://raw.githubusercontent.com/codeproject/CodeProject.AI-Server/refs/heads/main/devops/build/create_packages.sh --no-color --github-action | bash)
23+
echo "package_file=$package_file" >> $GITHUB_OUTPUT
24+
25+
- name: Upload ZIP as artifact
26+
uses: actions/upload-artifact@v3
27+
with:
28+
name: ${{ steps.create_package.outputs.package_file }}
29+
path: ${{ steps.create_package.outputs.package_file }}
30+
retention-days: 90 # 90 days is the max

devops/build/create_packages.sh

Lines changed: 105 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,15 @@ lineWidth=70
3030
# folder
3131
createExternalModulePackages=true
3232

33+
# Whether we're creating a package via a github action
34+
githubAction=false
35+
36+
# The current .NET version
3337
dotNetTarget="net8.0"
3438

39+
# Whether we're creating packages for all modules, or just a single module
40+
singleModule=false
41+
3542

3643
# Basic locations
3744

@@ -50,25 +57,20 @@ modulesDir="modules"
5057
# The name of the dir holding the external modules
5158
externalModulesDir="CodeProject.AI-Modules"
5259

60+
# The name of the dir holding the server code itself
61+
serverDir="CodeProject.AI-Server"
5362

54-
# The path to the directory containing this script
55-
#thisScriptDirPath=$(dirname "$0")
56-
thisScriptDirPath="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
57-
58-
# We're assuming this script lives in /devops/build
59-
pushd "${thisScriptDirPath}/../.." >/dev/null
60-
rootDirPath="$(pwd)"
61-
popd >/dev/null
62-
sdkPath="${rootDirPath}/${srcDirName}/${sdkDir}"
63-
utilsScriptsDirPath="${rootDirPath}/src/scripts"
63+
# Location of the utils script in the main CodeProject.AI server repo
64+
utilsScriptGitHubUrl='https://raw.githubusercontent.com/codeproject/CodeProject.AI-Server/refs/heads/main/src/scripts/utils.sh'
6465

6566
# Override some values via parameters ::::::::::::::::::::::::::::::::::::::::::
6667

6768
while [[ $# -gt 0 ]]; do
6869
param=$(echo $1 | tr '[:upper:]' '[:lower:]')
6970

70-
if [ "$param" = "--no-dotnet" ]; then includeDotNet=false; fi
71-
if [ "$param" = "--no-color" ]; then useColor=false; fi
71+
if [ "$param" = "--github-action" ]; then githubAction=true; fi
72+
if [ "$param" = "--no-dotnet" ]; then includeDotNet=false; fi
73+
if [ "$param" = "--no-color" ]; then useColor=false; fi
7274
if [ "$param" = "--verbosity" ]; then
7375
shift
7476
if [[ $# -gt 0 ]]; then
@@ -87,6 +89,35 @@ while [[ $# -gt 0 ]]; do
8789
shift
8890
done
8991

92+
93+
# The path to the directory containing this script
94+
#thisScriptDirPath=$(dirname "$0")
95+
thisScriptDirPath="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
96+
97+
# We're assuming this script lives in /devops/build, but this script could be
98+
# called directly from a module's folder if we're just creating a single package
99+
100+
if [ "$(basename $(cd .. ; pwd))" == "modules" ]; then
101+
# In a /modules/<moduleID> folder
102+
singleModule=true
103+
pushd "${thisScriptDirPath}/../.." >/dev/null
104+
elif [ "$(basename $(cd .. ; pwd))" == "${externalModulesDir}" ]; then
105+
# In /CodeProject.AI-Modules/<moduleID> folder
106+
singleModule=true
107+
pushd "${thisScriptDirPath}/../../%{serverDir}" >/dev/null
108+
else
109+
# Hopefully in the /devops/build folder
110+
pushd "${thisScriptDirPath}/../.." >/dev/null
111+
fi
112+
rootDirPath="$(pwd)"
113+
popd >/dev/null
114+
sdkPath="${rootDirPath}/${srcDirName}/${sdkDir}"
115+
utilsScriptsDirPath="${rootDirPath}/src/scripts"
116+
117+
if [ "${githubAction}" == true ]; then
118+
utilsScriptsDirPath="${utilsScriptGitHubUrl}"
119+
fi
120+
90121
# Standard output may be used as a return value in the functions. Expose stream
91122
# 3 so we can do 'echo "Hello, World!" >&3' within these functions for debugging
92123
# without interfering with return values.
@@ -113,7 +144,9 @@ function correctLineEndings () {
113144
fi
114145
}
115146

116-
correctLineEndings ${utilsScriptsDirPath}/utils.sh
147+
if [ "${githubAction}" != true ]; then
148+
correctLineEndings ${utilsScriptsDirPath}/utils.sh
149+
fi
117150

118151
# "platform" will be set by this script
119152
source ${utilsScriptsDirPath}/utils.sh
@@ -154,24 +187,32 @@ function doModulePackage () {
154187
# version to the package.bat file.
155188
packageVersion=$(getValueFromModuleSettings "modulesettings.json" "${packageModuleId}" "Version")
156189

157-
write "Packaging module ${packageModuleId} ${packageVersion}..." "White"
190+
if [ "${githubAction}" != true ]; then
191+
write "Packaging module ${packageModuleId} ${packageVersion}..." "White"
192+
fi
193+
194+
packageFileName="${packageModuleId}-${packageVersion}.zip"
158195

159-
correctLineEndings package.sh
196+
if [ "${githubAction}" != true ]; then
197+
correctLineEndings package.sh
198+
fi
160199
bash package.sh ${packageModuleId} ${packageVersion}
161200

162201
if [ $? -ne 0 ]; then
163202
writeLine "Error in package.sh for ${packageModuleDirName}" "Red"
164203
fi
165204

166205
popd >/dev/null
167-
206+
168207
# Move package into modules download cache
169-
# echo Moving ${packageModuleDirPath}/${packageModuleId}-${packageVersion}.zip to ${packageDirPath}/
170-
mv -f ${packageModuleDirPath}/${packageModuleId}-${packageVersion}.zip ${packageDirPath}/ >/dev/null
208+
# echo Moving ${packageModuleDirPath}/${packageFileName} to ${packageDirPath}/
209+
mv -f ${packageModuleDirPath}/${packageFileName} ${packageDirPath}/ >/dev/null
171210

172211
if [ $? -ne 0 ]; then
173212
writeLine "Error" "Red"
174213
success="false"
214+
elif [ "${githubAction}" == true ]; then
215+
echo $packageFileName
175216
else
176217
writeLine "done" "DarkGreen"
177218
fi
@@ -190,17 +231,18 @@ if [ ! -d "${packageDirPath}" ]; then mkdir -p "${packageDirPath}"; fi
190231

191232
# Let's go
192233

193-
scriptTitle=' Creating CodeProject.AI Module Downloads'
194-
writeLine
195-
writeLine "$scriptTitle" 'DarkCyan' 'Default' $lineWidth
196-
writeLine
197-
writeLine '======================================================================' 'DarkGreen'
198-
writeLine
199-
writeLine ' CodeProject.AI Packager ' 'DarkGreen'
200-
writeLine
201-
writeLine '======================================================================' 'DarkGreen'
202-
writeLine
203-
234+
if [ "${githubAction}" != true ]; then
235+
scriptTitle=' Creating CodeProject.AI Module Packages'
236+
writeLine
237+
writeLine "$scriptTitle" 'DarkCyan' 'Default' $lineWidth
238+
writeLine
239+
writeLine '======================================================================' 'DarkGreen'
240+
writeLine
241+
writeLine ' CodeProject.AI Packager ' 'DarkGreen'
242+
writeLine
243+
writeLine '======================================================================' 'DarkGreen'
244+
writeLine
245+
fi
204246

205247
if [ "$verbosity" != "quiet" ]; then
206248
writeLine
@@ -215,32 +257,48 @@ fi
215257
# And off we go...
216258
success='true'
217259

218-
# Walk through the internal modules directory and call the setup script in each dir
219-
for d in "${modulesDirPath}/"*/ ; do
220-
packageModuleDirPath=$d
221-
packageModuleDirName="$(basename $d)"
260+
if [ "${singleModule}" == true ]; then
261+
262+
packageModuleDirPath=$(pwd))
263+
packageModuleDirName="$(basename $(pwd))"
222264
packageModuleId=$(getModuleIdFromModuleSettings "${packageModuleDirPath}/modulesettings.json")
223265

224-
if [ "${packageModuleId}" == "" ]; then continue; fi
225-
226-
doModulePackage "$packageModuleId" "$packageModuleDirName" "$packageModuleDirPath"
227-
done
266+
if [ "${packageModuleId}" != "" ]; then
267+
doModulePackage "$packageModuleId" "$packageModuleDirName" "$packageModuleDirPath"
268+
fi
228269

229-
if [ "$createExternalModulePackages" == "true" ]; then
230-
# Walk through the external modules directory and call the setup script in each dir
231-
for d in "${externalModulesDirPath}/"*/ ; do
232-
packageModuleDirName="$(basename $d)"
233-
packageModuleDirPath=$d
270+
else
234271

272+
# Walk through the internal modules directory and call the setup script in each dir
273+
for d in "${modulesDirPath}/"*/ ; do
274+
packageModuleDirPath=$d
275+
packageModuleDirName="$(basename $d)"
235276
packageModuleId=$(getModuleIdFromModuleSettings "${packageModuleDirPath}/modulesettings.json")
236-
if [ "${packageModuleId}" == "" ]; then continue; fi
237277

238-
doModulePackage "$packageModuleId" "$packageModuleDirName" "$packageModuleDirPath"
278+
if [ "${packageModuleId}" != "" ]; then
279+
doModulePackage "$packageModuleId" "$packageModuleDirName" "$packageModuleDirPath"
280+
fi
239281
done
282+
283+
if [ "$createExternalModulePackages" == "true" ]; then
284+
# Walk through the external modules directory and call the setup script in each dir
285+
for d in "${externalModulesDirPath}/"*/ ; do
286+
packageModuleDirName="$(basename $d)"
287+
packageModuleDirPath=$d
288+
packageModuleId=$(getModuleIdFromModuleSettings "${packageModuleDirPath}/modulesettings.json")
289+
290+
if [ "${packageModuleId}" != "" ]; then
291+
doModulePackage "$packageModuleId" "$packageModuleDirName" "$packageModuleDirPath"
292+
fi
293+
done
294+
fi
295+
240296
fi
241297

242-
writeLine
243-
writeLine " Modules packaging Complete" "White" "DarkGreen" $lineWidth
244-
writeLine
298+
if [ "${githubAction}" != true ]; then
299+
writeLine
300+
writeLine " Modules packaging Complete" "White" "DarkGreen" $lineWidth
301+
writeLine
302+
fi
245303

246304
if [ "${success}" == "false" ]; then exit 1; fi

docs/GitHub Workflows.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
To control when a GitHub Action runs, you can configure the triggers in the workflow file. GitHub Actions can be triggered by various events like pushes, pull requests, manual invocation, schedules, and more. You can choose the events that suit your workflow. Here’s how you can configure the action to run only under specific conditions:
2+
3+
### Key Triggers to Control When a GitHub Action Runs:
4+
5+
1. **Manual Trigger (`workflow_dispatch`)**:
6+
- This allows you to run the action manually from the GitHub Actions tab, instead of running automatically on every push.
7+
8+
```yaml
9+
on:
10+
workflow_dispatch:
11+
```
12+
13+
2. **Push Events on Specific Branches**:
14+
- If you only want the action to run on specific branches (e.g., `main`), you can configure the action to only run on pushes to those branches.
15+
16+
```yaml
17+
on:
18+
push:
19+
branches:
20+
- main # Only run on pushes to the 'main' branch
21+
```
22+
23+
3. **Pull Request Events**:
24+
- You can trigger the action to run only when a pull request is opened, updated, or merged.
25+
26+
```yaml
27+
on:
28+
pull_request:
29+
branches:
30+
- main # Only trigger on pull requests to 'main' branch
31+
```
32+
33+
4. **Run on Specific Files**:
34+
- If you only want the action to run when certain files change (e.g., files in a `docs/` folder), you can use the `paths` filter:
35+
36+
```yaml
37+
on:
38+
push:
39+
branches:
40+
- main
41+
paths:
42+
- 'docs/**' # Only run when files in the 'docs/' directory change
43+
```
44+
45+
5. **Scheduled Runs (`schedule`)**:
46+
- You can schedule the action to run periodically (e.g., daily or weekly) using cron syntax.
47+
48+
```yaml
49+
on:
50+
schedule:
51+
- cron: '0 0 * * 1' # Runs every Monday at midnight UTC
52+
```
53+
54+
6. **Tag or Release Events**:
55+
- You can trigger actions based on new tags or releases in the repository:
56+
57+
```yaml
58+
on:
59+
push:
60+
tags:
61+
- 'v*' # Trigger when pushing a version tag like 'v1.0.0'
62+
release:
63+
types: [published] # Trigger on publishing a release
64+
```
65+
66+
### Combining Multiple Triggers
67+
68+
You can also combine multiple triggers if you want the action to run under different conditions, like on manual dispatch or when a tag is pushed.
69+
70+
```yaml
71+
on:
72+
push:
73+
branches:
74+
- main
75+
workflow_dispatch:
76+
pull_request:
77+
branches:
78+
- main
79+
release:
80+
types: [published]
81+
```
82+
83+
### Conditional Execution with `if`
84+
85+
In addition to controlling when the action is triggered, you can add conditions to individual steps to control whether they run based on specific conditions like commit messages, file changes, or environment variables.
86+
87+
Example:
88+
89+
```yaml
90+
jobs:
91+
example_job:
92+
runs-on: ubuntu-latest
93+
steps:
94+
- name: Run only if a specific file changed
95+
if: contains(github.event.head_commit.message, 'Update README')
96+
run: echo "README file updated"
97+
```
98+
99+
### Summary
100+
101+
- **Manual trigger**: Use `workflow_dispatch` to trigger manually.
102+
- **Branch-specific**: Use `on: push` with branch filters to trigger actions only on specific branches.
103+
- **Pull request**: Use `on: pull_request` to run the action when a PR is opened or updated.
104+
- **File-specific**: Use `paths` to limit actions to specific files or directories.
105+
- **Scheduled**: Use `schedule` for periodic runs.
106+
- **Tags/releases**: Use `push: tags` or `release` events for versioned workflows.
107+
108+
By combining these methods, you can control precisely when a GitHub Action runs, avoiding unnecessary executions on every push.

src/setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ exec 3>&1
268268
# dev environment.
269269
setupMode='SetupModule'
270270
currentDirName=$(basename "$(pwd)") # Get current dir name (not full path)
271-
currentDirName=${currentDirName:-/} # correct for the case where pwd=/
271+
currentDirName=${currentDirName:-/} # correct for the case where pwd=/
272272

273273
# Are we in /src? When executionEnvironment = "Development" this may be the case
274274
if [ "$currentDirName" = "$srcDirName" ]; then setupMode='SetupEverything'; fi

0 commit comments

Comments
 (0)