-
Notifications
You must be signed in to change notification settings - Fork 11
181 lines (158 loc) · 6.42 KB
/
build_gcp_azure_manual.yml
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
name: Azure & GCP AMI manual publish
on:
workflow_dispatch:
jobs:
build:
name: Build the Azure & GCP AMIs using Packer
strategy:
matrix:
cloud: [azure, gcp]
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
env:
# GCP
PKR_VAR_project_id: spacelift-workers
PKR_VAR_account_file: ./gcp.json
PKR_VAR_image_base_name: spacelift-worker
PKR_VAR_image_family: spacelift-worker
# Azure
PKR_VAR_client_id: ${{ secrets.AZURE_CLIENT_ID }}
PKR_VAR_client_secret: ${{ secrets.AZURE_CLIENT_SECRET }}
PKR_VAR_tenant_id: ${{ secrets.AZURE_TENANT_ID }}
PKR_VAR_subscription_id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
PKR_VAR_image_resource_group: rg-worker_images-public-westeurope
PKR_VAR_packer_work_group: rg-worker_images_packer-public-westeurope
PKR_VAR_gallery_resource_group: rg-worker_images-public-westeurope
PKR_VAR_gallery_name: worker_images_public
PKR_VAR_gallery_image_name: ubuntu_20_04
PKR_VAR_gallery_replication_regions: '["westeurope"]'
PKR_VAR_gallery_image_version: 1.0.${{ github.run_number }}
steps:
- name: Check out the source code
uses: actions/checkout@main
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ secrets.AWS_REGION }}
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
role-duration-seconds: 3600
- name: Set up Google Cloud SDK
if: matrix.cloud == 'gcp'
uses: google-github-actions/setup-gcloud@v1
- name: Create account file for GCP
if: matrix.cloud == 'gcp'
run: |
echo '${{ secrets.GCP_CREDENTIALS_JSON }}' > ${{ env.PKR_VAR_account_file }}
- name: Authenticate with GCP
if: matrix.cloud == 'gcp'
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_CREDENTIALS_JSON }}
- name: Export suffix for GCP
if: matrix.cloud == 'gcp'
run: |
echo "PKR_VAR_suffix=$(date +%s)-$(cat /dev/urandom | tr -dc 'a-z0-9' | head -c 8)" >> $GITHUB_ENV
- name: Setup packer
uses: hashicorp/setup-packer@main
with:
version: latest
- name: Initialize Packer
run: packer init ${{ matrix.cloud }}.pkr.hcl
- name: Azure => Build the AMI using Packer
if: matrix.cloud == 'azure'
run: packer build azure.pkr.hcl
- name: GCP => Build the AMI using Packer for US
if: matrix.cloud == 'gcp'
run: packer build gcp.pkr.hcl
env:
PKR_VAR_image_storage_location: us
PKR_VAR_zone: us-central1-a
- name: GCP => Build the AMI using Packer for EU
if: matrix.cloud == 'gcp'
run: packer build gcp.pkr.hcl
env:
PKR_VAR_image_storage_location: eu
PKR_VAR_zone: europe-west1-d
- name: GCP => Build the AMI using Packer for Asia
if: matrix.cloud == 'gcp'
run: packer build gcp.pkr.hcl
env:
PKR_VAR_image_storage_location: asia
PKR_VAR_zone: asia-northeast2-a
- name: GCP => Add IAM policy binding to the Compute Engine images
if: matrix.cloud == 'gcp'
run: |
gcloud compute images add-iam-policy-binding ${PKR_VAR_image_base_name}-us-${PKR_VAR_suffix} --member='allAuthenticatedUsers' --role='roles/compute.imageUser'
gcloud compute images add-iam-policy-binding ${PKR_VAR_image_base_name}-eu-${PKR_VAR_suffix} --member='allAuthenticatedUsers' --role='roles/compute.imageUser'
gcloud compute images add-iam-policy-binding ${PKR_VAR_image_base_name}-asia-${PKR_VAR_suffix} --member='allAuthenticatedUsers' --role='roles/compute.imageUser'
- name: Upload manifest
uses: actions/upload-artifact@v3
with:
path: manifest_${{ matrix.cloud }}.json
name: manifest_${{ matrix.cloud }}.json
retention-days: 5
print-markdown:
needs: [build]
name: Print the AMI IDs in a markdown format
runs-on: ubuntu-latest
steps:
- name: Download Google Cloud manifest
uses: actions/download-artifact@v3
with:
name: manifest_gcp.json
path: manifest_gcp.json
# The GCP manifest file look like this:
# "builds": [
# {
# "name": "spacelift",
# "builder_type": "googlecompute",
# "build_time": 1700479054,
# "files": null,
# "artifact_id": "spacelift-worker-us-1700478848-305dsvij",
# "packer_run_uuid": "cdc82943-986b-5ab9-6ce1-9024ca0ebb6a",
# "custom_data": null
# },
# {
# "name": "spacelift",
# "builder_type": "googlecompute",
# "build_time": 1700479263,
# "files": null,
# "artifact_id": "spacelift-worker-eu-1700478848-305dsvij",
# "packer_run_uuid": "2440c9b1-a342-3606-2661-6e5389bdffc6",
# "custom_data": null
# }
# ]
- name: Print in a markdown format
uses: actions/github-script@v6
env:
RUN_NUMBER: ${{ github.run_number }}
with:
script: |
const fs = require("fs");
content = fs.readFileSync("./manifest_gcp.json", "utf8");
manifest = JSON.parse(content);
const gcpLinesToPrint = [];
manifest["builds"].forEach((build) => {
artifact = build["artifact_id"];
if (artifact.indexOf("-us-") > 0) {
gcpLinesToPrint.push(` - United States | \`${artifact}\``);
}
if (artifact.indexOf("-eu-") > 0) {
gcpLinesToPrint.push(` - Europe | \`${artifact}\``);
}
if (artifact.indexOf("-asia-") > 0) {
gcpLinesToPrint.push(` - Asia | \`${artifact}\``);
}
});
console.log("## Azure");
console.log("");
console.log(`- Publisher | \`spaceliftinc1625499025476\`.`);
console.log(`- Offer | \`spacelift_worker\`.`);
console.log(`- SKU | \`ubuntu_20_04\`.`);
console.log(`- Version | \`1.0.${process.env.RUN_NUMBER}\`.`);
console.log("");
console.log("## Google Cloud Platform");
console.log("");
gcpLinesToPrint.forEach(line => console.log(line));