-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_asset
More file actions
executable file
·96 lines (74 loc) · 3.31 KB
/
create_asset
File metadata and controls
executable file
·96 lines (74 loc) · 3.31 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
#!/bin/bash
set -euo pipefail
# Colors
GREEN="\033[0;32m"
BLUE="\033[0;34m"
YELLOW="\033[1;33m"
RED="\033[0;31m"
NC="\033[0m"
# Spinner state
SPINNER_PID=""
SPINNER_MSG=""
start_spinner() {
SPINNER_MSG="$1"
echo -ne "${BLUE}==>${NC} $SPINNER_MSG..."
(
while true; do
for c in / - \\ \|; do
echo -ne "\r${BLUE}==>${NC} $SPINNER_MSG... $c"
sleep 0.1
done
done
) &
SPINNER_PID=$!
disown
}
stop_spinner_success() {
kill "$SPINNER_PID" >/dev/null 2>&1 || true
wait "$SPINNER_PID" 2>/dev/null || true
echo -ne "\r\033[K"
echo -e "${GREEN}✔${NC} $SPINNER_MSG"
}
stop_spinner_error() {
kill "$SPINNER_PID" >/dev/null 2>&1 || true
wait "$SPINNER_PID" 2>/dev/null || true
echo -ne "\r\033[K"
echo -e "${RED}✖${NC} $SPINNER_MSG"
exit 1
}
# Validate NRN
start_spinner "Validating that the NRN has been loaded into the environment."
if [ -z "${NRN:-}" ]; then
stop_spinner_error "NRN is not set. Please export the NRN variable before running the script."
fi
APPLICATION_ID=$(echo "$NRN" | sed -n 's/.*application=\([0-9]*\).*/\1/p')
stop_spinner_success "The NRN was found and parsed successfully (application_id=$APPLICATION_ID)."
# Build creation
start_spinner "Creating a new build for application_id=$APPLICATION_ID."
{
BUILD=$(np build create --format json --body "{\"application_id\": $APPLICATION_ID, \"branch\": \"main\",\"commit\": {\"id\": \"demo-custom-scopes\",\"permalink\": \"link\"}, \"description\":\"custom scope demo\"}" 2>/dev/null) || stop_spinner_error "An error occurred while creating the build."
BUILD_ID=$(echo "$BUILD" | jq -r .id)
} || stop_spinner_error "Unable to parse the build response."
stop_spinner_success "The build was created successfully (build_id=$BUILD_ID)."
# Asset creation
start_spinner "Registering an asset associated with build_id=$BUILD_ID."
{
ASSET=$(np asset create --format json --body "{\"metadata\": {}, \"type\": \"docker-image\",\"url\": \"federicomalehnullplatform/custom-scopes-demo:latest\",\"name\": \"My asset\",\"build_id\": $BUILD_ID,\"application_id\": $APPLICATION_ID, \"commit-sha\": \"demo-custom-scopes\"}" 2>/dev/null) || stop_spinner_error "An error occurred while registering the asset."
ASSET_ID=$(echo "$ASSET" | jq -r .id)
} || stop_spinner_error "Unable to parse the asset response."
stop_spinner_success "The asset was registered successfully (asset_id=$ASSET_ID)."
# Build update
start_spinner "Updating the build status to 'successful'."
(
np build update --id "$BUILD_ID" --status successful >/dev/null 2>&1
) || stop_spinner_error "An error occurred while updating the build status."
stop_spinner_success "The build status was updated successfully."
# Release creation
VERSION=0.0.2
start_spinner "Creating a new release (version $VERSION) for build_id=$BUILD_ID and application_id=$APPLICATION_ID."
{
RELEASE=$(np release create --format json --body "{\"status\": \"active\", \"build_id\": $BUILD_ID, \"application_id\": $APPLICATION_ID, \"semver\": \"$VERSION\"}" 2>/dev/null) || stop_spinner_error "An error occurred while creating the release."
RELEASE_ID=$(echo "$RELEASE" | jq -r .id)
} || stop_spinner_error "Unable to parse the release response."
stop_spinner_success "The release was created successfully (release_id=$RELEASE_ID)."
echo -e "${GREEN}✔${NC} The deployment pipeline completed successfully."