Skip to content

Commit

Permalink
Create jfrog upload artefacts gh action
Browse files Browse the repository at this point in the history
  • Loading branch information
msobo1 committed Oct 27, 2023
1 parent e4d5568 commit 89b37cf
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
37 changes: 37 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: 'Upload to JFrog'
description: 'Upload build artefacts to JFrog general registry.'
inputs:
access_token:
description: 'Access token for JFrog service account. Find it in org secrets.'
required: true
user_name:
description: 'Define release tag.'
required: true
registry_url:
description: 'Define release repository.'
required: true
artefacts_patterns:
description: 'Define patterns for files which are going to be uploaded - pytyhon regex.'
required: false
default: '*'
action_shell:
required: false
default: 'bash'

runs:
using: "composite"
steps:
- uses: actions/setup-python@v4
if: ${{ runner.environment == 'github-hosted' }}
with:
python-version: '3.11'

- name: 'Upload artefacts'
run: |
python3 ${{ github.action_path }}/artefacts2jfrog.py
shell: ${{ inputs.action_shell }}
env:
ARTEFACTS_PATTERNS: ${{ inputs.artefacts_patterns }}
REGISTRY_URL: ${{ inputs.registry_url }}
ACCESS_TOKEN: ${{ inputs.access_token }}
SERVICE_ACCOUNT: ${{ inputs.user_name }}
58 changes: 58 additions & 0 deletions artefacts2jfrog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3

import os
import re
import requests
import subprocess
import hashlib
import glob


patterns = os.environ["ARTEFACTS_PATTERNS"].split()
print(f"Looking for following patterns \n {patterns}")

def base_path(path_):
while 1:
path_ = os.path.split(path_)[0]
if "*" not in path_:
break
return path_

def find_artefacts():
artefacts = list()
for pattern in patterns:
for file_path in glob.iglob(pattern):
artefacts += [file_path]

return artefacts

def upload_artefacts(file_path):
jfrog_url = os.environ["REGISTRY_URL"]
jfrog_token = os.environ["ACCESS_TOKEN"]
jfrog_user = os.environ["SERVICE_ACCOUNT"]

file_name = os.path.split(file_path)[1]
# add check of sha for downloads
headers = {'content_type': 'application/octet-stream'}
jfrog_url_ = os.path.join(jfrog_url, file_name)

with open(file_path, 'rb') as f:
r = requests.put(jfrog_url_,
auth=(jfrog_user,
jfrog_token),
data=f,
headers=headers)
if r.status_code != 201:
print("Something went wrong! Check response:")
print(r.json())
raise IOError("Download error")
else:
print(f"#### {file_name} download url:\n{r.json()['downloadUri']}")


if __name__ == '__main__':
artefacts_ = find_artefacts()
print("Found following artefacts:")
print(artefacts_)
for file_path in artefacts_:
upload_artefacts(file_path)

0 comments on commit 89b37cf

Please sign in to comment.