Github Action的使用

Qt、CMake Project

Posted by Kingtous on December 20, 2020
本页面总访问量

此处记录使用Github Action编译Qt C++、CMake项目。

基本用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
name: 任务名称
on : [触发event1、触发event2...] # event下还有很多配置参数如`paths-ignore`可以忽略相关文件如README
jobs: #job名称
	build:
		name: 任务名称
		runs-on: 系统名称
		env:
			xxx: xxx # 自行配置的环境变量
    steps:
    	- name: 第一步
    		id: 唯一id,可以通过id来得到某一步的输出以及属性
    		uses: 使用第三方的xxx插件
    		with:
    			...第三方插件的传入值
    	...

CMake

安装LLVM 9打包llvm的一个简单项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
name: CMake
# 在什么时候进行
on: [push]

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  BUILD_TYPE: Release

jobs:
  build:
    # The CMake configure and build commands are platform agnostic and should work equally
    # well on Windows or Mac.  You can convert this to a matrix build if you need
    # cross-platform coverage.
    # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
    runs-on: ubuntu-18.04

    steps:
    - uses: actions/checkout@v2
    
    - name: Installing LLVM 9
      run: sudo apt install -y llvm-9-dev
   
    - name: Installing libedit
      run: sudo apt install -y libedit-dev

    - name: Create Build Environment
      # Some projects don't allow in-source building, so create a separate build directory
      # We'll use this as our working directory for all subsequent commands
      run: cmake -E make_directory $/build

    - name: Configure CMake
      # Use a bash shell so we can use the same syntax for environment variable
      # access regardless of the host operating system
      shell: bash
      working-directory: $/build
      # Note the current convention is to use the -S and -B options here to specify source 
      # and build directories, but this is only available with CMake 3.13 and higher.  
      # The CMake binaries on the Github Actions machines are (as of this writing) 3.12
      run: cmake $GITHUB_WORKSPACE

    - name: Build
      working-directory: $/build
      shell: bash
      # Execute the build.  You can specify a specific target with "--target <NAME>"
      run: cmake --build .
    
    - name: Create Release
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: $
      with:
        tag_name: $
        release_name: Release $
        draft: false
        prerelease: false
    - name: Upload Release Asset
      id: upload-release-asset 
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: $
      with:
        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: $/build/llvm_kaleidoscope
        asset_name: llvm_kaleidoscope
        asset_content_type: application/x-executable

Qt

MacOS下使用macdeployqt打包,并且发布release的一个实例实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
name: MacOS
on: 
  push:
    paths-ignore:
      - 'README.md'
      - 'LICENSE'
  pull_request:
    paths-ignore:
      - 'README.md'
      - 'LICENSE'
jobs:
  build:
    name: Build
    runs-on: $
    strategy:
      matrix:
        os: [macos-latest]
        qt_ver: [5.15.2]
        qt_arch: [clang_64]
    env:
      targetName: NoteEasy
    steps:
      - name: cacheQt
        id: MacosCacheQt
        uses: actions/cache@v1 #一个缓存action,可以减少第二+次的重复下载
        with:
          path: ../Qt/$/$
          key: $-Qt/$/$  
      - name: Install Qt
        # if: steps.MacosCacheQt.outputs.cache-hit != 'true'
        uses: jurplel/install-qt-action@v2
        with:
          version: $
          cached: $
      - uses: actions/checkout@v1
        with:
          fetch-depth: 1
      - name: build macos
        run: |
          qmake
          make
      # tag 打包
      - name: package
        if: startsWith(github.event.ref, 'refs/tags/')
        run: |
          # 拷贝依赖
          macdeployqt bin/${targetName}.app -qmldir=. -verbose=1 -dmg
      # tag 查询github-Release
      - name: queryRelease
        id: queryReleaseMacos
        if: startsWith(github.event.ref, 'refs/tags/')
        shell: pwsh
        env:
          githubFullName: $
          ref: $
        run: |
          [string]$tag = ${env:ref}.Substring(${env:ref}.LastIndexOf('/') + 1)
          [string]$url = 'https://api.github.com/repos/' + ${env:githubFullName} + '/releases/tags/' + ${tag}
          $response={}
          try {
            $response = Invoke-RestMethod -Uri $url -Method Get
          } catch {
            Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ 
            Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
            # 没查到,输出
            echo "::set-output name=needCreateRelease::true"  
            return
          }
          [string]$latestUpUrl = $response.upload_url
          Write-Host 'latestUpUrl:'$latestUpUrl
          if ($latestUpUrl.Length -eq 0) {
            # 没查到,输出
            echo "::set-output name=needCreateRelease::true"  
          }
      # tag 创建github-Release
      - name: createReleaseWin
        id: createReleaseWin
        if: startsWith(github.event.ref, 'refs/tags/') && steps.queryReleaseMacos.outputs.needCreateRelease == 'true'
        env:
          GITHUB_TOKEN: $
        uses: actions/create-release@v1.0.0
        with:
          tag_name: $
          release_name: Release $
          body: $
          draft: false
          prerelease: false
      # 重定向upload_url到环境变量uploadUrl。
      - name: getLatestTagRelease
        # tag 上一步无论成功还是失败都执行
        if: startsWith(github.event.ref, 'refs/tags/')
        shell: pwsh
        env:
          githubFullName: $
          upUrl: $
          ref: $
        run: |
          # upUrl不为空,导出就完事
          if (${env:upUrl}.Length -gt 0) {
              $v=${env:upUrl}
              echo "::set-env name=uploadUrl::$v"
              return
          } 
          [string]$tag = ${env:ref}.Substring(${env:ref}.LastIndexOf('/') + 1)
          [string]$url = 'https://api.github.com/repos/' + ${env:githubFullName} + '/releases/tags/' + ${tag}
          $response = Invoke-RestMethod -Uri $url -Method Get
          [string]$latestUpUrl = $response.upload_url
          Write-Host 'latestUpUrl:'$latestUpUrl
          echo "::set-env name=uploadUrl::$latestUpUrl"
          Write-Host 'env uploadUrl:'${env:uploadUrl}
      # tag 上传Release
      - name: uploadRelease
        id: uploadRelease
        if: startsWith(github.event.ref, 'refs/tags/')
        env:
          GITHUB_TOKEN: $
        uses: actions/upload-release-asset@v1.0.1
        with:
          upload_url: $
          asset_path: ./bin/$.dmg
          asset_name: $.dmg
          asset_content_type: application/applefile