|
| 1 | +name: build binary |
| 2 | +on: |
| 3 | + release: |
| 4 | + types: [published] |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + version: |
| 8 | + description: "Version tag to build (e.g., 9.0.0)" |
| 9 | + required: true |
| 10 | + type: string |
| 11 | + |
| 12 | +jobs: |
| 13 | + determine_whether_to_run: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + outputs: |
| 16 | + build_arc: ${{ steps.check-build-arc.outputs.run_jobs }} |
| 17 | + version: ${{ steps.get-version.outputs.version }} |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Check if Arc binary should be built |
| 21 | + id: check-build-arc |
| 22 | + env: |
| 23 | + INPUT_VERSION: ${{ inputs.version }} |
| 24 | + RELEASE_TAG: ${{ github.event.release.tag_name }} |
| 25 | + run: | |
| 26 | + TAG_NAME="${INPUT_VERSION:-${RELEASE_TAG}}" |
| 27 | + echo "Checking tag $TAG_NAME for ARC build" |
| 28 | + if echo "$TAG_NAME" | grep -Eq '^([8-9]|[1-9][0-9]+)\.[0-9]+\.[0-9]+$'; then |
| 29 | + echo "run_jobs=true" >> "$GITHUB_OUTPUT" |
| 30 | + else |
| 31 | + echo "run_jobs=false" >> "$GITHUB_OUTPUT" |
| 32 | + fi |
| 33 | +
|
| 34 | + - name: Get the version from the tag |
| 35 | + id: get-version |
| 36 | + env: |
| 37 | + INPUT_VERSION: ${{ inputs.version }} |
| 38 | + RELEASE_TAG: ${{ github.event.release.tag_name }} |
| 39 | + run: | |
| 40 | + VERSION="${INPUT_VERSION:-${RELEASE_TAG}}" |
| 41 | + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" |
| 42 | +
|
| 43 | + build-arc: |
| 44 | + name: Build ARC binary |
| 45 | + needs: determine_whether_to_run |
| 46 | + if: needs.determine_whether_to_run.outputs.build_arc == 'true' |
| 47 | + runs-on: ubuntu-latest |
| 48 | + timeout-minutes: 60 |
| 49 | + steps: |
| 50 | + - name: Checkout Repository |
| 51 | + uses: actions/checkout@v2 |
| 52 | + |
| 53 | + # Build AMI zip on AWS host and upload to this release by tag |
| 54 | + - name: building binaries for ami |
| 55 | + uses: appleboy/ssh-action@v0.1.7 |
| 56 | + env: |
| 57 | + VERSION: ${{ needs.determine_whether_to_run.outputs.version }} |
| 58 | + PAT_ACCESS_TOKEN: ${{ secrets.PAT_ACCESS_TOKEN }} |
| 59 | + with: |
| 60 | + host: ${{ secrets.AWS_BUILD_HOST }} |
| 61 | + username: ${{ secrets.AWS_BUILD_USERNAME }} |
| 62 | + key: ${{ secrets.AWS_BUILD_KEY }} |
| 63 | + port: 22 |
| 64 | + envs: VERSION,PAT_ACCESS_TOKEN |
| 65 | + script: | |
| 66 | + set -euo pipefail |
| 67 | + export PATH=$PATH:/usr/local/go/bin:/usr/bin |
| 68 | + export GOPATH=$HOME/go |
| 69 | +
|
| 70 | + # Write build helper script on the remote host |
| 71 | + cat > "$HOME/rs_build.sh" <<'EOS' |
| 72 | + #!/usr/bin/env bash |
| 73 | + # Usage: rs_build.sh <version> <mode> [branch_or_tag] |
| 74 | + # mode: ami | cluster |
| 75 | + set -euo pipefail |
| 76 | +
|
| 77 | + version="${1:?version required}" |
| 78 | + mode="${2:-ami}" |
| 79 | + branch="${3:-$version}" |
| 80 | +
|
| 81 | + # ----- Token resolution ----- |
| 82 | + # Use env PAT_ACCESS_TOKEN or ~/.rs_pat if present. |
| 83 | + PAT_ACCESS_TOKEN="${PAT_ACCESS_TOKEN:-}" |
| 84 | + if [[ -z "${PAT_ACCESS_TOKEN}" && -f "${HOME}/.rs_pat" ]]; then |
| 85 | + PAT_ACCESS_TOKEN="$(cat "${HOME}/.rs_pat")" |
| 86 | + fi |
| 87 | + : "${PAT_ACCESS_TOKEN:?PAT_ACCESS_TOKEN missing (export it or place it in ~/.rs_pat)}" |
| 88 | +
|
| 89 | + if [[ "${mode}" != "ami" && "${mode}" != "cluster" ]]; then |
| 90 | + echo "mode must be 'ami' or 'cluster'"; exit 1 |
| 91 | + fi |
| 92 | +
|
| 93 | + # ----- Prereqs ----- |
| 94 | + command -v go >/dev/null || { echo "go not found on builder"; exit 1; } |
| 95 | + command -v zip >/dev/null || { echo "zip not found on builder"; exit 1; } |
| 96 | + command -v curl >/dev/null || { echo "curl not found on builder"; exit 1; } |
| 97 | + command -v git >/dev/null || { echo "git not found on builder"; exit 1; } |
| 98 | +
|
| 99 | + export GO111MODULE=on |
| 100 | + export CGO_ENABLED=1 |
| 101 | + export PATH="$PATH:/usr/local/go/bin:/usr/bin" |
| 102 | +
|
| 103 | + # ----- Clone & build ----- |
| 104 | + sudo rm -rf arc-noss build out 2>/dev/null || true |
| 105 | + echo "Cloning appbaseio-confidential/rs-api-server @ ${branch}" |
| 106 | + git clone --depth=1 -b "${branch}" "https://${PAT_ACCESS_TOKEN}@github.com/appbaseio-confidential/rs-api-server" arc-noss |
| 107 | +
|
| 108 | + pushd arc-noss >/dev/null |
| 109 | + make clean |
| 110 | + if [[ "${mode}" == "ami" ]]; then |
| 111 | + BILLING=true CLUSTER_BILLING= VERSION="${version}" make |
| 112 | + else |
| 113 | + BILLING= CLUSTER_BILLING=true VERSION="${version}" make |
| 114 | + fi |
| 115 | + popd >/dev/null |
| 116 | +
|
| 117 | + mkdir -p out |
| 118 | + # Zip without the leading arc-noss/ so the archive has build/ and go/ at root |
| 119 | + pushd arc-noss >/dev/null |
| 120 | + zip -r "../out/arc-linux-${mode}.zip" build go >/dev/null |
| 121 | + popd >/dev/null |
| 122 | + echo "Packaged out/arc-linux-${mode}.zip (root: build/, go/)" |
| 123 | +
|
| 124 | + # ----- Upload to GitHub release (by tag) ----- |
| 125 | + owner="appbaseio" |
| 126 | + repo="reactivesearch-api" |
| 127 | + tag="${version}" |
| 128 | +
|
| 129 | + release_json="$(curl -fsSL -H "Authorization: token ${PAT_ACCESS_TOKEN}" \ |
| 130 | + "https://api.github.com/repos/${owner}/${repo}/releases/tags/${tag}")" || { |
| 131 | + echo "Failed to fetch release for tag ${tag}"; exit 1; } |
| 132 | +
|
| 133 | + # First id field is the release id |
| 134 | + release_id="$(printf '%s' "${release_json}" | awk -F: '/"id":/{gsub(/[ ,]/,"",$2); print $2; exit}')" || true |
| 135 | + if [[ -z "${release_id}" ]]; then |
| 136 | + echo "Could not parse release id for tag ${tag}"; exit 1 |
| 137 | + fi |
| 138 | +
|
| 139 | + asset_path="out/arc-linux-${mode}.zip" |
| 140 | + asset_name="$(basename "${asset_path}")" |
| 141 | + upload_url="https://uploads.github.com/repos/${owner}/${repo}/releases/${release_id}/assets?name=${asset_name}" |
| 142 | +
|
| 143 | + echo "Uploading ${asset_name} to ${owner}/${repo} (release ${tag})..." |
| 144 | + http_code="$(curl -sS -w '%{http_code}' -o /tmp/upload_resp.json \ |
| 145 | + -X POST -H "Authorization: token ${PAT_ACCESS_TOKEN}" \ |
| 146 | + -H "Content-Type: application/octet-stream" \ |
| 147 | + --data-binary @"${asset_path}" \ |
| 148 | + "${upload_url}")" |
| 149 | +
|
| 150 | + if [[ "${http_code}" == "201" ]]; then |
| 151 | + echo "Upload successful: ${asset_name}" |
| 152 | + elif [[ "${http_code}" == "422" ]]; then |
| 153 | + echo "Asset exists; attempting replace…" |
| 154 | + if command -v jq >/dev/null 2>&1; then |
| 155 | + assets_url="$(printf '%s' "${release_json}" | jq -r '.assets_url')" |
| 156 | + assets_json="$(curl -fsSL -H "Authorization: token ${PAT_ACCESS_TOKEN}" "${assets_url}")" |
| 157 | + existing_id="$(printf '%s' "${assets_json}" | jq -r ".[] | select(.name==\"${asset_name}\") | .id")" || true |
| 158 | + if [[ -n "${existing_id}" && "${existing_id}" != "null" ]]; then |
| 159 | + curl -fsSL -X DELETE -H "Authorization: token ${PAT_ACCESS_TOKEN}" \ |
| 160 | + "https://api.github.com/repos/${owner}/${repo}/releases/assets/${existing_id}" || true |
| 161 | + http_code2="$(curl -sS -w '%{http_code}' -o /tmp/upload_resp2.json \ |
| 162 | + -X POST -H "Authorization: token ${PAT_ACCESS_TOKEN}" \ |
| 163 | + -H "Content-Type: application/octet-stream" \ |
| 164 | + --data-binary @"${asset_path}" \ |
| 165 | + "${upload_url}")" |
| 166 | + [[ "${http_code2}" == "201" ]] && echo "Re-upload successful" || { echo "Re-upload failed (${http_code2})"; cat /tmp/upload_resp2.json; exit 1; } |
| 167 | + else |
| 168 | + echo "Could not find existing asset id to delete."; cat /tmp/upload_resp.json; exit 1 |
| 169 | + fi |
| 170 | + else |
| 171 | + echo "jq not available; cannot replace existing asset automatically."; cat /tmp/upload_resp.json; exit 1 |
| 172 | + fi |
| 173 | + else |
| 174 | + echo "Upload failed (${http_code})."; cat /tmp/upload_resp.json; exit 1 |
| 175 | + fi |
| 176 | +
|
| 177 | + echo "Done." |
| 178 | + EOS |
| 179 | + chmod +x "$HOME/rs_build.sh" |
| 180 | +
|
| 181 | + # Build AMI artifact first so Packer can fetch it |
| 182 | + "$HOME/rs_build.sh" "${VERSION}" ami "${VERSION}" |
| 183 | +
|
| 184 | + - name: Build AMI |
| 185 | + uses: hashicorp/packer-github-actions@master |
| 186 | + with: |
| 187 | + command: build |
| 188 | + target: "./ami.json" |
| 189 | + env: |
| 190 | + PACKER_LOG: 1 |
| 191 | + AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }} |
| 192 | + AWS_SECRET_KEY: ${{ secrets.AWS_SECRET_KEY }} |
| 193 | + VERSION: ${{ needs.determine_whether_to_run.outputs.version }} |
| 194 | + |
| 195 | + # Reuse the same AWS host to build the cluster zip and upload it |
| 196 | + - name: building binaries for cluster (linux/amd64) |
| 197 | + uses: appleboy/ssh-action@v0.1.7 |
| 198 | + env: |
| 199 | + VERSION: ${{ needs.determine_whether_to_run.outputs.version }} |
| 200 | + PAT_ACCESS_TOKEN: ${{ secrets.PAT_ACCESS_TOKEN }} |
| 201 | + with: |
| 202 | + host: ${{ secrets.AWS_BUILD_HOST }} |
| 203 | + username: ${{ secrets.AWS_BUILD_USERNAME }} |
| 204 | + key: ${{ secrets.AWS_BUILD_KEY }} |
| 205 | + port: 22 |
| 206 | + envs: VERSION,PAT_ACCESS_TOKEN |
| 207 | + script: | |
| 208 | + set -euo pipefail |
| 209 | + export PATH=$PATH:/usr/local/go/bin:/usr/bin |
| 210 | + export GOPATH=$HOME/go |
| 211 | + if [[ ! -x "$HOME/rs_build.sh" ]]; then |
| 212 | + echo "Missing build helper; abort."; exit 1 |
| 213 | + fi |
| 214 | + "$HOME/rs_build.sh" "${VERSION}" cluster "${VERSION}" |
| 215 | +
|
| 216 | + send-packer-event-arc: |
| 217 | + name: Send Packer Event |
| 218 | + needs: build-arc |
| 219 | + uses: ./.github/workflows/build_images.yml |
| 220 | + with: |
| 221 | + # Pass a tag-style ref so the called workflow's regex gate passes on manual runs |
| 222 | + ref: "refs/tags/${{ needs.determine_whether_to_run.outputs.version }}" |
| 223 | + event_name: new_release |
| 224 | + secrets: |
| 225 | + token: ${{ secrets.REPO_ACCESS_TOKEN }} |
| 226 | + |
| 227 | + send-docker-event-arc: |
| 228 | + name: Send Docker Event |
| 229 | + needs: [build-arc, determine_whether_to_run] |
| 230 | + runs-on: ubuntu-latest |
| 231 | + steps: |
| 232 | + - name: Send repo dispatch |
| 233 | + uses: peter-evans/repository-dispatch@v2 |
| 234 | + with: |
| 235 | + token: ${{ secrets.REPO_ACCESS_TOKEN }} |
| 236 | + repository: appbaseio-confidential/rs-api-server |
| 237 | + event-type: publish_docker |
| 238 | + client-payload: '{"version": "${{ needs.determine_whether_to_run.outputs.version }}" }' |
0 commit comments