Browse Source

Update Version Bump action for Helm charts (#27)

pull/28/head
Vince Grassia 4 years ago committed by GitHub
parent
commit
060e84547c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      version-bump/Dockerfile
  2. 5
      version-bump/README.md
  3. 50
      version-bump/main.py

3
version-bump/Dockerfile

@ -5,7 +5,8 @@ ADD . /app
WORKDIR /app WORKDIR /app
RUN pip3 install lxml --target=/app RUN pip3 install lxml --target=/app
RUN pip3 install pyyaml --target=/app
ENV PYTHONPATH /app ENV PYTHONPATH /app
ENTRYPOINT [ "python", "/app/main.py" ] ENTRYPOINT [ "python", "/app/main.py" ]

5
version-bump/README.md

@ -1,6 +1,7 @@
# Version Bump Action # Version Bump Action
A Github Action that will replace versions in JSON, PLIST, and XML files. Specifically created for interacting with AndroidManifest, iOS development plists, and Node package JSON files. A Github Action that will replace versions in JSON, PLIST, YAML, and XML files.
Specifically created for interacting with AndroidManifest, iOS development plists, Helm Charts, and Node package JSON files.
## Usage ## Usage
@ -10,4 +11,4 @@ uses: ./version-bump
with: with:
version: ${{ github.event.inputs.version_number }} version: ${{ github.event.inputs.version_number }}
file_path: "./AndroidManifest.xml" file_path: "./AndroidManifest.xml"
``` ```

50
version-bump/main.py

@ -3,44 +3,50 @@ import json
import plistlib import plistlib
import re import re
import lxml.etree as ET import lxml.etree as ET
import yaml
def get_file_type(file): def get_file_type(file_path):
file_type = os.path.splitext(file)[1] file_type = os.path.splitext(file_path)[1]
return file_type return file_type
def update_json(version, file): def get_file_name(file_path):
with open(file) as json_file: file_name = os.path.basename(file_path)
return file_name
def update_json(version, file_path):
with open(file_path) as json_file:
data = json.load(json_file) data = json.load(json_file)
data["version"] = version data["version"] = version
try: try:
data["packages"][""]["version"] = version data["packages"][""]["version"] = version
except KeyError: except KeyError:
pass pass
json.dump(data, open(file, "w"), indent=2) json.dump(data, open(file_path, "w"), indent=2)
with open(file, "a") as f: with open(file_path, "a") as f:
f.write("\n") # Make sure we add the new line back in at EOF. f.write("\n") # Make sure we add the new line back in at EOF.
def update_plist(version, file): def update_plist(version, file_path):
with open(file, "rb") as plist: with open(file_path, "rb") as plist:
pl = plistlib.load(plist) pl = plistlib.load(plist)
pl["CFBundleShortVersionString"] = version pl["CFBundleShortVersionString"] = version
data = pl data = pl
with open(file, "wb") as update_plist: with open(file_path, "wb") as update_plist:
plistlib.dump(data, update_plist, sort_keys=False) plistlib.dump(data, update_plist, sort_keys=False)
def update_xml(version, file): def update_xml(version, file_path):
mytree = ET.parse(file) mytree = ET.parse(file_path)
myroot = mytree.getroot() myroot = mytree.getroot()
# Android Manifests # Android Manifests
if myroot.tag == "manifest": if myroot.tag == "manifest":
with open(file, "r") as f: with open(file_path, "r") as f:
data = f.read() data = f.read()
data_new = re.sub( data_new = re.sub(
'android:versionName="[0-9]+\.[0-9]+\.[0-9]+"', 'android:versionName="[0-9]+\.[0-9]+\.[0-9]+"',
@ -48,18 +54,29 @@ def update_xml(version, file):
data, data,
flags=re.M, flags=re.M,
) )
with open(file, "w") as f: with open(file_path, "w") as f:
f.write(data_new) f.write(data_new)
# Microsoft .NET project files # Microsoft .NET project files
elif myroot.attrib.has_key("Sdk") and "Microsoft.NET.Sdk" in myroot.attrib["Sdk"]: elif myroot.attrib.has_key("Sdk") and "Microsoft.NET.Sdk" in myroot.attrib["Sdk"]:
version_property = [x for x in myroot[0] if x.tag == "Version"][-1] version_property = [x for x in myroot[0] if x.tag == "Version"][-1]
version_property.text = version version_property.text = version
mytree.write(file) mytree.write(file_path)
# MSBuild Props # MSBuild Props
else: else:
myroot[0][1].text = version myroot[0][1].text = version
mytree.write(file, encoding="utf-8") mytree.write(file_path, encoding="utf-8")
# For updating Helm Charts - Chart.yaml version
def update_yaml(version, file_path):
with open(file_path, "r") as f:
doc = yaml.load(f, Loader=yaml.FullLoader)
doc["version"] = version
with open(file_path, "w") as f:
yaml.dump(doc, f)
if __name__ == "__main__": if __name__ == "__main__":
@ -72,6 +89,7 @@ if __name__ == "__main__":
except TypeError: except TypeError:
raise Exception(f"File path for {file_path} not found.") raise Exception(f"File path for {file_path} not found.")
file_name = get_file_name(file_path)
file_type = get_file_type(file_path) file_type = get_file_type(file_path)
# Handle the file based on the extension. # Handle the file based on the extension.
@ -81,6 +99,8 @@ if __name__ == "__main__":
update_json(version, file_path) update_json(version, file_path)
elif file_type == ".plist": elif file_type == ".plist":
update_plist(version, file_path) update_plist(version, file_path)
elif file_name == "Chart.yaml" or file_name == "Chart.yml":
update_yaml(version, file_path)
else: else:
raise Exception("No file was recognized as a supported format.") raise Exception("No file was recognized as a supported format.")

Loading…
Cancel
Save