From 545aa842284812b8a210ce28b720c1ca5dd0fabc Mon Sep 17 00:00:00 2001
From: shadow <81448108+shdwmtr@users.noreply.github.com>
Date: Wed, 16 Oct 2024 23:33:51 -0300
Subject: [PATCH] chore: Make artifact CI (#140)

---
 .github/workflows/artifact.yml | 332 +++++++++++++++++++++++++++++++++
 1 file changed, 332 insertions(+)
 create mode 100644 .github/workflows/artifact.yml

diff --git a/.github/workflows/artifact.yml b/.github/workflows/artifact.yml
new file mode 100644
index 00000000..7cf7c2c2
--- /dev/null
+++ b/.github/workflows/artifact.yml
@@ -0,0 +1,332 @@
+name: Build Millennium
+
+on:
+  workflow_dispatch:
+
+jobs:
+  build-windows:
+    permissions:
+      contents: write
+      issues: write
+      pull-requests: write
+
+    runs-on: windows-latest
+
+    env:
+      ACTIONS_ALLOW_UNSECURE_COMMANDS: true
+      VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
+
+    steps:
+    - name: Export GitHub Actions cache environment variables
+      uses: actions/github-script@v7
+      with:
+        script: |
+          core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
+          core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');
+
+    - name: Checkout repository
+      uses: actions/checkout@v4
+      with:
+        submodules: true
+
+    - name: (Setup) Node.js
+      uses: actions/setup-node@v3
+      with:
+        node-version: 20
+
+    - name: (Setup) Assets Build Environment
+      run: cd assets && npm install && npm run build
+      env:
+        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+
+    - name: (Setup) Build Assets
+      run: . scripts\ci\win32\mk-assets.ps1
+    
+    - name: (Setup) Construct Python Environment
+      run: . scripts\ci\win32\setup-python.ps1 D:/a/env/ext/data/cache
+
+    - name: (Setup) Semantic Release
+      run: npm install --save-dev semantic-release @semantic-release/github @semantic-release/exec @semantic-release/changelog @semantic-release/git
+      env: 
+        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+
+    # dry run to get the next version
+    - name: Bump Version
+      id: read_version
+      run: . scripts\ci\win32\version.ps1
+      env:
+        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+
+    - name: Set up cache for Python source
+      uses: actions/cache@v3
+      id: build-cache
+      with:
+        path: Python-3.11.8/PCbuild/win32
+        key: ${{ runner.os }}-python-3.11.8-build
+        restore-keys: ${{ runner.os }}-python-3.11.8-build-  
+
+    - name: (Python) Add msbuild to PATH
+      if: steps.build-cache.outputs.cache-hit != 'true'
+      uses: microsoft/setup-msbuild@v2
+
+    - name: (Python) Download 3.11.8 win32 source
+      if: steps.build-cache.outputs.cache-hit != 'true'
+      run: . scripts\ci\win32\build-python.ps1
+
+    - name: Upload Build Artifact
+      uses: actions/upload-artifact@v4
+      with:
+        name: python 3.11.8 build libraries
+        path: D:/a/Millennium/Millennium/build/python
+
+    - name: Install prerequisites
+      uses: msys2/setup-msys2@v2
+      with:
+        msystem: mingw32
+        install: >-
+          mingw-w64-i686-libgcrypt
+          mingw-w64-i686-gcc
+          mingw-w64-i686-cmake
+          mingw-w64-i686-ninja
+
+    - name: (Dependency) Install vcpkg
+      shell: pwsh
+      run: ./vendor/vcpkg/bootstrap-vcpkg.bat && ./vendor/vcpkg/vcpkg integrate install
+
+    - name: (Generator) Configure CMake
+      shell: msys2 {0}
+      run: cmake --preset=windows-mingw-release -DGITHUB_ACTION_BUILD=ON
+
+    - name: Build Millennium
+      shell: msys2 {0}
+      run: |
+        mkdir D:/a/Millennium/Millennium/build/artifacts
+        cmake --build build --config Release
+        cp D:/a/Millennium/Millennium/Python-3.11.8/PCbuild/win32/python311.dll D:/a/env/python311.dll
+        cp /d/a/Millennium/Millennium/build/user32.dll  D:/a/env/user32.dll
+        mkdir D:/a/env/ext/bin
+        # Disable Millennium CLI for now, as it keeps get false positive detections for no apparent reason
+        # cp /d/a/Millennium/Millennium/build/cli/millennium.exe D:/a/env/ext/bin/millennium.exe
+
+    - name: Upload Build Artifact
+      uses: actions/upload-artifact@v4
+      with:
+        include-hidden-files: true
+        name: millennium-windows
+        path: D:/a/env/
+
+  build-linux:
+    permissions:
+      contents: write
+      issues: write
+      pull-requests: write
+
+    runs-on: ubuntu-latest
+
+    env:
+      VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
+
+    steps:
+    - name: Export GitHub Actions cache environment variables
+      uses: actions/github-script@v7
+      with:
+        script: |
+          core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
+          core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');
+
+    - name: Checkout repository
+      uses: actions/checkout@v4
+      with:
+        submodules: true
+
+    - name: (Setup) Node.js
+      uses: actions/setup-node@v3
+      with:
+        node-version: 20
+
+    - name: (Setup) Assets Build Environment
+      run: cd assets && npm install && npm run build
+      env:
+        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+
+    - name: (Setup) Build Assets
+      run: bash scripts/ci/posix/mk-assets.sh
+    
+    - name: (Setup) Semantic Release
+      run: npm install --save-dev semantic-release @semantic-release/github @semantic-release/exec @semantic-release/changelog @semantic-release/git
+      env: 
+        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+
+    # dry run to get the next version
+    - name: Bump Version
+      id: read_version
+      run: bash scripts/ci/posix/version.sh
+      env:
+        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+
+    - name: Install dependencies
+      run: |
+        sudo dpkg --add-architecture i386
+        sudo apt-get update
+        sudo apt-get install -y \
+          build-essential \
+          zlib1g-dev:i386 \
+          libncurses5-dev:i386 \
+          libgdbm-dev:i386 \
+          libnss3-dev:i386 \
+          libssl-dev:i386 \
+          libreadline-dev:i386 \
+          libffi-dev:i386 \
+          wget \
+          curl \
+          libbz2-dev:i386 \
+          libsqlite3-dev:i386 \
+          liblzma-dev:i386 \
+          gcc-multilib \
+          g++-multilib
+
+    - name: Set up cache for Python source
+      uses: actions/cache@v3
+      id: build-cache
+      with:
+        path: home/runner/.millennium/ext/data/cache
+        key: ${{ runner.os }}-python-linux-3.11.8-build
+        restore-keys: ${{ runner.os }}-python-linux-3.11.8-build-  
+
+    - name: Download Python 3.11.8 source
+      if: steps.build-cache.outputs.cache-hit != 'true'
+      run: |
+        wget https://www.python.org/ftp/python/3.11.8/Python-3.11.8.tgz
+        tar -xf Python-3.11.8.tgz
+
+    - name: Configure and build Python 3.11.8 (32-bit)
+      if: steps.build-cache.outputs.cache-hit != 'true'
+      run: |
+        mkdir -p $HOME/.millennium/ext/data/cache
+        cd Python-3.11.8
+        sudo CFLAGS="-m32" LDFLAGS="-m32" ./configure --prefix=$HOME/.millennium/ext/data/cache --enable-optimizations
+        sudo make -j$(nproc)
+        sudo make altinstall
+
+    - name: Setup installation
+      if: steps.build-cache.outputs.cache-hit != 'true'
+      run: |
+        sudo mkdir -p $HOME/.millennium/ext/data/cache/lib/tmp
+        cd $HOME/.millennium/ext/data/cache/lib/tmp
+        sudo ar -x ../libpython3.11.a
+        sudo gcc -m32 -shared -o ../libpython-3.11.8.so *.o
+
+        cd $HOME/.millennium/ext/data/cache/lib
+        sudo rm -rf tmp
+        ls
+
+        sudo mkdir -p $HOME/Documents/LibPython/
+        cd $HOME/.millennium/ext/data/cache/include/python3.11/
+        sudo mv * $HOME/Documents/LibPython/
+
+        sudo rm -rf $HOME/.millennium/ext/data/cache/lib/python3.11/test/
+        sudo rm -rf $HOME/.millennium/ext/data/cache/share
+        sudo rm -rf $HOME/.millennium/ext/data/cache/include
+        sudo rm -rf $HOME/.millennium/ext/data/cache/lib/python3.11/__pycache__/
+        sudo rm -rf $HOME/.millennium/ext/data/cache/lib/python3.11/config-3.11-x86_64-linux-gnu/
+        sudo rm $HOME/.millennium/ext/data/cache/lib/libpython3.11.a
+
+        sudo mv $HOME/.millennium/ext/data/cache/lib/libpython-3.11.8.so $HOME/.millennium/libpython-3.11.8.so
+
+        $HOME/.millennium/ext/data/cache/bin/python3.11 --version
+        mkdir -p /home/runner/env/ext/data
+        sudo cp -r $HOME/.millennium/ext/data/cache /home/runner/env/ext/data
+
+    - name: (Generator) Install CMake
+      uses: jwlawson/actions-setup-cmake@v2
+      with:
+        cmake-version: '3.21.1'
+      env: 
+        VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
+
+    - name: (Dependency) Install vcpkg & setup environment
+      run: sudo apt install libgtk-3-dev ninja-build &&  ./vendor/vcpkg/bootstrap-vcpkg.sh && ./vendor/vcpkg/vcpkg integrate install
+      env: 
+        VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
+
+    - name: (Generator) Configure CMake
+      run: cmake --preset=linux -G "Ninja" -DGITHUB_ACTION_BUILD=ON
+      env: 
+        VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
+
+
+    - name: Build Millennium
+      run: |
+        # mkdir -p /home/runner/Millennium/build/artifacts
+        cmake --build build --config Release
+
+        # ls -laR /home/runner/Millennium/build
+
+        mkdir -p /home/runner/env
+        cp ./build/libMillennium.so /home/runner/env/libMillennium.so
+        cp ~/.millennium/libpython-3.11.8.so /home/runner/env/libpython-3.11.8.so
+        cp ./scripts/posix/start.sh /home/runner/env/start.sh
+        
+        mkdir -p /home/runner/env/ext/bin
+        cp ./build/cli/millennium /home/runner/env/ext/bin/millennium
+
+    - name: Upload Build Artifact
+      uses: actions/upload-artifact@v4
+      with:
+        include-hidden-files: true
+        name: millennium-linux
+        path: /home/runner/env/
+
+  release:
+    needs: [build-windows, build-linux]
+    runs-on: ubuntu-latest
+    steps:
+    - name: Checkout repository
+      uses: actions/checkout@v4
+      with:
+        submodules: true
+
+    - name: Download Windows Artifact
+      uses: actions/download-artifact@v4
+      with:
+        name: millennium-windows
+        path: ./artifacts/windows
+
+    - name: Download Linux Artifact
+      uses: actions/download-artifact@v4
+      with:
+        name: millennium-linux
+        path: ./artifacts/linux
+
+    - name: Get Version
+      id: read_version
+      run: bash scripts/ci/posix/version.sh
+      env:
+        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+
+    - name: Proccess Artifacts
+      run: |
+        mkdir -p ./artifacts/release
+
+        cd ./artifacts/windows
+        zip -r ../release/millennium-v${{ steps.read_version.outputs.version }}-windows-x86_64.zip .
+        cd - # return to the original directory
+
+        cd ./artifacts/linux
+        tar -czvf ../release/millennium-v${{ steps.read_version.outputs.version }}-linux-x86_64.tar.gz .
+        cd - # return to the original directory
+
+    - name: Upload Windows Artifact
+      uses: actions/upload-artifact@v4
+      with:
+        include-hidden-files: true
+        name: millennium-windows
+        path: ./artifacts/release/millennium-v${{ steps.read_version.outputs.version }}-windows-x86_64.zip
+
+    - name: Upload Linux Artifact
+      uses: actions/upload-artifact@v4
+      with:
+        include-hidden-files: true
+        name: millennium-linux
+        path: ./artifacts/release/millennium-v${{ steps.read_version.outputs.version }}-linux-x86_64.tar.gz
+