|
| 1 | +name: Build and Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + tags: ['v*'] |
| 7 | + pull_request: |
| 8 | + branches: [main] |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + test: |
| 15 | + name: Test |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v4 |
| 19 | + |
| 20 | + - name: Set up Go |
| 21 | + uses: actions/setup-go@v5 |
| 22 | + with: |
| 23 | + go-version: '1.22' |
| 24 | + |
| 25 | + - name: Install PC/SC development files |
| 26 | + run: sudo apt-get update && sudo apt-get install -y libpcsclite-dev |
| 27 | + |
| 28 | + - name: Run tests |
| 29 | + run: go test -v -race -coverprofile=coverage.out ./... |
| 30 | + |
| 31 | + - name: Upload coverage |
| 32 | + uses: codecov/codecov-action@v4 |
| 33 | + with: |
| 34 | + files: ./coverage.out |
| 35 | + fail_ci_if_error: false |
| 36 | + |
| 37 | + build: |
| 38 | + name: Build (${{ matrix.os }}) |
| 39 | + needs: test |
| 40 | + strategy: |
| 41 | + fail-fast: false |
| 42 | + matrix: |
| 43 | + include: |
| 44 | + - os: ubuntu-latest |
| 45 | + goos: linux |
| 46 | + goarch: amd64 |
| 47 | + - os: macos-13 |
| 48 | + goos: darwin |
| 49 | + goarch: amd64 |
| 50 | + - os: macos-latest |
| 51 | + goos: darwin |
| 52 | + goarch: arm64 |
| 53 | + - os: windows-latest |
| 54 | + goos: windows |
| 55 | + goarch: amd64 |
| 56 | + |
| 57 | + runs-on: ${{ matrix.os }} |
| 58 | + |
| 59 | + steps: |
| 60 | + - uses: actions/checkout@v4 |
| 61 | + |
| 62 | + - name: Set up Go |
| 63 | + uses: actions/setup-go@v5 |
| 64 | + with: |
| 65 | + go-version: '1.22' |
| 66 | + |
| 67 | + # Linux dependencies |
| 68 | + - name: Install Linux dependencies |
| 69 | + if: matrix.goos == 'linux' |
| 70 | + run: sudo apt-get update && sudo apt-get install -y libpcsclite-dev |
| 71 | + |
| 72 | + - name: Build |
| 73 | + env: |
| 74 | + GOOS: ${{ matrix.goos }} |
| 75 | + GOARCH: ${{ matrix.goarch }} |
| 76 | + CGO_ENABLED: 1 |
| 77 | + run: | |
| 78 | + VERSION="${GITHUB_REF_NAME:-dev}" |
| 79 | + BUILD_TIME=$(date -u +%Y-%m-%dT%H:%M:%SZ) |
| 80 | + GIT_COMMIT="${GITHUB_SHA:-unknown}" |
| 81 | +
|
| 82 | + go build -ldflags="-s -w -X 'github.com/SimplyPrint/nfc-agent/internal/api.Version=${VERSION}' -X 'github.com/SimplyPrint/nfc-agent/internal/api.BuildTime=${BUILD_TIME}' -X 'github.com/SimplyPrint/nfc-agent/internal/api.GitCommit=${GIT_COMMIT}'" -o dist/nfc-agent${{ matrix.goos == 'windows' && '.exe' || '' }} ./cmd/nfc-agent |
| 83 | +
|
| 84 | + - name: Upload artifact |
| 85 | + uses: actions/upload-artifact@v4 |
| 86 | + with: |
| 87 | + name: nfc-agent-${{ matrix.goos }}-${{ matrix.goarch }} |
| 88 | + path: dist/ |
| 89 | + |
| 90 | + # macOS code signing and DMG creation |
| 91 | + macos-package: |
| 92 | + name: Package macOS |
| 93 | + needs: build |
| 94 | + if: startsWith(github.ref, 'refs/tags/') |
| 95 | + runs-on: macos-latest |
| 96 | + |
| 97 | + steps: |
| 98 | + - uses: actions/checkout@v4 |
| 99 | + |
| 100 | + - name: Download macOS ARM64 artifact |
| 101 | + uses: actions/download-artifact@v4 |
| 102 | + with: |
| 103 | + name: nfc-agent-darwin-arm64 |
| 104 | + path: dist/arm64/ |
| 105 | + |
| 106 | + - name: Download macOS AMD64 artifact |
| 107 | + uses: actions/download-artifact@v4 |
| 108 | + with: |
| 109 | + name: nfc-agent-darwin-amd64 |
| 110 | + path: dist/amd64/ |
| 111 | + |
| 112 | + - name: Create universal binary |
| 113 | + run: | |
| 114 | + mkdir -p dist/universal |
| 115 | + lipo -create dist/arm64/nfc-agent dist/amd64/nfc-agent -output dist/universal/nfc-agent |
| 116 | + chmod +x dist/universal/nfc-agent |
| 117 | +
|
| 118 | + - name: Import Code Signing Certificate |
| 119 | + if: env.APPLE_CERTIFICATE != '' |
| 120 | + env: |
| 121 | + APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }} |
| 122 | + APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }} |
| 123 | + run: | |
| 124 | + echo "$APPLE_CERTIFICATE" | base64 --decode > certificate.p12 |
| 125 | + security create-keychain -p "" build.keychain |
| 126 | + security default-keychain -s build.keychain |
| 127 | + security unlock-keychain -p "" build.keychain |
| 128 | + security import certificate.p12 -k build.keychain -P "$APPLE_CERTIFICATE_PASSWORD" -T /usr/bin/codesign |
| 129 | + security set-key-partition-list -S apple-tool:,apple: -s -k "" build.keychain |
| 130 | +
|
| 131 | + - name: Create App Bundle |
| 132 | + run: | |
| 133 | + mkdir -p "dist/NFC Agent.app/Contents/MacOS" |
| 134 | + mkdir -p "dist/NFC Agent.app/Contents/Resources" |
| 135 | + cp dist/universal/nfc-agent "dist/NFC Agent.app/Contents/MacOS/" |
| 136 | + cp build/darwin/Info.plist "dist/NFC Agent.app/Contents/" |
| 137 | + # Update version in Info.plist |
| 138 | + VERSION="${GITHUB_REF_NAME#v}" |
| 139 | + sed -i '' "s/1.0.0/${VERSION}/g" "dist/NFC Agent.app/Contents/Info.plist" |
| 140 | +
|
| 141 | + - name: Sign App Bundle |
| 142 | + if: env.APPLE_CERTIFICATE != '' |
| 143 | + env: |
| 144 | + APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }} |
| 145 | + APPLE_IDENTITY: ${{ secrets.APPLE_IDENTITY }} |
| 146 | + run: | |
| 147 | + codesign --force --options runtime --sign "$APPLE_IDENTITY" --entitlements build/darwin/entitlements.plist "dist/NFC Agent.app" |
| 148 | +
|
| 149 | + - name: Create DMG |
| 150 | + run: | |
| 151 | + VERSION="${GITHUB_REF_NAME#v}" |
| 152 | + hdiutil create -volname "NFC Agent" -srcfolder "dist/NFC Agent.app" -ov -format UDZO "dist/NFC-Agent-${VERSION}.dmg" |
| 153 | +
|
| 154 | + - name: Notarize DMG |
| 155 | + if: env.APPLE_ID != '' |
| 156 | + env: |
| 157 | + APPLE_ID: ${{ secrets.APPLE_ID }} |
| 158 | + APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }} |
| 159 | + APPLE_APP_PASSWORD: ${{ secrets.APPLE_APP_PASSWORD }} |
| 160 | + run: | |
| 161 | + VERSION="${GITHUB_REF_NAME#v}" |
| 162 | + xcrun notarytool submit "dist/NFC-Agent-${VERSION}.dmg" --apple-id "$APPLE_ID" --team-id "$APPLE_TEAM_ID" --password "$APPLE_APP_PASSWORD" --wait |
| 163 | + xcrun stapler staple "dist/NFC-Agent-${VERSION}.dmg" |
| 164 | +
|
| 165 | + - name: Upload DMG artifact |
| 166 | + uses: actions/upload-artifact@v4 |
| 167 | + with: |
| 168 | + name: nfc-agent-macos-dmg |
| 169 | + path: dist/*.dmg |
| 170 | + |
| 171 | + # Windows installer |
| 172 | + windows-package: |
| 173 | + name: Package Windows |
| 174 | + needs: build |
| 175 | + if: startsWith(github.ref, 'refs/tags/') |
| 176 | + runs-on: windows-latest |
| 177 | + |
| 178 | + steps: |
| 179 | + - uses: actions/checkout@v4 |
| 180 | + |
| 181 | + - name: Download Windows artifact |
| 182 | + uses: actions/download-artifact@v4 |
| 183 | + with: |
| 184 | + name: nfc-agent-windows-amd64 |
| 185 | + path: dist/nfc-agent_windows_amd64_v1/ |
| 186 | + |
| 187 | + - name: Install Inno Setup |
| 188 | + run: choco install innosetup -y |
| 189 | + |
| 190 | + - name: Build installer |
| 191 | + run: | |
| 192 | + $version = "${{ github.ref_name }}".TrimStart('v') |
| 193 | + & "C:\Program Files (x86)\Inno Setup 6\ISCC.exe" /DVersion=$version build\windows\installer.iss |
| 194 | +
|
| 195 | + - name: Upload installer artifact |
| 196 | + uses: actions/upload-artifact@v4 |
| 197 | + with: |
| 198 | + name: nfc-agent-windows-installer |
| 199 | + path: dist/*.exe |
| 200 | + |
| 201 | + # Linux packages |
| 202 | + linux-package: |
| 203 | + name: Package Linux |
| 204 | + needs: build |
| 205 | + if: startsWith(github.ref, 'refs/tags/') |
| 206 | + runs-on: ubuntu-latest |
| 207 | + |
| 208 | + steps: |
| 209 | + - uses: actions/checkout@v4 |
| 210 | + |
| 211 | + - name: Set up Go |
| 212 | + uses: actions/setup-go@v5 |
| 213 | + with: |
| 214 | + go-version: '1.22' |
| 215 | + |
| 216 | + - name: Install dependencies |
| 217 | + run: | |
| 218 | + sudo apt-get update |
| 219 | + sudo apt-get install -y libpcsclite-dev |
| 220 | +
|
| 221 | + - name: Install GoReleaser |
| 222 | + uses: goreleaser/goreleaser-action@v6 |
| 223 | + with: |
| 224 | + distribution: goreleaser |
| 225 | + version: latest |
| 226 | + install-only: true |
| 227 | + |
| 228 | + - name: Build packages with GoReleaser |
| 229 | + run: | |
| 230 | + goreleaser release --clean --skip=publish --config .goreleaser.yaml |
| 231 | + env: |
| 232 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 233 | + |
| 234 | + - name: Upload deb package |
| 235 | + uses: actions/upload-artifact@v4 |
| 236 | + with: |
| 237 | + name: nfc-agent-linux-deb |
| 238 | + path: dist/*.deb |
| 239 | + |
| 240 | + - name: Upload rpm package |
| 241 | + uses: actions/upload-artifact@v4 |
| 242 | + with: |
| 243 | + name: nfc-agent-linux-rpm |
| 244 | + path: dist/*.rpm |
| 245 | + |
| 246 | + # Create release |
| 247 | + release: |
| 248 | + name: Create Release |
| 249 | + needs: [macos-package, windows-package, linux-package] |
| 250 | + if: startsWith(github.ref, 'refs/tags/') |
| 251 | + runs-on: ubuntu-latest |
| 252 | + |
| 253 | + steps: |
| 254 | + - uses: actions/checkout@v4 |
| 255 | + |
| 256 | + - name: Download all artifacts |
| 257 | + uses: actions/download-artifact@v4 |
| 258 | + with: |
| 259 | + path: artifacts/ |
| 260 | + |
| 261 | + - name: Prepare release files |
| 262 | + run: | |
| 263 | + mkdir -p release |
| 264 | + find artifacts -type f \( -name "*.dmg" -o -name "*.exe" -o -name "*.deb" -o -name "*.rpm" -o -name "*.tar.gz" -o -name "*.zip" \) -exec cp {} release/ \; |
| 265 | + ls -la release/ |
| 266 | +
|
| 267 | + - name: Create Release |
| 268 | + uses: softprops/action-gh-release@v2 |
| 269 | + with: |
| 270 | + files: release/* |
| 271 | + generate_release_notes: true |
| 272 | + draft: false |
| 273 | + prerelease: ${{ contains(github.ref, '-') }} |
| 274 | + env: |
| 275 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 276 | + |
| 277 | + # Update Homebrew tap |
| 278 | + homebrew: |
| 279 | + name: Update Homebrew |
| 280 | + needs: release |
| 281 | + if: startsWith(github.ref, 'refs/tags/') && !contains(github.ref, '-') |
| 282 | + runs-on: ubuntu-latest |
| 283 | + |
| 284 | + steps: |
| 285 | + - name: Update Homebrew formula |
| 286 | + uses: dawidd6/action-homebrew-bump-formula@v3 |
| 287 | + with: |
| 288 | + token: ${{ secrets.HOMEBREW_TAP_TOKEN }} |
| 289 | + tap: SimplyPrint/homebrew-tap |
| 290 | + formula: nfc-agent |
| 291 | + tag: ${{ github.ref_name }} |
0 commit comments