Search code examples
c++visual-studioqtmsbuildgithub-actions

How to build my Qt/MSbuild project in Github actions


My project is developed in Qt C++ in Windows environment. I want to build on Github actions by using msbuild and windeployqt.

This is my example yml script

name: MSBuild

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

env:
  # Path to the solution file relative to the root of the project.
  SOLUTION_FILE_PATH: ..
  QT_VERSION:   6.4.3

  # Configuration type to build.
  # You can convert this to a build matrix if you need coverage of multiple configuration types.
  # https://docs.github.com/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
  BUILD_CONFIGURATION: Release

permissions:
  contents: read

jobs:
  build:
   runs-on: windows-latest

steps:
- name: (1) Checkout repo
  uses: actions/checkout@v3

- name: (2) Install Qt
  uses: jurplel/install-qt-action@v3
  with:
    version:      ${{ env.QT_VERSION }}
    host:         windows
    target:       desktop
    arch:         win64_msvc2019_64
    dir:          ${{ runner.temp }}
    setup-python: false 
    
- name: (4) Add MSBuild to PATH
  uses: microsoft/[email protected]

- name: Create build folder
  run: mkdir -p ${{ runner.temp }}\build

- name: (5) Build with msbuild
  working-directory: ${{ runner.temp }}\build
  run: echo "Start building in ${{ runner.temp }}\build"
       cd ${{ runner.temp }}\build
       msbuild /m /p:Configuration=${{ env.BUILD_CONFIGURATION }} ${{ github.workspace }}\xxx.sln

- name: (6) List contents of the build directory
  run: |
    ls -R ${{ runner.temp }}\build
    
#- name: (6) Deploy with windeployqt
#  working-directory: ${{ runner.temp }}
#  run: |
#    mkdir ${{ env.DEPLOY_DIR }}
#    windeployqt --dir ${{ env.DEPLOY_DIR }} ${{ env.SOLUTION_FILE_PATH }}/path/to/my/exe

But after step 5 and 6, nothing happened. There's even no any error message that I can check if the source code is built correctly.

Can someone teach me how to build it with MSbuild and windeployqt?

Thanks!

Update: after syntax errors fixed:

- name: (2) Install Qt
  uses: jurplel/install-qt-action@v3
  with:
    version:      ${{ env.QT_VERSION }}
    host:         windows
    target:       desktop
    arch:         win64_msvc2019_64
    dir:          ${{ runner.temp }}
    setup-python: false 
    
- name: Print Qt version information
  run: |
    qmake -v
    
- name: (3) Add MSBuild to PATH
  uses: microsoft/[email protected]

- name: (4) Create build folder
  run: |
       mkdir -p ${{ runner.temp }}\build

- name: (5) Set Qt Environment Variables
  run: |
    echo "Setting Qt Environment Variables"
       echo "QTDIR=${{ runner.temp }}/Qt/${{ env.QT_VERSION }}/msvc2019_64" >> $GITHUB_ENV
       echo "QtToolsPath=${{ runner.temp }}/Qt/${{ env.QT_VERSION }}/msvc2019_64/bin" >> $GITHUB_ENV
  shell: bash

- name: (6) Build with msbuild
  working-directory: ${{ github.workspace }}
  run: |
       echo "Current directory: $($PWD.Path)"
       msbuild /p:Configuration=${{ env.BUILD_CONFIGURATION }} /p:OutDir=${{ runner.temp }}\build\ "${{ env.SOLUTION_FILE_PATH }}\QtStockV3.sln"

enter image description here

UPDATE 2: Q: How to include path in github action?

In the section below "Set Qt Environment Variables and path", I set several include path which contain my header files.

But according to the error msg, it didn't find the header files there?

I'll provide a complete example here, after I resolve all of this. And thanks for your help!

enter image description here

- name: (5) Install Vcpkg and cURL 
  working-directory: ${{ runner.temp }}
  run: |
    git clone https://github.com/microsoft/vcpkg.git
    cd vcpkg
    .\bootstrap-vcpkg.bat
    .\vcpkg install curl
    curl --version

- name: Set Qt Environment Variables and path
  run: |
       echo "QTDIR=${{ runner.temp }}/Qt/${{ env.QT_VERSION }}/msvc2019_64" >> $GITHUB_ENV
       echo "QtToolsPath=${{ runner.temp }}/Qt/${{ env.QT_VERSION }}/msvc2019_64/bin" >> $GITHUB_ENV
       echo "INCLUDE_PATH_1=${{ github.workspace }}/${{ env.PROJ_NAME }}/src/header" >> $GITHUB_PATH
       echo "INCLUDE_PATH_2=${{ github.workspace }}/${{ env.PROJ_NAME }}/src/parser" >> $GITHUB_PATH
       echo "INCLUDE_PATH_3=${{ github.workspace }}/${{ env.PROJ_NAME }}/src/cc" >> $GITHUB_PATH
       echo "INCLUDE_PATH_VCPKG=${{ runner.temp }}/vcpkg/installed/x64-windows/include" >> $GITHUB_PATH
       ls ${INCLUDE_PATH_1}
       ls ${INCLUDE_PATH_2}
       ls ${INCLUDE_PATH_3}
  shell: bash

    
- name: (7) Build with msbuild
  working-directory: ${{ github.workspace }}
  run: |
       echo "Current directory: $($PWD.Path)"
       msbuild /p:Configuration=${{ env.BUILD_CONFIGURATION }} /p:OutDir=${{ runner.temp }}\build\ "${{ env.SOLUTION_FILE_PATH }}\QtStockV3.sln"

enter image description here

Update 3 The way I use $GITHUB_PATH and $GITHUB_ENV seems not work. I installed json in the same way I installed in local.

The pkg was installed properly but still see the error msg in log.

- name: Install nlohmann JSON
  working-directory: ${{ runner.temp }}
  run: |
        git clone https://github.com//json.git
        cd json
        echo "${{ runner.temp }}\json\include" >> $GITHUB_ENV
        ls "${{ runner.temp }}\json\include"

enter image description here

enter image description here

Update 4: I installed json with vcpkg, then the errors left are related to header files path.

To simplified the including path, I move all of my headers to the same folder with .cc

update: I noticed that the path to my header file was not included in the list. But how to do this? To modify the vcxproj file locally? enter image description here

enter image description here


Solution

  • IIRC, you need to set QTDIR and QtToolsPath env vars before your build step. You may use QT_ROOT_DIR to set these in a separate step (see example below with Bash):

    - name: Set QTDIR and QtToolsPath
      shell: bash
      run: |
        echo "QTDIR=$QT_ROOT_DIR" >> "$GITHUB_ENV"
        echo "QtToolsPath=$QT_ROOT_DIR/bin" >> "$GITHUB_ENV"
    
    - name: Build
      run: ...
    

    where, QT_ROOT_DIR represents the Qt installation directory.

    See jurplel/install-qt-action's dir input parameter for more details on QT_ROOT_DIR.


    Depending on your application, you may need to configure modules input parameter to install Qt modules which are not installed by default.

    Apart from that, you might want to enable caching to speed up your workflow runs i.e. cache: true. See cache input parameter for more details.