Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
bde034ba7a
|
|||
3e09e510da
|
|||
131ab9e789
|
|||
255ff55cee
|
|||
bb0c14b39f
|
|||
1ffba72b6b
|
|||
af6b41a0ae
|
|||
26a8759919
|
|||
62c3f0e00f
|
|||
dff4090678
|
|||
0c8e75e82e
|
|||
715c984e40
|
|||
9d348117f8
|
|||
81966c095c
|
|||
23cfe0f6ea
|
|||
b4efe0d259
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*.pyc
|
28
.gitlab-ci.yml
Normal file
28
.gitlab-ci.yml
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
stages:
|
||||||
|
- check
|
||||||
|
- release
|
||||||
|
|
||||||
|
build:
|
||||||
|
stage: check
|
||||||
|
image: registry.gitlab.com/wolutator/base-build-env:latest
|
||||||
|
tags:
|
||||||
|
- hottis
|
||||||
|
- linux
|
||||||
|
- docker
|
||||||
|
script:
|
||||||
|
- for I in *.py; do python -m py_compile $I; done
|
||||||
|
- for I in *.py; do python -m pycodestyle --max-line-length=120 $I; done
|
||||||
|
|
||||||
|
release:
|
||||||
|
stage: release
|
||||||
|
image: registry.gitlab.com/wolutator/base-build-env
|
||||||
|
tags:
|
||||||
|
- hottis
|
||||||
|
- linux
|
||||||
|
- docker
|
||||||
|
only:
|
||||||
|
- release
|
||||||
|
script:
|
||||||
|
- python gitlabreleaseuploader.py -p $PRIVATE_TOKEN -i $CI_PROJECT_ID -u $CI_PROJECT_URL
|
||||||
|
-f gitlabreleaseuploader.py -F info.json -T $CI_COMMIT_REF_NAME
|
||||||
|
|
139
gitlabreleaseuploader.py
Normal file → Executable file
139
gitlabreleaseuploader.py
Normal file → Executable file
@ -1,20 +1,53 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Gitlab Release Uploader')
|
parser = argparse.ArgumentParser(description='Gitlab Release Uploader')
|
||||||
parser.add_argument('--privateToken', '-p', help='Private token to access Gitlab', required=True)
|
parser.add_argument('--privateToken', '-p',
|
||||||
parser.add_argument('--projectId', '-i', help='ProjectID of the related project', required=True)
|
help='Private token to access Gitlab', required=True)
|
||||||
parser.add_argument('--projectUrl', '-u', help='URL of the related project at Gitlab', required=True)
|
parser.add_argument('--projectId', '-i',
|
||||||
parser.add_argument('--file', '-f', help='File to be released', required=True)
|
help='ProjectID of the related project', required=True)
|
||||||
parser.add_argument('--releaseName', '-n', help='Name of the release', required=True)
|
parser.add_argument('--projectUrl', '-u',
|
||||||
parser.add_argument('--releaseTag', '-t', help='Tag of the release in the repo, make sure it already exists', required=True)
|
help='URL of the related project at Gitlab', required=True)
|
||||||
parser.add_argument('--description', '-d', help='Description of the release', required=True)
|
parser.add_argument('--file', '-f',
|
||||||
|
help='File to be released', required=True)
|
||||||
|
parser.add_argument('--releaseName', '-n',
|
||||||
|
help='Name of the release', required=False,
|
||||||
|
default='')
|
||||||
|
parser.add_argument('--releaseTag', '-t',
|
||||||
|
help='Tag of the release in the repo',
|
||||||
|
required=False,
|
||||||
|
default='')
|
||||||
|
parser.add_argument('--releaseTagTarget', '-T',
|
||||||
|
help='Commit or branch the tag should point to',
|
||||||
|
required=False,
|
||||||
|
default='')
|
||||||
|
parser.add_argument('--createReleaseTag', '-c',
|
||||||
|
help='Shall the release be created here',
|
||||||
|
required=False,
|
||||||
|
action='store_true',
|
||||||
|
default=False)
|
||||||
|
parser.add_argument('--description', '-d',
|
||||||
|
help='Description of the release', required=False,
|
||||||
|
default='')
|
||||||
|
parser.add_argument('--releaseInfoFile', '-F',
|
||||||
|
help='File containing JSON object with release info '
|
||||||
|
'(release tag, create release tag, description',
|
||||||
|
required=False,
|
||||||
|
default='')
|
||||||
|
parser.add_argument('--instanceUrl', '-I',
|
||||||
|
help='URL of your gitlab instance', required=False,
|
||||||
|
default='https://gitlab.com')
|
||||||
|
parser.add_argument('--verbose', '-v',
|
||||||
|
help='verbose output',
|
||||||
|
required=False,
|
||||||
|
action='store_true',
|
||||||
|
default=False)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
print(args)
|
|
||||||
|
|
||||||
privateToken = args.privateToken
|
privateToken = args.privateToken
|
||||||
projectId = args.projectId
|
projectId = args.projectId
|
||||||
@ -22,27 +55,91 @@ projectUrl = args.projectUrl
|
|||||||
fileToUpload = args.file
|
fileToUpload = args.file
|
||||||
releaseName = args.releaseName
|
releaseName = args.releaseName
|
||||||
releaseTag = args.releaseTag
|
releaseTag = args.releaseTag
|
||||||
|
releaseTagTarget = args.releaseTagTarget
|
||||||
releaseDescription = args.description
|
releaseDescription = args.description
|
||||||
|
instanceUrl = args.instanceUrl
|
||||||
|
createReleaseTag = args.createReleaseTag
|
||||||
|
releaseInfoFilename = args.releaseInfoFile
|
||||||
|
verbose = args.verbose
|
||||||
|
releaseInfo = {}
|
||||||
|
|
||||||
|
if (releaseInfoFilename):
|
||||||
|
with open(releaseInfoFilename, 'r') as releaseInfoFile:
|
||||||
|
releaseInfo = releaseInfoFile.read()
|
||||||
|
releaseInfo = json.loads(releaseInfo)
|
||||||
|
if 'releaseName' in releaseInfo:
|
||||||
|
releaseName = releaseInfo['releaseName']
|
||||||
|
if 'releaseTag' in releaseInfo:
|
||||||
|
releaseTag = releaseInfo['releaseTag']
|
||||||
|
if 'releaseTagTarget' in releaseInfo:
|
||||||
|
releaseTagTarget = releaseInfo['releaseTagTarget']
|
||||||
|
if 'createReleaseTag' in releaseInfo:
|
||||||
|
createReleaseTag = (releaseInfo['createReleaseTag'] in
|
||||||
|
('true', 'True'))
|
||||||
|
if 'description' in releaseInfo:
|
||||||
|
releaseDescription = releaseInfo['description']
|
||||||
|
|
||||||
|
if releaseName == '':
|
||||||
|
raise Exception('No release name given')
|
||||||
|
if releaseTag == '':
|
||||||
|
raise Exception('No release tag given')
|
||||||
|
if releaseTagTarget == '':
|
||||||
|
raise Exception('No release tag target given')
|
||||||
|
if releaseDescription == '':
|
||||||
|
raise Exception('No release description given')
|
||||||
|
|
||||||
|
|
||||||
|
# --- upload the file
|
||||||
uploadUrl = "https://gitlab.com/api/v4/projects/%s/uploads" % projectId
|
uploadUrl = "%s/api/v4/projects/%s/uploads" % (instanceUrl, projectId)
|
||||||
createReleaseUrl = "https://gitlab.com/api/v4/projects/%s/releases" % projectId
|
|
||||||
|
|
||||||
|
|
||||||
headers = {"PRIVATE-TOKEN": privateToken}
|
headers = {"PRIVATE-TOKEN": privateToken}
|
||||||
|
|
||||||
files = {"file": open(fileToUpload, 'rb')}
|
files = {"file": open(fileToUpload, 'rb')}
|
||||||
|
|
||||||
uploadResult = requests.post(uploadUrl, files=files, headers=headers)
|
uploadResult = requests.post(uploadUrl, files=files, headers=headers)
|
||||||
|
|
||||||
|
if uploadResult.status_code != 201:
|
||||||
print(uploadResult)
|
print(uploadResult)
|
||||||
print(uploadResult.text)
|
print(uploadResult.text)
|
||||||
if uploadResult.status_code != 201:
|
|
||||||
raise Exception('Unable to upload file to Gitlab')
|
raise Exception('Unable to upload file to Gitlab')
|
||||||
|
|
||||||
|
if verbose:
|
||||||
|
print(uploadResult)
|
||||||
|
print(uploadResult.text)
|
||||||
|
print('File successfully uploaded')
|
||||||
|
|
||||||
uploadResultJson = json.loads(uploadResult.text)
|
uploadResultJson = json.loads(uploadResult.text)
|
||||||
|
|
||||||
|
# --- create release tag
|
||||||
|
if createReleaseTag:
|
||||||
|
createReleaseTagUrl = ("%s/api/v4//projects/%s/repository/tags" %
|
||||||
|
(instanceUrl, projectId))
|
||||||
|
headers = {"PRIVATE-TOKEN": privateToken,
|
||||||
|
"Content-Type": "application/json"}
|
||||||
|
|
||||||
|
payloadCreateReleaseTag = {
|
||||||
|
"tag_name": releaseTag,
|
||||||
|
"id": projectId,
|
||||||
|
"ref": releaseTagTarget,
|
||||||
|
"message": "Tag for release %s" % releaseName
|
||||||
|
}
|
||||||
|
|
||||||
|
createReleaseTagResult = requests.post(createReleaseTagUrl,
|
||||||
|
headers=headers,
|
||||||
|
data=json.dumps(payloadCreateReleaseTag))
|
||||||
|
|
||||||
|
if createReleaseTagResult.status_code != 201:
|
||||||
|
print(createReleaseTagResult)
|
||||||
|
print(createReleaseTagResult.text)
|
||||||
|
raise Exception('Unable to create release tag')
|
||||||
|
|
||||||
|
if verbose:
|
||||||
|
print(createReleaseTagResult)
|
||||||
|
print(createReleaseTagResult.text)
|
||||||
|
print('Tag successfully created')
|
||||||
|
|
||||||
|
|
||||||
|
# --- create release
|
||||||
|
createReleaseUrl = "%s/api/v4/projects/%s/releases" % (instanceUrl, projectId)
|
||||||
|
headers = {"PRIVATE-TOKEN": privateToken, "Content-Type": "application/json"}
|
||||||
fullUrl = projectUrl + uploadResultJson['url']
|
fullUrl = projectUrl + uploadResultJson['url']
|
||||||
|
|
||||||
payloadCreateRelease = {
|
payloadCreateRelease = {
|
||||||
@ -59,11 +156,15 @@ payloadCreateRelease = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
headers = { "PRIVATE-TOKEN" : privateToken, "Content-Type": "application/json"}
|
createReleaseResult = requests.post(createReleaseUrl, headers=headers,
|
||||||
|
data=json.dumps(payloadCreateRelease))
|
||||||
|
|
||||||
createReleaseResult = requests.post(createReleaseUrl, headers=headers, data=json.dumps(payloadCreateRelease))
|
if createReleaseResult.status_code != 201:
|
||||||
print(createReleaseResult)
|
print(createReleaseResult)
|
||||||
print(createReleaseResult.text)
|
print(createReleaseResult.text)
|
||||||
if createReleaseResult.status_code != 201:
|
|
||||||
raise Exception('Unable to create release')
|
raise Exception('Unable to create release')
|
||||||
|
|
||||||
|
if verbose:
|
||||||
|
print(createReleaseResult)
|
||||||
|
print(createReleaseResult.text)
|
||||||
|
print('Release successfully created')
|
||||||
|
7
info.json
Normal file
7
info.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"releaseTag": "v1.6",
|
||||||
|
"createReleaseTag": "true",
|
||||||
|
"releaseName": "Seventh release of the uploader",
|
||||||
|
"description": "Work on readme and change of CI script"
|
||||||
|
}
|
||||||
|
|
110
readme.md
Normal file
110
readme.md
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
# Release Uploader for Gitlab
|
||||||
|
|
||||||
|
Creating releases on Gitlab seems to work only via the REST API.
|
||||||
|
|
||||||
|
This Python script handles the upload of a release file, the creation of
|
||||||
|
the required release tag in the repository and the creation of the actual
|
||||||
|
release referring to both the tag and the file.
|
||||||
|
|
||||||
|
It supports commandline options and an input file, adjusted to be used in
|
||||||
|
Gitlab CI scripts.
|
||||||
|
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
wn@tron:~/workspace-python/gitlabreleaseuploader [master ≡]$ ./gitlabreleaseuploader.py -h
|
||||||
|
usage: gitlabreleaseuploader.py [-h] --privateToken PRIVATETOKEN --projectId
|
||||||
|
PROJECTID --projectUrl PROJECTURL --file FILE
|
||||||
|
[--releaseName RELEASENAME]
|
||||||
|
[--releaseTag RELEASETAG]
|
||||||
|
[--releaseTagTarget RELEASETAGTARGET]
|
||||||
|
[--createReleaseTag]
|
||||||
|
[--description DESCRIPTION]
|
||||||
|
[--releaseInfoFile RELEASEINFOFILE]
|
||||||
|
[--instanceUrl INSTANCEURL] [--verbose]
|
||||||
|
|
||||||
|
Gitlab Release Uploader
|
||||||
|
|
||||||
|
optional arguments:
|
||||||
|
-h, --help show this help message and exit
|
||||||
|
--privateToken PRIVATETOKEN, -p PRIVATETOKEN
|
||||||
|
Private token to access Gitlab
|
||||||
|
--projectId PROJECTID, -i PROJECTID
|
||||||
|
ProjectID of the related project
|
||||||
|
--projectUrl PROJECTURL, -u PROJECTURL
|
||||||
|
URL of the related project at Gitlab
|
||||||
|
--file FILE, -f FILE File to be released
|
||||||
|
--releaseName RELEASENAME, -n RELEASENAME
|
||||||
|
Name of the release
|
||||||
|
--releaseTag RELEASETAG, -t RELEASETAG
|
||||||
|
Tag of the release in the repo
|
||||||
|
--releaseTagTarget RELEASETAGTARGET, -T RELEASETAGTARGET
|
||||||
|
Commit or branch the tag should point to
|
||||||
|
--createReleaseTag, -c
|
||||||
|
Shall the release be created here
|
||||||
|
--description DESCRIPTION, -d DESCRIPTION
|
||||||
|
Description of the release
|
||||||
|
--releaseInfoFile RELEASEINFOFILE, -F RELEASEINFOFILE
|
||||||
|
File containing JSON object with release info (release
|
||||||
|
tag, create release tag, description
|
||||||
|
--instanceUrl INSTANCEURL, -I INSTANCEURL
|
||||||
|
URL of your gitlab instance
|
||||||
|
--verbose, -v verbose output
|
||||||
|
wn@tron:~/workspace-python/gitlabreleaseuploader [master ≡]$
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Without release info file:
|
||||||
|
|
||||||
|
gitlabreleaseuploader.py -p PRIVATE_TOKEN -i PROJECT_ID -u PROJECT_URL
|
||||||
|
-f FILE_TO_BE_RELEASED -n RELEASE_NAME
|
||||||
|
-t RELEASE_TAG -T RELEASE_TAG_TARGET -c
|
||||||
|
-d RELEASE_DESCRIPTION -I INSTANCE_URL
|
||||||
|
|
||||||
|
|
||||||
|
### With release info file:
|
||||||
|
|
||||||
|
Release Info File:
|
||||||
|
|
||||||
|
{
|
||||||
|
"releaseTag": "v1.5",
|
||||||
|
"createReleaseTag": "true",
|
||||||
|
"releaseName": "Sixth release of the uploader",
|
||||||
|
"description": "Now with CI script and automatic release upload (he, eat your own dogfood)"
|
||||||
|
}
|
||||||
|
|
||||||
|
Command:
|
||||||
|
|
||||||
|
gitlabreleaseuploader.py -p PRIVATE_TOKEN -i PROJECT_ID -u PROJECT_URL
|
||||||
|
-f FILE_TO_BE_RELEASED -F RELEASE_INFO_FILE
|
||||||
|
|
||||||
|
|
||||||
|
### Use in CI script
|
||||||
|
|
||||||
|
The PRIVATE_TOKEN should not be entered directly into the CI script but added as protected a CI variable to you particular project.
|
||||||
|
|
||||||
|
Then add the following stage into your CI script:
|
||||||
|
|
||||||
|
release:
|
||||||
|
stage: release
|
||||||
|
image: registry.gitlab.com/wolutator/base-build-env
|
||||||
|
only:
|
||||||
|
- release
|
||||||
|
script:
|
||||||
|
- gitlabreleaseuploader.py -p $PRIVATE_TOKEN -i $CI_PROJECT_ID -u $CI_PROJECT_URL
|
||||||
|
-f gitlabreleaseuploader.py -F info.json -T $CI_COMMIT_REF_NAME
|
||||||
|
|
||||||
|
(Note, please: this stage definition is for a docker worker. It uses my ``base-build-env`` image, which already contains the
|
||||||
|
uploader script. However, it is no problem to use the uploader script also in different environments. It just depends on the
|
||||||
|
``Requests`` package.)
|
||||||
|
|
||||||
|
Most information are taken from CI builtin variables, the tag name, release name and release description should be stored in
|
||||||
|
the release info file (here: ``info.json``), which is under version control.
|
||||||
|
|
||||||
|
To avoid releasing a file with everything single push to an arbitrary branch, add the ``only`` block.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user