-
Notifications
You must be signed in to change notification settings - Fork 2
169 lines (145 loc) · 4.77 KB
/
exporter.yaml
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
name: exporter
on:
push:
branches:
- main
pull_request:
branches:
- "*"
env:
CARGO_TERM_COLOR: always
SQLX_VERSION: 0.8.2
SQLX_FEATURES: postgres
jobs:
fmt-lint-test:
name: format, lint and test
runs-on: ubuntu-latest
services:
postgres:
image: postgres:14
env:
POSTGRES_USER: ${{ secrets.CI_DATABASE_USERNAME || 'postgres' }}
POSTGRES_PASSWORD: ${{ secrets.CI_DATABASE_PASSWORD || github.sha }}
POSTGRES_DB: ${{ secrets.CI_DATABASE_NAME || 'test-db' }}
ports:
- 5432:5432
env:
POSTGRES_USER: ${{ secrets.CI_DATABASE_USERNAME || 'postgres' }}
POSTGRES_PASSWORD: ${{ secrets.CI_DATABASE_PASSWORD || github.sha }}
POSTGRES_DB: ${{ secrets.CI_DATABASE_NAME || 'test-db' }}
POSTGRES_HOST: localhost
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup .env
run: ./scripts/create_env.sh
- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
- uses: Swatinem/rust-cache@v2
with:
cache-targets: true
- name: Check Format
run: cargo fmt --all -- --check
- uses: taiki-e/install-action@nextest
- name: Cache sqlx-cli
uses: actions/cache@v4
id: cache-sqlx
with:
path: |
~/.cargo/bin/sqlx
~/.cargo/bin/cargo-sqlx
key: ${{ runner.os }}-sqlx-${{ env.SQLX_VERSION }}
- name: Install sqlx-cli
if: steps.cache-sqlx.outputs.cache-hit == false
run: |
cargo install sqlx-cli \
--force \
--version=${{ env.SQLX_VERSION }} \
--features ${{ env.SQLX_FEATURES }} \
--no-default-features \
--locked
- name: Migrate database
run: |
sudo apt-get install libpq-dev -y
./scripts/init_db.sh
- name: Lint
if: github.actor != 'dependabot[bot]'
run: |
cargo clippy -- -D warnings
- name: Check sqlx offline query
if: github.actor != 'dependabot[bot]'
run: |
cd apps/exporter
cargo sqlx prepare --check -- --bin swu-exporter
- name: Run tests
run: |
cd apps/exporter
cargo nextest run
build-deploy:
name: build and deploy
runs-on: ubuntu-latest
needs:
- fmt-lint-test
environment: production
if: github.ref == 'refs/heads/main'
permissions:
contents: read
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Exporter Google Sheet Credentials
env:
EXPORTER_SHEETS_CREDENTIALS: ${{secrets.EXPORTER_SHEETS_CREDENTIALS}}
run: |
mkdir credentials
echo $EXPORTER_SHEETS_CREDENTIALS > credentials/exporter.json
- name: Login
id: auth
uses: google-github-actions/auth@v2
with:
token_format: access_token
workload_identity_provider: ${{ secrets.GCP_IDENTITY_PROVIDER }}
service_account: ${{ secrets.GCP_DEPLOY_SERVICE_ACCOUNT }}
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v2
with:
install_components: beta
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker Image Metadata
id: meta
uses: docker/metadata-action@v4
with:
images: |
${{ vars.CLOUD_RUN_EXPORTER_IMAGE }}
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=sha
- name: Login to GAR
uses: docker/login-action@v3
with:
registry: us-central1-docker.pkg.dev
username: oauth2accesstoken
password: ${{ steps.auth.outputs.access_token }}
- name: Build production image
uses: docker/build-push-action@v5
with:
context: .
provenance: false
file: apps/exporter/Dockerfile
target: runtime
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=${{ vars.CLOUD_RUN_EXPORTER_IMAGE }}:buildcache
cache-to: type=registry,ref=${{ vars.CLOUD_RUN_EXPORTER_IMAGE }}:buildcache,mode=max
- name: Replace variables in exporter-job.yaml
run: |
sed -i s#%EXPORTER_IMAGE%#${{ fromJSON(steps.meta.outputs.json).tags[1] }}#g exporter-job.yaml
sed -i s#%SERVICE_ACCOUNT%#${{ secrets.CLOUD_RUN_SERVICE_ACCOUNT }}#g exporter-job.yaml
- name: Update Cloud Run Job
run: |
gcloud run jobs replace exporter-job.yaml --region=us-central1 --project=${{ secrets.GCP_PROJECT_ID }}