Skip to content

Commit

Permalink
Improve release workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimfacion committed Feb 12, 2025
1 parent 9e05ec2 commit 2dccf4f
Show file tree
Hide file tree
Showing 3 changed files with 291 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: "Release"

on:
workflow_call:
inputs:
branch_platform:
type: string
default: 'main'
required: false
previous_version:
type: string
default: ''
required: false
new_version:
type: string
default: ''
required: false

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: checkout repo content
uses: actions/checkout@v4
- name: setup python
uses: actions/setup-python@v5
with:
python-version: 3.8 #install the python needed
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f scripts/requirements.txt ]; then pip install -r scripts/requirements.txt; fi
- name: execute py script # run file
run: |
python scripts/release.py ${{ github.event.inputs.branch_platform }} ${{ github.event.inputs.previous_version }} ${{ github.event.inputs.new_version }}
252 changes: 252 additions & 0 deletions scripts/release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
import argparse
import logging
import os
import re
import time
from datetime import date

import requests
from OBAS_utils.release_utils import closeRelease, download_url

logging.basicConfig(encoding="utf-8", level=logging.INFO)

parser = argparse.ArgumentParser("release")
parser.add_argument(
"branch_platform", help="The new version number of the release.", type=str
)
parser.add_argument(
"previous_version", help="The previous version number of the release.", type=str
)
parser.add_argument(
"new_version", help="The new version number of the release.", type=str
)
parser.add_argument(
"--dev", help="Flag to prevent pushing the release.", action="store_false"
)
args = parser.parse_args()

previous_version = args.previous_version
new_version = args.new_version
branch_platform = args.branch_platform

github_token = os.environ["GREN_GITHUB_TOKEN"]

os.environ["DRONE_COMMIT_AUTHOR"] = "Filigran-Automation"
os.environ["GIT_AUTHOR_NAME"] = "Filigran Automation"
os.environ["GIT_AUTHOR_EMAIL"] = "[email protected]"
os.environ["GIT_COMMITTER_NAME"] = "Filigran Automation"
os.environ["GIT_COMMITTER_EMAIL"] = "[email protected]"

# Platform
logging.info("[platform] Starting the release")

# Pom
with open(
"pom.xml",
"r",
) as file:
filedata = file.read()
filedata = re.sub(
r"<groupId>io.openbas</groupId>(\s+)<artifactId>(.*)</artifactId>(\s+)<version>"
+ previous_version
+ "</version>",
r"<groupId>io.openbas</groupId>\1<artifactId>\2</artifactId>\3<version>"
+ new_version
+ "</version>",
filedata,
)
with open(
"pom.xml",
"w",
) as file:
file.write(filedata)
with open(
"./openbas-api/pom.xml",
"r",
) as file:
filedata = file.read()
filedata = re.sub(
r"<groupId>io.openbas</groupId>(\s+)<artifactId>(.*)</artifactId>(\s+)<version>"
+ previous_version
+ "</version>",
r"<groupId>io.openbas</groupId>\1<artifactId>\2</artifactId>\3<version>"
+ new_version
+ "</version>",
filedata,
)
with open(
"./openbas-api/pom.xml",
"w",
) as file:
file.write(filedata)
with open(
"./openbas-framework/pom.xml",
"r",
) as file:
filedata = file.read()
filedata = re.sub(
r"<groupId>io.openbas</groupId>(\s+)<artifactId>(.*)</artifactId>(\s+)<version>"
+ previous_version
+ "</version>",
r"<groupId>io.openbas</groupId>\1<artifactId>\2</artifactId>\3<version>"
+ new_version
+ "</version>",
filedata,
)
with open(
"./openbas-framework/pom.xml",
"w",
) as file:
file.write(filedata)
with open(
"./openbas-model/pom.xml",
"r",
) as file:
filedata = file.read()
filedata = re.sub(
r"<groupId>io.openbas</groupId>(\s+)<artifactId>(.*)</artifactId>(\s+)<version>"
+ previous_version
+ "</version>",
r"<groupId>io.openbas</groupId>\1<artifactId>\2</artifactId>\3<version>"
+ new_version
+ "</version>",
filedata,
)
with open(
"./openbas-model/pom.xml",
"w",
) as file:
file.write(filedata)

# Package.json
with open("./openbas-front/package.json", "r") as file:
filedata = file.read()
filedata = filedata.replace(
'"version": "' + previous_version + '"', '"version": "' + new_version + '"'
)
with open("./openbas-front/package.json", "w") as file:
file.write(filedata)

logging.info("[platform] Pushing to " + branch_platform)
os.system(
'git commit -a -m "[all] Release '
+ new_version
+ '" > /dev/null 2>&1 && git push origin '
+ branch_platform
+ " > /dev/null 2>&1"
)

logging.info("[platform] Tagging")
os.system("git tag " + new_version + " && git push --tags > /dev/null 2>&1")

logging.info(
"[platform] Tag pushed! Waiting 30 minutes for CI/CD build before final release...."
)
time.sleep(1800)

logging.info("[platform] Generating release")
os.system("gren release > /dev/null 2>&1")

# Modify the release note
logging.info("[platform] Getting the current release note")
release = requests.get(
"https://api.github.com/repos/OpenBAS-Platform/openbas/releases/latest",
headers={
"Accept": "application/vnd.github+json",
"Authorization": "Bearer " + github_token,
"X-GitHub-Api-Version": "2022-11-28",
},
)
release_data = release.json()
release_body = release_data["body"]

logging.info("[platform] Generating the new release note")
github_release_note = requests.post(
"https://api.github.com/repos/OpenBAS-Platform/openbas/releases/generate-notes",
headers={
"Accept": "application/vnd.github+json",
"Authorization": "Bearer " + github_token,
"X-GitHub-Api-Version": "2022-11-28",
},
json={"tag_name": new_version, "previous_tag_name": previous_version},
)
github_release_note_data = github_release_note.json()
github_release_note_data_body = github_release_note_data["body"]
if "Full Changelog" not in release_body:
new_release_note = (
release_body
+ "\n"
+ github_release_note_data_body.replace(
"## What's Changed", "#### Pull Requests:\n"
).replace("## New Contributors", "#### New Contributors:\n")
)
else:
new_release_note = release_body

logging.info("[platform] Updating the release")
requests.patch(
"https://api.github.com/repos/OpenBAS-Platform/openbas/releases/"
+ str(release_data["id"]),
headers={
"Accept": "application/vnd.github+json",
"Authorization": "Bearer " + github_token,
"X-GitHub-Api-Version": "2022-11-28",
},
json={"body": new_release_note},
)

logging.info("[platform] Uploading release artifacts...")

logging.info("[platform] Downloading latest builds")
today = date.today()
timestamp = str(today.year) + "{:02d}".format(today.month) + "{:02d}".format(today.day)
download_url(
"https://filigran.jfrog.io/artifactory/openbas/openbas-" + timestamp + ".tar.gz",
save_path="openbas-release-" + new_version + ".tar.gz",
)
download_url(
"https://filigran.jfrog.io/artifactory/openbas/openbas-"
+ timestamp
+ "_musl.tar.gz",
save_path="openbas-release-" + new_version + "_musl.tar.gz",
)

logging.info("[platform] Updating the release")
with open("openbas-release-" + new_version + ".tar.gz", "rb") as f:
data = f.read()
requests.post(
"https://uploads.github.com/repos/OpenBAS-Platform/openbas/releases/"
+ str(release_data["id"])
+ "/assets?name=openbas-release-"
+ new_version
+ ".tar.gz",
headers={
"Accept": "application/vnd.github+json",
"Authorization": "Bearer " + github_token,
"X-GitHub-Api-Version": "2022-11-28",
"Content-Type": "application/octet-stream",
},
data=data,
)
with open("openbas-release-" + new_version + "_musl.tar.gz", "rb") as f:
data = f.read()
requests.post(
"https://uploads.github.com/repos/OpenBAS-Platform/openbas/releases/"
+ str(release_data["id"])
+ "/assets?name=openbas-release-"
+ new_version
+ "_musl.tar.gz",
headers={
"Accept": "application/vnd.github+json",
"Authorization": "Bearer " + github_token,
"X-GitHub-Api-Version": "2022-11-28",
"Content-Type": "application/octet-stream",
},
data=data,
)

closeRelease(
"https://api.github.com/repos/OpenBAS-Platform/openbas", new_version, github_token
)

logging.info("[platform] Release done!")
3 changes: 3 additions & 0 deletions scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PyYAML
requests
git+https://github.com/OpenBAS-Platform/release.git@improvement/new_release_workflow

0 comments on commit 2dccf4f

Please sign in to comment.