refactor to reduce code duplication #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Create Installer | ||
on: | ||
workflow_call: | ||
inputs: | ||
package_name: | ||
type: string | ||
required: true | ||
new_version: | ||
type: string | ||
required: true | ||
release_tag: | ||
type: string | ||
required: true | ||
release_id: | ||
type: string | ||
required: true | ||
os: | ||
type: string | ||
required: true | ||
runner: | ||
type: string | ||
required: true | ||
python_version: | ||
type: string | ||
required: true | ||
build_nodejs_ui: | ||
type: boolean | ||
required: true | ||
test_app: | ||
type: boolean | ||
required: true | ||
commitish_to_release: | ||
type: string | ||
required: true | ||
jobs: | ||
create_artifact: | ||
runs-on: ${{ inputs.runner }} | ||
name: Create artifact for ${{ inputs.os }} | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
with: | ||
ref: ${{ inputs.commitish_to_release }} | ||
- name: Get artifact name | ||
id: get_artifact | ||
shell: bash | ||
run: | | ||
ARCH=$(python -c "import platform; arch=platform.machine().lower(); print('x64' if arch == 'x86_64' else arch)") | ||
KERNEL=$(python -c "import platform; print(platform.system().lower())") | ||
BUILD_NAME="${{ inputs.package_name }}-${{ inputs.new_version }}-${KERNEL}-${ARCH}" | ||
OS_STRING="" | ||
if [ "${{ inputs.os }}" == "linux" ]; then | ||
EXTENSION=".deb" | ||
elif [ "${{ inputs.os }}" == "macos" ]; then | ||
OS_STRING="-macos" | ||
EXTENSION=".pkg" | ||
elif [ "${{ inputs.os }}" == "windows" ]; then | ||
EXTENSION=".exe" | ||
fi | ||
BUILD_NAME="${{ inputs.package_name }}-${{ inputs.new_version }}${OS_STRING}-${KERNEL}-${ARCH}" | ||
ARTIFACT_NAME="${BUILD_NAME}${EXTENSION}" | ||
echo "got BUILD_NAME=${BUILD_NAME}" | ||
echo "got ARTIFACT_NAME=${ARTIFACT_NAME}" | ||
echo "BUILD_NAME=${BUILD_NAME}" >> $GITHUB_ENV | ||
echo "ARTIFACT_NAME=${ARTIFACT_NAME}" >> $GITHUB_OUTPUT | ||
- name: Check if artifact exists | ||
shell: bash | ||
env: | ||
GH_TOKEN: ${{ github.token }} | ||
run: | | ||
json_response=$(gh api \ | ||
--method GET \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
repos/$GITHUB_REPOSITORY/releases/${{ inputs.release_id }}/assets) | ||
echo $json_response | ||
if echo "$json_response" | jq -e '.[] | select(.name == "'"${{ steps.get_artifact.outputs.ARTIFACT_NAME }}"'")' > /dev/null; then | ||
echo "Artifact ${{ steps.get_artifact.outputs.ARTIFACT_NAME }} already exists in release ${{ inputs.release_id }}" | ||
echo "This might be okay if you are re-running the workflow" | ||
exit 1 | ||
fi | ||
- name: Install conda | ||
uses: conda-incubator/setup-miniconda@v3 | ||
with: | ||
miniconda-version: "latest" | ||
auto-update-conda: true | ||
activate-environment: alphax_release | ||
python-version: "${{ inputs.python_version }}" | ||
- name: Install dependencies | ||
shell: bash -el {0} | ||
run: | | ||
if [ "${{ inputs.os }}" == "windows" ]; then | ||
# TODO: pefile==2024.8.26 is super-slow on the 'binary vs. data reclassification' step, cf. https://github.com/erocarrera/pefile/issues/420 | ||
pip install pefile==2023.2.7 | ||
fi | ||
pip install pyinstaller==6.10.0 build==1.2.1 | ||
pip freeze | ||
- name: Build installer (Unix) | ||
if: inputs.os != 'windows' | ||
shell: bash -el {0} | ||
run: | | ||
release/${{ inputs.os }}/build_installer_${{ inputs.os }}.sh | ||
pip freeze | ||
ls * | ||
ls dist*/* | ||
- name: Build installer (Windows) | ||
if: inputs.os == 'windows' | ||
shell: pwsh | ||
run: | | ||
release/${{ inputs.os }}/build_installer_${{ inputs.os }}.ps1 | ||
pip freeze | ||
ls * | ||
ls dist*/* | ||
- name: Test app | ||
if: ${{ inputs.test_app }} | ||
shell: bash -el {0} | ||
# TODO this is not unified yet between peptdeep & alphadia | ||
run: | | ||
if [ "${{ inputs.os }}" == "windows" ]; then | ||
EXECUTABLE="dist_pyinstaller/${{ inputs.package_name }}/${{ inputs.package_name }}.exe" | ||
else | ||
EXECUTABLE="dist_pyinstaller/${{ inputs.package_name }}/${{ inputs.package_name }}" | ||
fi | ||
COMMAND="--version" | ||
if [ ! -e ${EXECUTABLE} ]; then | ||
if [ "${{ inputs.os }}" == "windows" ]; then | ||
EXECUTABLE="dist_pyinstaller/${{ inputs.package_name }}_gui/${{ inputs.package_name }}_gui.exe" | ||
else | ||
EXECUTABLE="dist_pyinstaller/${{ inputs.package_name }}_gui/${{ inputs.package_name }}_gui" | ||
fi | ||
COMMAND="--help" | ||
fi | ||
echo "calling ${EXECUTABLE} ${COMMAND}" | ||
eval ${EXECUTABLE} ${COMMAND} | ||
- name: Setup Node.js | ||
if: ${{ inputs.build_nodejs_ui }} | ||
uses: actions/setup-node@v4 | ||
- name: Build Node.js GUI (Unix) | ||
if: inputs.build_nodejs_ui && inputs.os != 'windows' | ||
shell: bash | ||
run: | | ||
release/${{ inputs.os }}/build_gui_${{ inputs.os }}.sh | ||
- name: Build Node.js GUI (Windows) | ||
if: inputs.build_nodejs_ui && inputs.os == 'windows' | ||
shell: pwsh | ||
run: | | ||
release/${{ inputs.os }}/build_gui_${{ inputs.os }}.ps1 | ||
- name: Build package (Unix) | ||
if: inputs.os != 'windows' | ||
shell: bash -el {0} | ||
continue-on-error: true | ||
run: | | ||
release/${{ inputs.os }}/build_package_${{ inputs.os }}.sh | ||
ls * | ||
ls dist*/* | ||
- name: Build package (Windows) | ||
if: inputs.os == 'windows' | ||
shell: pwsh | ||
continue-on-error: true | ||
run: | | ||
release/${{ inputs.os }}/build_package_${{ inputs.os }}.ps1 | ||
ls * | ||
ls dist*/* | ||
- name: Test package | ||
id: test_package | ||
shell: bash -el {0} | ||
run: | | ||
if [ "${{ inputs.os }}" == "linux" ]; then | ||
sudo dpkg -i ./${{ steps.get_artifact.outputs.ARTIFACT_NAME }} | ||
elif [ "${{ inputs.os }}" == "macos" ]; then | ||
sudo installer -pkg ./${{ steps.get_artifact.outputs.ARTIFACT_NAME }} -target / | ||
else | ||
./${{ steps.get_artifact.outputs.ARTIFACT_NAME }} //verysilent //SUPPRESSMSGBOXES //log=log.txt //noicons //tasks= //portable=1 | ||
cat log.txt | ||
fi | ||
- name: Upload installer | ||
env: | ||
GH_TOKEN: ${{ github.token }} | ||
GH_REPO: ${{ github.repository }} | ||
run: gh release upload ${{ inputs.release_tag }} ./${{steps.get_artifact.outputs.ARTIFACT_NAME}} | ||
- name: Fallback upload installer as job asset | ||
if: ${{ failure() && steps.test_package.conclusion == 'failure' }} | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: ${{steps.get_artifact.outputs.ARTIFACT_NAME}} | ||
path: ./${{steps.get_artifact.outputs.ARTIFACT_NAME}} | ||
if-no-files-found: error |