|
| 1 | +name: Build and Deploy AO/HyperBEAM |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +env: |
| 9 | + GCP_IMAGE_NAME: hyperbeam-image |
| 10 | + GCP_PROJECT: hyperbeam-cd |
| 11 | + GCP_INSTANCE_NAME: hyperbeam |
| 12 | + GCP_ZONE: us-central1-a |
| 13 | + |
| 14 | +jobs: |
| 15 | + build: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + outputs: |
| 18 | + image_name: ${{ steps.set_image_name.outputs.image_name }} |
| 19 | + steps: |
| 20 | + - uses: actions/checkout@v4 |
| 21 | + - id: auth |
| 22 | + name: Authenticate to Google Cloud |
| 23 | + uses: google-github-actions/auth@v2 |
| 24 | + with: |
| 25 | + credentials_json: ${{ secrets.CD_SERVICE_ACCOUNT }} |
| 26 | + |
| 27 | + - name: Setup GCloud SDK |
| 28 | + uses: google-github-actions/setup-gcloud@v2 |
| 29 | + |
| 30 | + - name: Setup build tools (Erlang, Packer and Rebar3) |
| 31 | + run: | |
| 32 | + curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - |
| 33 | + sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" |
| 34 | + sudo apt-get update |
| 35 | + sudo apt-get install -y packer git libssl-dev ncurses-dev make cmake gcc g++ |
| 36 | + git clone https://github.com/erlang/otp.git && cd otp && git checkout maint-27 && ./configure && make -j8 && sudo make install |
| 37 | + git clone https://github.com/erlang/rebar3.git && cd rebar3 && ./bootstrap && sudo mv rebar3 /usr/local/bin/ |
| 38 | +
|
| 39 | + - name: Build and release AO/HyperBEAM with Rebar3 |
| 40 | + run: | |
| 41 | + rebar3 clean |
| 42 | + rebar3 get-deps |
| 43 | + rebar3 compile |
| 44 | + rebar3 release |
| 45 | +
|
| 46 | + - name: Set image name with timestamp and commit SHA |
| 47 | + id: set_image_name |
| 48 | + run: | |
| 49 | + SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-7) |
| 50 | + TIMESTAMPED_IMAGE_NAME="${{ env.GCP_IMAGE_NAME }}-${SHORT_SHA}-$(date +%Y%m%d-%H%M%S)" |
| 51 | + echo "image_name=${TIMESTAMPED_IMAGE_NAME}" >> "$GITHUB_OUTPUT" |
| 52 | +
|
| 53 | + - name: Build Packer Image |
| 54 | + run: | |
| 55 | + packer init . |
| 56 | + packer validate . |
| 57 | + packer build -var "image_name=${{ steps.set_image_name.outputs.image_name }}" -var "project_id=${{ env.GCP_PROJECT }}" . |
| 58 | +
|
| 59 | + - name: Tag image for reference |
| 60 | + run: | |
| 61 | + gcloud compute images add-labels ${{ steps.set_image_name.outputs.image_name }} \ |
| 62 | + --project=${{ env.GCP_PROJECT }} \ |
| 63 | + --labels=workflow_run=${{ github.run_id }},commit_sha=${{ github.sha }} |
| 64 | +
|
| 65 | + deploy: |
| 66 | + needs: build |
| 67 | + runs-on: ubuntu-latest |
| 68 | + steps: |
| 69 | + - id: auth |
| 70 | + name: Authenticate to Google Cloud |
| 71 | + uses: google-github-actions/auth@v2 |
| 72 | + with: |
| 73 | + credentials_json: ${{ secrets.CD_SERVICE_ACCOUNT }} |
| 74 | + |
| 75 | + - name: Setup GCloud SDK |
| 76 | + uses: google-github-actions/setup-gcloud@v2 |
| 77 | + |
| 78 | + - name: Create Confidential VM |
| 79 | + run: | |
| 80 | + gcloud compute instances create ${{ env.GCP_INSTANCE_NAME }} \ |
| 81 | + --zone=${{ env.GCP_ZONE }} \ |
| 82 | + --machine-type=n2d-standard-2 \ |
| 83 | + --min-cpu-platform="AMD Milan" \ |
| 84 | + --confidential-compute-type=SEV_SNP \ |
| 85 | + --maintenance-policy=TERMINATE \ |
| 86 | + --image-family=ubuntu-2404-lts-amd64 \ |
| 87 | + --image-project=ubuntu-os-cloud \ |
| 88 | + --project=${{ env.GCP_PROJECT }} \ |
| 89 | + --network-interface=network-tier=PREMIUM,nic-type=GVNIC,stack-type=IPV4_ONLY,subnet=default \ |
| 90 | + --tags=http-server,https-server \ |
| 91 | + --shielded-secure-boot \ |
| 92 | + --shielded-vtpm \ |
| 93 | + --shielded-integrity-monitoring \ |
| 94 | + --create-disk=auto-delete=yes,boot=yes,device-name=${{ env.GCP_INSTANCE_NAME }},image=projects/${{ env.GCP_PROJECT }}/global/images/${{ needs.build.outputs.image_name }},mode=rw,size=20,type=pd-balanced |
| 95 | +
|
| 96 | + test: |
| 97 | + needs: deploy |
| 98 | + runs-on: ubuntu-latest |
| 99 | + steps: |
| 100 | + - name: Wait for deployment |
| 101 | + run: sleep 60 # Add appropriate wait time for your service to start |
| 102 | + |
| 103 | + - name: Run tests |
| 104 | + run: | |
| 105 | + # Add your test commands here |
| 106 | + echo "Running tests..." |
| 107 | +
|
| 108 | + cleanup: |
| 109 | + needs: [build, test] # Added build to needs to access the image name |
| 110 | + if: always() |
| 111 | + runs-on: ubuntu-latest |
| 112 | + steps: |
| 113 | + - id: auth |
| 114 | + name: Authenticate to Google Cloud |
| 115 | + uses: google-github-actions/auth@v2 |
| 116 | + with: |
| 117 | + credentials_json: ${{ secrets.CD_SERVICE_ACCOUNT }} |
| 118 | + |
| 119 | + - name: Setup GCloud SDK |
| 120 | + uses: google-github-actions/setup-gcloud@v2 |
| 121 | + |
| 122 | + - name: Delete Confidential VM |
| 123 | + run: | |
| 124 | + gcloud compute instances delete ${{ env.GCP_INSTANCE_NAME }} \ |
| 125 | + --project=${{ env.GCP_PROJECT }} \ |
| 126 | + --zone=${{ env.GCP_ZONE }} \ |
| 127 | + --quiet |
| 128 | +
|
| 129 | + - name: Clean up old images |
| 130 | + run: | |
| 131 | + # Keep only the last 5 images |
| 132 | + gcloud compute images list \ |
| 133 | + --project=${{ env.GCP_PROJECT }} \ |
| 134 | + --filter="name ~ '^${{ env.GCP_IMAGE_NAME }}-'" \ |
| 135 | + --format="get(name)" \ |
| 136 | + --sort-by=~creationTimestamp \ |
| 137 | + | tail -n +6 \ |
| 138 | + | xargs -r gcloud compute images delete --quiet --project=${{ env.GCP_PROJECT }} |
0 commit comments