Skip to content

Commit a5f686a

Browse files
authored
Extract script (#27)
* Extract command steps into bash script * Convert repository parameter to env var * Add check for no tar.gz or whl files
1 parent bf70613 commit a5f686a

File tree

2 files changed

+67
-33
lines changed

2 files changed

+67
-33
lines changed

src/commands/upload_python_package.yml

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,13 @@ steps:
2929
- authenticate_with_oidc:
3030
service_account: <<parameters.service_account>>
3131
- run:
32-
name: Cloudsmith - Check dist_dir exists
32+
name: Cloudsmith - Configure repository and dist_dir
3333
command: |
34-
if [ -d "<<parameters.dist_dir>>" ]; then
35-
echo "<<parameters.dist_dir>> is a valid directory"
36-
else
37-
echo "<<parameters.dist_dir>> is not a directory"
38-
exit 1
39-
fi
40-
- run:
41-
name: Cloudsmith - Upload source distribution(s)
42-
command: |
43-
for filename in <<parameters.dist_dir>>/*.tar.gz
44-
do
45-
[ -f "$filename" ] || continue
46-
47-
echo "Uploading source distribution $filename to Cloudsmith repository $CLOUDSMITH_ORGANISATION/<<parameters.repository>> ..."
48-
49-
cloudsmith push python --verbose --api-key "$CLOUDSMITH_OIDC_TOKEN" "$CLOUDSMITH_ORGANISATION/<<parameters.repository>>" "$filename"
34+
echo "export CLOUDSMITH_REPOSITORY=\"<<parameters.repository>>\"" >> $BASH_ENV
35+
echo "CLOUDSMITH_REPOSITORY=<<parameters.repository>>"
5036
51-
echo ""
52-
echo "Package upload and synchronisation completed OK."
53-
done
37+
echo "export DIST_DIR=\"<<parameters.dist_dir>>\"" >> $BASH_ENV
38+
echo "DIST_DIR=<<parameters.dist_dir>>"
5439
- run:
55-
name: Cloudsmith - Upload wheel distribution(s)
56-
command: |
57-
for filename in <<parameters.dist_dir>>/*.whl
58-
do
59-
[ -f "$filename" ] || continue
60-
61-
echo "Uploading wheel $filename to Cloudsmith repository $CLOUDSMITH_ORGANISATION/<<parameters.repository>> ..."
62-
63-
cloudsmith push python --verbose --api-key "$CLOUDSMITH_OIDC_TOKEN" "$CLOUDSMITH_ORGANISATION/<<parameters.repository>>" "$filename"
64-
65-
echo ""
66-
echo "Package upload and synchronisation completed OK."
67-
done
40+
name: Cloudsmith - Upload python package file(s)
41+
command: <<include(scripts/upload_python_package.sh)>>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
3+
# shellcheck disable=SC2016
4+
# shellcheck disable=SC2129
5+
6+
# Check required environment variables are set and look valid
7+
if [ -z "$CLOUDSMITH_ORGANISATION" ]
8+
then
9+
echo "Unable to upload package. Env var CLOUDSMITH_ORGANISATION is not defined."
10+
exit 1
11+
fi
12+
13+
if [ -z "$CLOUDSMITH_REPOSITORY" ]
14+
then
15+
echo "Unable to upload package. Env var CLOUDSMITH_REPOSITORY is not defined."
16+
exit 1
17+
fi
18+
19+
if [ -d "$DIST_DIR" ]; then
20+
echo "$DIST_DIR is a valid directory."
21+
else
22+
echo "$DIST_DIR is not a directory."
23+
exit 1
24+
fi
25+
26+
# Check there are tar.gz or whl files to upload
27+
LS_TAR_GZ_CMD="ls -A ${DIST_DIR}/*.tar.gz"
28+
LS_WHL_CMD="ls -A ${DIST_DIR}/*.whl"
29+
30+
if [ -z "$($LS_TAR_GZ_CMD)" ] && [ -z "$($LS_WHL_CMD)" ]
31+
then
32+
echo "$DIST_DIR does not contain any tar.gz or whl files to upload."
33+
exit 1
34+
fi
35+
36+
# Upload source distribution if present
37+
for filename in "$DIST_DIR"/*.tar.gz
38+
do
39+
[ -f "$filename" ] || continue
40+
41+
echo "Uploading source distribution $filename to Cloudsmith repository $CLOUDSMITH_ORGANISATION/$CLOUDSMITH_REPOSITORY ..."
42+
43+
cloudsmith push python --verbose --api-key "$CLOUDSMITH_OIDC_TOKEN" "$CLOUDSMITH_ORGANISATION/$CLOUDSMITH_REPOSITORY" "$filename"
44+
45+
echo ""
46+
echo "Package upload and synchronisation completed OK."
47+
done
48+
49+
# Upload wheel distribution(s) if present
50+
for filename in "$DIST_DIR"/*.whl
51+
do
52+
[ -f "$filename" ] || continue
53+
54+
echo "Uploading wheel $filename to Cloudsmith repository $CLOUDSMITH_ORGANISATION/$CLOUDSMITH_REPOSITORY ..."
55+
56+
cloudsmith push python --verbose --api-key "$CLOUDSMITH_OIDC_TOKEN" "$CLOUDSMITH_ORGANISATION/$CLOUDSMITH_REPOSITORY" "$filename"
57+
58+
echo ""
59+
echo "Package upload and synchronisation completed OK."
60+
done

0 commit comments

Comments
 (0)