-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yaml
280 lines (246 loc) · 9.26 KB
/
action.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
---
# SPDX-License-Identifier: Apache-2.0
# Copyright 2024 The Linux Foundation <matthew.watkins@linuxfoundation.org>
name: "Nexus Upload"
description: "Nexus Upload Action"
inputs:
NEXUS_USERNAME:
description: "Nexus username"
required: true
NEXUS_PASSWORD:
description: "Nexus password"
required: true
NEXUS_SERVER:
description: "Nexus server"
required: true
NEXUS_REPOSITORY:
description: "Nexus repository"
required: true
UPLOAD_DIRECTORY:
description: "Directory containing files to upload"
required: true
FILENAME_SUFFIX:
description: "File extension to match"
required: false
TESTING:
description: "Boolean"
required: false
default: "false"
options:
- true
- false
# Not implemented yet; repository_format may be superfluous
# We might need it in future, so leaving a reminder here
# repository_format:
# description: "Nexus repository format"
# required: false
outputs:
ERRORS:
description: "Occurence of errors [true|false]"
value: ${{ steps.nexus-upload.outputs.errors }}
SUCCESSES:
description: "Number of successful uploads"
value: ${{ steps.nexus-upload.outputs.successes }}
FAILURES:
description: "Number of failed uploads"
value: ${{ steps.nexus-upload.outputs.failures }}
runs:
using: "composite"
steps:
- name: "Set GitHub Action path"
run: |
# Write GITHUB_ACTION_PATH to GITHUB_PATH
echo "$GITHUB_ACTION_PATH" >> "$GITHUB_PATH"
shell: bash
env:
GITHUB_ACTION_PATH: ${{ github.action_path }}
- name: "Writing cURL configuration file"
shell: bash
# yamllint disable-line rule:line-length
run: |
# Writing/display job configuration
echo "machine ${{ inputs.nexus_server }} > .netrc
echo " login ${{ inputs.nexus_username }} >> .netrc
echo " password ${{ inputs.nexus_password }}" >> .netrc
echo "machine ${{ inputs.nexus_server }} login ${{ inputs.nexus_username }} password ***"
echo "nexus_repository: ${{ inputs.nexus_repository }}"
echo "upload_directory: ${{ inputs.upload_directory }}"
echo "filename_suffix: ${{ inputs.filename_suffix }}"
echo "testing: ${{ inputs.testing }}"
- name: "Uploading files to Nexus"
id: nexus-upload
shell: bash
run: |
# Uploading files to Nexus
#SHELLCODESTART
#!/usr/bin/env bash
# Uncomment to enable debugging
# set -vx
CURL_CONFIG=".netrc"
# Count file upload successes/failures
successes="0"; failures="0"
# Functions
show_help() {
# Command usage help
cat << EOF
Usage: ${0##*/} [-h] -r repository -d directory [-e file extension]
-h display this help and exit
-r remote repository name (mandatory)
-d local directory of files to upload (mandatory)
-e match file extension (optional)
EOF
}
error_help() {
show_help >&2
exit 1
}
status_report() {
if [ "$failures" -eq 0 ]; then
errors="false"; EXIT_STATUS="0"
else
errors="true"; EXIT_STATUS="1"
fi
# Print a helpful status summary
echo "Errors: $errors Successes: $successes Failures: $failures"
if [ -n "$GITHUB_OUTPUT" ]; then
echo "Running as GitHub action; setting output values"
# shellcheck disable=SC2129
echo "errors=$errors" >> "$GITHUB_OUTPUT"
echo "successes=$successes" >> "$GITHUB_OUTPUT"
echo "failures=$failures" >> "$GITHUB_OUTPUT"
fi
if [ "$testing" = "true" ]; then
# Clean directory of any previous auto-generated content
find "$upload_directory" -name upload-*.txt -type f -delete
rmdir "$upload_directory"
fi
exit "$EXIT_STATUS"
}
curl_upload() {
FILE="$1"
echo "Sending: ${FILE}"
if ("$CURL" --no-progress-meter --fail --netrc-file "$CURL_CONFIG" \
--upload-file "$FILE" "$nexus_url"); then #> /dev/null 2>&1
successes=$((successes+1))
else
failures=$((failures+1))
fi
}
process_files() {
for FILE in "${UPLOAD_FILES_ARRAY[@]}"; do
curl_upload "$FILE"
done
}
upload_start() {
# Passthrough function (allows for use from shell/terminal outside GitHub)
CURL=$(which curl)
if [ ! -x "$CURL" ];then
echo "cURL was not found in your PATH"; exit 1
fi
while getopts hr:d:e: opt; do
case $opt in
r) nexus_repository="$OPTARG"
;;
d) upload_directory="$OPTARG"
if [ ! -d "$upload_directory" ]; then
echo "Specified upload directory not found: $upload_directory"
CURRENT_DIR=$(pwd)
echo "Current directory: $CURRENT_DIR"; exit 1
fi
;;
e) file_extension="$OPTARG" # Not mandatory
;;
# Not implemented; this parameter may be superfluous
# repository_format
h|?)
show_help
exit 0
;;
*)
error_help
esac
done
shift "$((OPTIND -1))" # Discard the options
# Check for required parameters and setup
if [ -z ${nexus_repository+x} ]; then
echo "Supplying the repository is mandatory"
errors="true"
fi
if [ -z ${upload_directory+x} ]; then
echo "Supplying the upload directory is mandatory"
errors="true"
fi
if [ ! -f "$CURL_CONFIG" ]; then
echo "cURL configuration file was not found ($CURL_CONFIG)"
errors="true"
else
# The server name is extracted from the configuration file
nexus_server=$(grep machine "$CURL_CONFIG" | awk '{print $2}')
fi
if [ "$errors" = "true" ];then
failures=$((failures+1))
status_report
fi
# Gather files to upload into an array (does not traverse directories recursively)
mapfile -t UPLOAD_FILES_ARRAY \
< <(find "$upload_directory" -name "*$file_extension" -maxdepth 1 -type f -print)
if [ "${#UPLOAD_FILES_ARRAY[@]}" -ne 0 ]; then
echo "Number of files to upload: ${#UPLOAD_FILES_ARRAY[@]}"
else
echo "No files found matching pattern: $upload_directory/*$file_extension"
failures=$((failures+1))
status_report
fi
# Combine separate parameters into nexus_url
if [[ -n ${nexus_server+x} ]] && [[ -n ${nexus_repository+x} ]]; then
nexus_url="https://$nexus_server/repository/$nexus_repository/"
else
echo "A required parameter was not supplied"
echo "nexus_server: $nexus_server"
echo "nexus_repository: $nexus_repository"; exit 1
fi
# Example .netrc configuration file format (indentation matters)
# machine [nexus_server]
# login [nexus_username]
# password [nexus_password]
echo "Attempting uploads to: $nexus_url"
process_files
status_report
}
if [ -z "$nexus_repository" ]; then
nexus_repository="${{ inputs.nexus_repository }}"
fi
if [ -z "$upload_directory" ]; then
upload_directory="${{ inputs.upload_directory }}"
fi
if [ -z "$filename_suffix" ]; then
filename_suffix="${{ inputs.filename_suffix }}"
fi
if [ -z "$testing" ]; then
testing="${{ inputs.testing }}"
fi
if [ -z "$upload_directory" ] || [ -z "$nexus_repository" ]; then
echo "One or more required action parameters NOT set"
echo "nexus_repository: [$nexus_repository]"
echo "upload_directory: [$upload_directory]"; exit 1
fi
if [ "$testing" = "true" ]; then
if [ ! -d "$upload_directory" ]; then
mkdir "$upload_directory"
fi
DATETIME=$(date '+%Y%m%d-%H%M')
echo "Test file $DATETIME" > "$upload_directory/upload-$DATETIME.txt"
fi
# Main script entry point
if [ -z filename_suffix ]; then
upload_start -r "$nexus_repository" -d "$upload_directory"
else
# Matching files by extension is optional; pass it when set
upload_start -r "$nexus_repository" -d "$upload_directory" -e "$filename_suffix"
fi
#SHELLCODEEND
- name: "Removing cURL configuration file"
shell: bash
run: |
# Removing cURL configuration file
rm .netrc