|
| 1 | +name: Build and Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - "v*" # Trigger the workflow on tags starting with "v" |
| 7 | + |
| 8 | +jobs: |
| 9 | + build: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + strategy: |
| 13 | + matrix: |
| 14 | + goos: [linux, darwin, windows] |
| 15 | + goarch: [386, amd64, arm, arm64] |
| 16 | + exclude: |
| 17 | + # Exclude certain combinations |
| 18 | + - goos: darwin |
| 19 | + goarch: 386 |
| 20 | + - goos: darwin |
| 21 | + goarch: arm |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Checkout code |
| 25 | + uses: actions/checkout@v4 |
| 26 | + |
| 27 | + - name: Set up Go |
| 28 | + uses: actions/setup-go@v5 |
| 29 | + with: |
| 30 | + go-version: "1.22" # Adjust the Go version if necessary |
| 31 | + |
| 32 | + - name: Build binary |
| 33 | + run: | |
| 34 | + mkdir -p bin |
| 35 | + GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -o bin/csv2json_${{ matrix.goos }}_${{ matrix.goarch }}$([[ ${{ matrix.goos }} == 'windows' ]] && echo .exe || echo '') ./cmd/csv2json/main.go |
| 36 | +
|
| 37 | + - name: List files for debug |
| 38 | + run: ls -la bin |
| 39 | + |
| 40 | + - name: Move binary to release directory |
| 41 | + run: | |
| 42 | + mkdir -p release |
| 43 | + ls -la release |
| 44 | + mv bin/csv2json_${{ matrix.goos }}_${{ matrix.goarch }}* release/ |
| 45 | + ls -la release |
| 46 | +
|
| 47 | + - name: List release directory for debug |
| 48 | + run: ls -la release |
| 49 | + |
| 50 | + - name: Upload Release Assets |
| 51 | + uses: actions/upload-artifact@v4 |
| 52 | + with: |
| 53 | + name: release-assets-${{ matrix.goos }}-${{ matrix.goarch }} |
| 54 | + path: release/ |
| 55 | + |
| 56 | + release: |
| 57 | + runs-on: ubuntu-latest |
| 58 | + needs: build |
| 59 | + |
| 60 | + steps: |
| 61 | + - name: Download Release Assets |
| 62 | + uses: actions/download-artifact@v4 |
| 63 | + with: |
| 64 | + path: release-assets |
| 65 | + pattern: release-assets-* |
| 66 | + merge-multiple: true |
| 67 | + |
| 68 | + - name: Create directories for compression |
| 69 | + run: mkdir -p release-compressed |
| 70 | + |
| 71 | + - name: Compress Release Assets |
| 72 | + run: | |
| 73 | + version=${GITHUB_REF#refs/tags/v} |
| 74 | +
|
| 75 | + # Set up name mappings |
| 76 | + declare -A goos_map=( ["linux"]="linux" ["darwin"]="macos" ["windows"]="windows" ) |
| 77 | + declare -A goarch_map=( ["386"]="x86" ["amd64"]="x64" ["arm"]="arm" ["arm64"]="arm64" ) |
| 78 | +
|
| 79 | + # Compress binaries |
| 80 | + for file in release-assets/*; do |
| 81 | + base_name=$(basename "$file") |
| 82 | + echo "Compressing $base_name" |
| 83 | +
|
| 84 | + # Extract goos and goarch |
| 85 | + parts=(${base_name//_/ }) |
| 86 | + goos=${parts[1]} |
| 87 | + goarch_with_ext=${parts[2]} |
| 88 | + goarch=${goarch_with_ext%%.*} # Remove the file extension |
| 89 | +
|
| 90 | + friendly_goos=${goos_map[$goos]} |
| 91 | + friendly_goarch=${goarch_map[$goarch]} |
| 92 | +
|
| 93 | + # Rename the file to csv2json or csv2json.exe |
| 94 | + if [[ $goos == "windows" ]]; then |
| 95 | + mv "$file" release-assets/csv2json.exe |
| 96 | + file="release-assets/csv2json.exe" |
| 97 | + else |
| 98 | + mv "$file" release-assets/csv2json |
| 99 | + file="release-assets/csv2json" |
| 100 | + fi |
| 101 | +
|
| 102 | + # Use .zip for macOS and Windows, .tar.gz for Linux |
| 103 | + if [[ $goos == "windows" || $goos == "darwin" ]]; then |
| 104 | + zip_filename="csv2json-${version}-${friendly_goos}-${friendly_goarch}.zip" |
| 105 | + zip release-compressed/${zip_filename} -j "$file" |
| 106 | + else |
| 107 | + tar_filename="csv2json-${version}-${friendly_goos}-${friendly_goarch}.tar.gz" |
| 108 | + tar -czvf release-compressed/${tar_filename} -C release-assets csv2json |
| 109 | + fi |
| 110 | + done |
| 111 | +
|
| 112 | + - name: Create GitHub Release |
| 113 | + uses: softprops/action-gh-release@v2 |
| 114 | + with: |
| 115 | + files: release-compressed/* |
| 116 | + env: |
| 117 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments