Skip to content

Commit

Permalink
added python build file to get dynamically current pom.xml dependenci…
Browse files Browse the repository at this point in the history
…es, add build.sh deprecated and improve current github workflow (#138)

Co-authored-by: Dorian Grasset <[email protected]>
  • Loading branch information
GuilhemSempere and dorian-grst authored Jul 25, 2024
1 parent 7770283 commit c92c6bd
Show file tree
Hide file tree
Showing 3 changed files with 272 additions and 82 deletions.
221 changes: 139 additions & 82 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,144 @@ on:
workflow_dispatch

jobs:
build:
setup-and-build:
runs-on: ubuntu-latest

outputs:
# Output the release version to be used in the following steps
release_version: ${{ steps.grep_release_version.outputs.release_version }}
steps:
- name: Set up Java
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 17
- name: Run build script
run: |
wget https://raw.githubusercontent.com/${{ github.repository }}/master/misc/build.sh
chmod +x build.sh
./build.sh -P prod
- name: Grep release version
id: grep_release_version
run: |
# RESULT=$(grep -m 1 project\\\.version ${{ github.event.repository.name }}/pom.xml | sed -n 's/.*<project\.version>\(.*\)<\/project\.version>.*/\1/p')
echo "release_version=$(grep -m 1 project\\\.version ${{ github.event.repository.name }}/pom.xml | sed -n 's/.*<project\.version>\(.*\)<\/project\.version>.*/\1/p')" >>$GITHUB_OUTPUT
shell: bash
- name: Create Release
id: create_release
uses: actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: "${{ steps.grep_release_version.outputs.release_version }}"
release_name: "${{ steps.grep_release_version.outputs.release_version }}"
draft: false
prerelease: false
- name: Update Release Description
run: |
release_id=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/releases/tags/${{ steps.grep_release_version.outputs.release_version }}" | jq -r .id)
curl -X PATCH -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/${{ github.repository }}/releases/${release_id}" -d '{"body": "For initial setups not using Docker, download the bundle creation script corresponding to your OS, place it in the desired installation location, and launch it!"}'
- name: Upload Main Release Asset (webapp zip)
id: upload-release-asset
uses: actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
asset_path: ${{ github.event.repository.name }}/target/Gigwa_V${{ steps.grep_release_version.outputs.release_version }}_Webapp.zip
asset_name: Gigwa_V${{ steps.grep_release_version.outputs.release_version }}_Webapp.zip
asset_content_type: application/zip
- name: Upload Additional Release Asset (osx bundle creation script)
id: upload-osx-bundle-asset
uses: actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ${{ github.event.repository.name }}/misc/macos_bundle.command
asset_name: Gigwa_V${{ steps.grep_release_version.outputs.release_version }}_bundle_creation_osx.command
asset_content_type: application/x-sh
- name: Upload Additional Release Asset (linux bundle creation script)
id: upload-linux-bundle-asset
uses: actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ${{ github.event.repository.name }}/misc/linux_bundle.sh
asset_name: Gigwa_V${{ steps.grep_release_version.outputs.release_version }}_bundle_creation_ubuntu.sh
asset_content_type: application/x-sh
- name: Upload Additional Release Asset (windows bundle creation script)
id: upload-win-bundle-asset
uses: actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ${{ github.event.repository.name }}/misc/win_bundle.ps1
asset_name: Gigwa_V${{ steps.grep_release_version.outputs.release_version }}_bundle_creation_windows.ps1
asset_content_type: application/powershell
- name: Upload Additional Release Asset (docker-compose file)
id: upload-docker-compose-asset
uses: actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ${{ github.event.repository.name }}/docker-compose.yml
asset_name: Gigwa_V${{ steps.grep_release_version.outputs.release_version }}_docker-compose.yml
asset_content_type: application/yml
- uses: actions/checkout@v4

# Set up Python for the build script
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

# Install Python dependencies for the build script
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install gitpython
# Set up Java for the maven build
- name: Set up Java
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 17

# Get the release version from the pom.xml file
- name: Grep release version
id: grep_release_version
run: |
echo "release_version=$(grep -m 1 project\\\.version pom.xml | sed -n 's/.*<project\.version>\(.*\)<\/project\.version>.*/\1/p')" >> $GITHUB_OUTPUT
# Run the maven build script which will build the project and create the Webapp zip
- name: Run Python build script
run: |
cd misc
python3 build.py
# Upload the Webapp zip as artifact for upload-release-assets
- name: Upload Webapp zip
uses: actions/upload-artifact@v3
with:
name: Gigwa_V${{ steps.grep_release_version.outputs.release_version }}_Webapp.zip
path: target/Gigwa_V${{ steps.grep_release_version.outputs.release_version }}_Webapp.zip

# Upload the Gigwa directory as artifact for the Dockerfile
- name: Upload Gigwa directory
uses: actions/upload-artifact@v3
with:
name: gigwa
path: target/gigwa/

create-release:
needs: setup-and-build
runs-on: ubuntu-latest
outputs:
# Output the upload URL to be used in the following steps
upload_url: ${{ steps.create_release.outputs.upload_url }}
steps:
# Create a release with the good version and add a description
- name: Create Release
id: create_release
uses: actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: "${{ needs.setup-and-build.outputs.release_version }}"
release_name: "${{ needs.setup-and-build.outputs.release_version }}"
body: |
For initial setups not using Docker, download the bundle creation script corresponding to your OS, place it in the desired installation location, and launch it!
draft: false
prerelease: false

upload-release-assets:
needs: [setup-and-build, create-release]
runs-on: ubuntu-latest
strategy:
matrix:
# Define the assets to be uploaded with their path, name, content type, and add a flag "download" to download the asset before uploading it
asset:
- { path: "target/Gigwa_V${{ needs.setup-and-build.outputs.release_version }}_Webapp.zip", name: "Gigwa_V${{ needs.setup-and-build.outputs.release_version }}_Webapp.zip", content_type: "application/zip", downloaded: true }
- { path: "misc/macos_bundle.command", name: "Gigwa_V${{ needs.setup-and-build.outputs.release_version }}_bundle_creation_osx.command", content_type: "application/x-sh" }
- { path: "misc/linux_bundle.sh", name: "Gigwa_V${{ needs.setup-and-build.outputs.release_version }}_bundle_creation_ubuntu.sh", content_type: "application/x-sh" }
- { path: "misc/win_bundle.ps1", name: "Gigwa_V${{ needs.setup-and-build.outputs.release_version }}_bundle_creation_windows.ps1", content_type: "application/powershell" }
- { path: "docker-compose.yml", name: "Gigwa_V${{ needs.setup-and-build.outputs.release_version }}_docker-compose.yml", content_type: "application/yml" }
steps:
- uses: actions/checkout@v4

# Download the asset if the flag "download" is set to true
- name: Download Webapp zip
if: matrix.asset.downloaded == true
uses: actions/download-artifact@v3
with:
name: Gigwa_V${{ needs.setup-and-build.outputs.release_version }}_Webapp.zip
path: target/

# Upload the asset to the release
- name: Upload Release Asset
uses: actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ needs.create-release.outputs.upload_url }}
asset_path: ${{ matrix.asset.path }}
asset_name: ${{ matrix.asset.name }}
asset_content_type: ${{ matrix.asset.content_type }}

build-push-docker:
needs: [ setup-and-build, create-release ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

# Set up Docker Buildx for multi-platform builds
- name: Set up Docker Buildx
uses: docker/[email protected]

# Login to Docker Hub
- name: Login to Docker Hub
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

# Download the Gigwa directory artifact for the Dockerfile
- name: Download gigwa directory
uses: actions/download-artifact@v3
with:
name: gigwa
path: target/gigwa/

# Build and push the Docker image
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/gigwa:${{ needs.setup-and-build.outputs.release_version }}
${{ secrets.DOCKERHUB_USERNAME }}/gigwa:latest
128 changes: 128 additions & 0 deletions misc/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import os
import git
import xml.etree.ElementTree as ET
import subprocess

# Dossier où les projets seront clonés
script_dir = os.path.dirname(os.path.abspath(__file__))
gigwa2_dir = os.path.dirname(script_dir)
parent_dir = os.path.dirname(gigwa2_dir)

# Fonction pour lire les modules depuis le bom/pom.xml
def get_modules_from_bom():
bom_pom_path = os.path.join(gigwa2_dir, 'bom', 'pom.xml')
tree = ET.parse(bom_pom_path)
root = tree.getroot()
namespaces = {'m': 'http://maven.apache.org/POM/4.0.0'}

modules = []
for module in root.findall(".//m:module", namespaces):
module_path = module.text
if module_path != '..': # Ignore the parent module
module_name = os.path.basename(module_path)
modules.append(module_name)

return modules

# Fonction pour cloner un dépôt avec la branche master
def clone_repo(repo_url, clone_path):
full_clone_path = os.path.join(parent_dir, clone_path)
if not os.path.exists(full_clone_path):
print(f"Cloning {repo_url} into {full_clone_path} (branch: master)")
git.Repo.clone_from(repo_url, full_clone_path, branch='master')
else:
print(f"Repository {repo_url} already exists at {full_clone_path}")

# Fonction pour analyser le pom.xml et extraire les dépendances fr.cirad
def extract_dependencies(pom_path):
tree = ET.parse(pom_path)
root = tree.getroot()
namespaces = {'m': 'http://maven.apache.org/POM/4.0.0'}

dependencies = []
for dependency in root.findall(".//m:dependency", namespaces):
group_id = dependency.find("m:groupId", namespaces).text
if group_id == "fr.cirad":
artifact_id = dependency.find("m:artifactId", namespaces).text
version = dependency.find("m:version", namespaces).text
dependencies.append((artifact_id, version))
return dependencies

# Fonction pour faire un checkout sur une version spécifique
def checkout_version(repo_path, version):
full_repo_path = os.path.join(parent_dir, repo_path)
repo = git.Repo(full_repo_path)
try:
repo.git.checkout(version)
print(f"Checked out {full_repo_path} to version {version}")
except git.GitCommandError as e:
print(f"Error: Unable to checkout version {version} in {full_repo_path}: {e}")

# Stocker les résultats
results = {}
seen_artifacts = set()

# Vérifier et analyser le pom.xml du projet actuel
current_pom_path = os.path.join(gigwa2_dir, "pom.xml")
if os.path.exists(current_pom_path):
current_dependencies = extract_dependencies(current_pom_path)
if current_dependencies:
results['CurrentProject'] = []
for artifact_id, version in current_dependencies:
if artifact_id not in seen_artifacts:
results['CurrentProject'].append((artifact_id, version))
seen_artifacts.add(artifact_id)
else:
print("Error: pom.xml not found in the Gigwa2 directory")

# Obtenir la liste des modules depuis bom/pom.xml
app_names = get_modules_from_bom()

# Cloner les projets et analyser les pom.xml
for app_name in app_names:
repo_url = f"https://github.com/GuilhemSempere/{app_name}.git"
clone_path = os.path.join(parent_dir, app_name)

clone_repo(repo_url, clone_path)

pom_path = os.path.join(clone_path, "pom.xml")
if os.path.exists(pom_path):
dependencies = extract_dependencies(pom_path)
if dependencies:
for artifact_id, version in dependencies:
if artifact_id not in seen_artifacts:
if app_name not in results:
results[app_name] = []
results[app_name].append((artifact_id, version))
seen_artifacts.add(artifact_id)

# Affichage des résultats et checkout des versions dans cloned_projects
print("Dependencies:")

for app_name, deps in results.items():
print(f"----- Project: {app_name} -----")
for artifact_id, version in deps:
print(f" ArtifactId: {artifact_id}, Version: {version}")
artifact_clone_path = os.path.join(parent_dir, artifact_id)
# Vérifier si le projet de l'artefact est déjà cloné
if os.path.exists(artifact_clone_path):
# Faire un checkout sur la version
checkout_version(artifact_clone_path, version)
else:
print(f"Error: Project for artifactId {artifact_id} not found in {parent_dir}")
print(f"-------------------------------")

# Exécuter mvn install sur le pom.xml dans le dossier bom
print("Executing 'mvn install' on the bom/pom.xml...")
try:
bom_dir = os.path.join(gigwa2_dir, 'bom')
if os.path.exists(bom_dir):
os.chdir(bom_dir)
subprocess.run(['mvn', 'install', '-P', 'prod', '-f', 'pom.xml'], check=True)
print("mvn install completed successfully on bom/pom.xml.")
else:
print(f"Error: bom directory not found in {gigwa2_dir}")
except subprocess.CalledProcessError as e:
print(f"Error while running 'mvn install' on bom/pom.xml: {e}")
finally:
os.chdir(script_dir) # Retour au répertoire du script
5 changes: 5 additions & 0 deletions misc/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#
# WARNING - Don't launch this script from its original location (within the Gigwa2 source hierarchy) as it would create duplicate source files in your IDE workspace

##############################################################################################################################
## DEPRECATED - This script is no longer used and has been replaced by the python script build.py ##
## It takes the latest versions of subprojects without checking whether they are the correct version for the latest release ##
##############################################################################################################################

set -e

m2repo=$(mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout)
Expand Down

0 comments on commit c92c6bd

Please sign in to comment.