Skip to content

Commit 4a6fcda

Browse files
authored
Merge pull request #311 from JoinColony/feat/M1-proxy-colonies
feat: Proxy colonies M1
2 parents 01bad00 + a927a71 commit 4a6fcda

File tree

31 files changed

+5297
-10054
lines changed

31 files changed

+5297
-10054
lines changed
+220
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
name: Block ingestor proxy chain - Build and deploy
2+
3+
on:
4+
5+
workflow_dispatch: # Allows manual workflow trigger
6+
repository_dispatch: # Allows API workflow trigger
7+
types: [cdapp-block-ingestor-proxy-chain]
8+
9+
# push:
10+
# branches:
11+
# - master # Automatically deploy on commits to master
12+
# paths-ignore:
13+
# - '.github/**'
14+
# - '**.md'
15+
16+
concurrency:
17+
group: cdapp-block-ingestor-proxy-chain
18+
cancel-in-progress: true
19+
20+
# Set global env variables
21+
env:
22+
AWS_REGION: eu-west-2
23+
ECR_REPOSITORY: ${{ secrets.AWS_ACCOUNT_ID_QA }}.dkr.ecr.eu-west-2.amazonaws.com/cdapp/block-ingestor-proxy-chain
24+
COMMIT_HASH: ${{ github.event.client_payload.COMMIT_HASH != null && github.event.client_payload.COMMIT_HASH || github.sha }}
25+
26+
jobs:
27+
28+
# Build cdapp block-ingestor and push to AWS ECR
29+
buildAndPush:
30+
31+
runs-on: ubuntu-latest
32+
33+
steps:
34+
35+
- name: Echo Env Vars through Context
36+
run: |
37+
echo "$GITHUB_CONTEXT"
38+
39+
- name: Configure AWS credentials
40+
uses: aws-actions/configure-aws-credentials@v2
41+
with:
42+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_QA }}
43+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_QA }}
44+
aws-region: ${{ env.AWS_REGION }}
45+
46+
- name: Login to Amazon ECR
47+
id: login-ecr
48+
uses: aws-actions/amazon-ecr-login@v1
49+
50+
- name: Checkout
51+
uses: actions/checkout@v2
52+
with:
53+
fetch-depth: 0
54+
55+
- name: Checkout relevant branch
56+
run:
57+
git checkout ${{ github.event.client_payload.COMMIT_HASH != null && github.event.client_payload.COMMIT_HASH || github.sha }}
58+
59+
- name: Set up Docker Buildx
60+
uses: docker/setup-buildx-action@v2
61+
62+
- name: Build and push
63+
uses: docker/build-push-action@v3
64+
with:
65+
context: ${{ github.workspace }}
66+
file: ./Dockerfile
67+
push: true
68+
build-args: |
69+
BUILD_TARGET=proxy-chain
70+
tags: |
71+
${{ env.ECR_REPOSITORY }}:run-${{ github.run_number }}
72+
${{ env.ECR_REPOSITORY }}:${{ env.COMMIT_HASH }}
73+
74+
- uses: sarisia/actions-status-discord@c193626e5ce172002b8161e116aa897de7ab5383
75+
if: always()
76+
with:
77+
webhook: ${{ secrets.DISCORD_WEBHOOK }}
78+
title: "Build and push cdapp block-ingestor"
79+
80+
# Deploy cdapp block-ingestor to QA environment
81+
deployQA:
82+
83+
needs: buildAndPush
84+
85+
runs-on: ubuntu-latest
86+
87+
env:
88+
NAMESPACE: cdapp
89+
CLUSTER_NAME: qa-cluster
90+
ENVIRONMENT_TAG: qa
91+
REPOSITORY_NAME: cdapp/block-ingestor-proxy-chain
92+
93+
steps:
94+
95+
- name: Configure AWS credentials for ECR
96+
uses: aws-actions/configure-aws-credentials@v2
97+
with:
98+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_QA }}
99+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_QA }}
100+
aws-region: ${{ env.AWS_REGION }}
101+
102+
- name: Login to Amazon ECR
103+
id: login-ecr
104+
uses: aws-actions/amazon-ecr-login@v1
105+
106+
- name: Echo current image and tag new image
107+
run: |
108+
echo -e "Getting image info...\n"
109+
110+
echo -e "###### Current image being used ######\n"
111+
SHA256=$(aws ecr batch-get-image --repository-name ${{ env.REPOSITORY_NAME }} --image-ids imageTag=${{ env.ENVIRONMENT_TAG }} --output json | jq '.images[].imageId.imageDigest')
112+
aws ecr batch-get-image --repository-name ${{ env.REPOSITORY_NAME }} --image-ids imageDigest=$SHA256 --output json | jq '.images[].imageId'
113+
114+
echo -e "\n###### Tagging new image with environment tag ######"
115+
MANIFEST=$(aws ecr batch-get-image --repository-name ${{ env.REPOSITORY_NAME }} --image-ids imageTag=${{ env.COMMIT_HASH }} --output json | jq --raw-output --join-output '.images[0].imageManifest')
116+
aws ecr put-image --repository-name ${{ env.REPOSITORY_NAME }} --image-tag ${{ env.ENVIRONMENT_TAG }} --image-manifest "$MANIFEST"
117+
118+
echo -e "\n###### New image being used ######\n"
119+
SHA256=$(aws ecr batch-get-image --repository-name ${{ env.REPOSITORY_NAME }} --image-ids imageTag=${{ env.ENVIRONMENT_TAG }} --output json | jq '.images[].imageId.imageDigest')
120+
aws ecr batch-get-image --repository-name ${{ env.REPOSITORY_NAME }} --image-ids imageDigest=$SHA256 --output json | jq '.images[].imageId'
121+
122+
- name: Configure AWS credentials for EKS
123+
uses: aws-actions/configure-aws-credentials@v2
124+
with:
125+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_QA }}
126+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_QA }}
127+
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID_QA }}:role/eks-admin
128+
role-session-name: github-cicd
129+
role-duration-seconds: 1200
130+
aws-region: ${{ env.AWS_REGION }}
131+
132+
- name: Configure AWS EKS
133+
run: |
134+
aws eks --region ${{ env.AWS_REGION }} update-kubeconfig --name ${{ env.CLUSTER_NAME }}
135+
136+
- name: Deploy to Kubernetes cluster
137+
run: |
138+
kubectl rollout restart deployment/cdapp-block-ingestor-${{ env.ENVIRONMENT_TAG }}-polygon-amoy -n ${{ env.NAMESPACE }}
139+
140+
- name: Validate Kubernetes deployment
141+
run: |
142+
kubectl rollout status deployment/cdapp-block-ingestor-${{ env.ENVIRONMENT_TAG }}-polygon-amoy -n ${{ env.NAMESPACE }}
143+
144+
- uses: sarisia/actions-status-discord@c193626e5ce172002b8161e116aa897de7ab5383
145+
if: always()
146+
with:
147+
webhook: ${{ secrets.DISCORD_WEBHOOK }}
148+
title: "Deploy cdapp block-ingestor to ${{ env.ENVIRONMENT_TAG }}-polygon-amoy"
149+
150+
# Deploy cdapp block-ingestor to Prod environment
151+
deployProd:
152+
153+
needs: deployQA
154+
155+
environment: prod
156+
157+
runs-on: ubuntu-latest
158+
159+
env:
160+
NAMESPACE: cdapp
161+
CLUSTER_NAME: prod-cluster
162+
ENVIRONMENT_TAG: prod
163+
REPOSITORY_NAME: cdapp/block-ingestor-proxy-chain
164+
165+
steps:
166+
167+
- name: Configure AWS credentials for ECR
168+
uses: aws-actions/configure-aws-credentials@v2
169+
with:
170+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_QA }}
171+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_QA }}
172+
aws-region: ${{ env.AWS_REGION }}
173+
174+
- name: Login to Amazon ECR
175+
id: login-ecr
176+
uses: aws-actions/amazon-ecr-login@v1
177+
178+
- name: Echo current image and tag new image
179+
run: |
180+
echo -e "Getting image info...\n"
181+
182+
echo -e "###### Current image being used ######\n"
183+
SHA256=$(aws ecr batch-get-image --repository-name ${{ env.REPOSITORY_NAME }} --image-ids imageTag=${{ env.ENVIRONMENT_TAG }} --output json | jq '.images[].imageId.imageDigest')
184+
aws ecr batch-get-image --repository-name ${{ env.REPOSITORY_NAME }} --image-ids imageDigest=$SHA256 --output json | jq '.images[].imageId'
185+
186+
echo -e "\n###### Tagging new image with environment tag ######"
187+
MANIFEST=$(aws ecr batch-get-image --repository-name ${{ env.REPOSITORY_NAME }} --image-ids imageTag=${{ env.COMMIT_HASH }} --output json | jq --raw-output --join-output '.images[0].imageManifest')
188+
aws ecr put-image --repository-name ${{ env.REPOSITORY_NAME }} --image-tag ${{ env.ENVIRONMENT_TAG }} --image-manifest "$MANIFEST"
189+
190+
echo -e "\n###### New image being used ######\n"
191+
SHA256=$(aws ecr batch-get-image --repository-name ${{ env.REPOSITORY_NAME }} --image-ids imageTag=${{ env.ENVIRONMENT_TAG }} --output json | jq '.images[].imageId.imageDigest')
192+
aws ecr batch-get-image --repository-name ${{ env.REPOSITORY_NAME }} --image-ids imageDigest=$SHA256 --output json | jq '.images[].imageId'
193+
194+
- name: Configure AWS credentials for EKS
195+
uses: aws-actions/configure-aws-credentials@v2
196+
with:
197+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_PROD }}
198+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_PROD }}
199+
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID_PROD }}:role/eks-admin
200+
role-session-name: github-cicd
201+
role-duration-seconds: 1200
202+
aws-region: ${{ env.AWS_REGION }}
203+
204+
- name: Configure AWS EKS
205+
run: |
206+
aws eks --region ${{ env.AWS_REGION }} update-kubeconfig --name ${{ env.CLUSTER_NAME }}
207+
208+
- name: Deploy to Kubernetes cluster
209+
run: |
210+
kubectl rollout restart deployment/cdapp-block-ingestor-${{ env.ENVIRONMENT_TAG }}-polygon-mainnet -n ${{ env.NAMESPACE }}
211+
212+
- name: Validate Kubernetes deployment
213+
run: |
214+
kubectl rollout status deployment/cdapp-block-ingestor-${{ env.ENVIRONMENT_TAG }}-polygon-mainnet -n ${{ env.NAMESPACE }}
215+
216+
- uses: sarisia/actions-status-discord@c193626e5ce172002b8161e116aa897de7ab5383
217+
if: always()
218+
with:
219+
webhook: ${{ secrets.DISCORD_WEBHOOK }}
220+
title: "Deploy cdapp block-ingestor to ${{ env.ENVIRONMENT_TAG }}-polygon-mainnet"

.github/workflows/build-deploy.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build and deploy cdapp block-ingestor docker image
1+
name: Block ingestor main chain - Build and deploy
22

33
on:
44

@@ -65,6 +65,8 @@ jobs:
6565
context: ${{ github.workspace }}
6666
file: ./Dockerfile
6767
push: true
68+
build-args: |
69+
BUILD_TARGET=main-chain
6870
tags: |
6971
${{ env.ECR_REPOSITORY }}:run-${{ github.run_number }}
7072
${{ env.ECR_REPOSITORY }}:${{ env.COMMIT_HASH }}

Dockerfile

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Declare the build argument at the top
2+
ARG BUILD_TARGET=main-chain
3+
14
FROM node:20-slim AS base
25
ENV PNPM_HOME="/pnpm"
36
ENV PATH="$PNPM_HOME:$PATH"
@@ -25,3 +28,16 @@ COPY --from=build /workspace /workspace
2528
WORKDIR /workspace/apps/proxy-chain
2629
CMD ["pnpm", "--filter", "@joincolony/proxy-chain", "prod"]
2730

31+
# Final stage that will be used
32+
FROM ${BUILD_TARGET} AS final
33+
34+
# Re-declare the build argument in the final stage
35+
ARG BUILD_TARGET
36+
37+
# Add labels and echo build info
38+
LABEL build_type=${BUILD_TARGET}
39+
RUN echo "🏗️ Building ${BUILD_TARGET} version of block-ingestor" && \
40+
echo "📦 Final build target: ${BUILD_TARGET}"
41+
42+
# Keep existing CMD from the selected stage
43+

apps/main-chain/src/eventListeners/colony.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,6 @@ export const setupListenersForColonies = async (): Promise<void> => {
115115
ContractEventsSignatures.ReputationMiningCycleComplete,
116116
handleReputationMiningCycleComplete,
117117
);
118-
addProxyColoniesEventListener(
119-
ContractEventsSignatures.ProxyColonyRequested,
120-
handleProxyColonyRequested,
121-
);
122118
};
123119

124120
export const setupListenersForColony = (
@@ -175,6 +171,12 @@ export const setupListenersForColony = (
175171
),
176172
);
177173

174+
addProxyColoniesEventListener(
175+
ContractEventsSignatures.ProxyColonyRequested,
176+
colonyAddress,
177+
handleProxyColonyRequested,
178+
);
179+
178180
/*
179181
* @NOTE Setup both token event listners
180182
*

apps/main-chain/src/eventListeners/proxyColonies.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import eventManager from '~eventManager';
99

1010
export const addProxyColoniesEventListener = (
1111
eventSignature: ContractEventsSignatures,
12+
colonyAddress: string,
1213
handler: EventHandler,
1314
): void =>
1415
eventManager.addEventListener({
1516
type: EventListenerType.ProxyColonies,
1617
eventSignature,
1718
topics: [utils.id(eventSignature)],
18-
address: process.env.CHAIN_NETWORK_CONTRACT ?? '',
19+
address: colonyAddress,
20+
colonyAddress,
1921
handler,
2022
});

apps/main-chain/src/handlers/colonies/colonyAdded.ts

-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ import { getColonyContributorId } from '~utils/contributors';
1919
import { tryFetchGraphqlQuery } from '~utils/graphql';
2020
import { createUniqueColony } from './helpers/createUniqueColony';
2121
import { output } from '@joincolony/utils';
22-
import rpcProvider from '~provider';
23-
import networkClient from '~networkClient';
24-
import { getTransactionSignerAddress } from '~utils/transactions';
2522
import { getColonyCreationSalt } from './helpers/validateCreationSalt';
2623

2724
export default async (event: ContractEvent): Promise<void> => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { Id } from '@colony/colony-js';
2+
import {
3+
ColonyActionType,
4+
GetProxyColonyDocument,
5+
GetProxyColonyQuery,
6+
GetProxyColonyQueryVariables,
7+
UpdateProxyColonyDocument,
8+
UpdateProxyColonyMutation,
9+
UpdateProxyColonyMutationVariables,
10+
} from '@joincolony/graphql';
11+
import { ContractEvent } from '@joincolony/blocks';
12+
import {
13+
DisableProxyColonyOperation,
14+
getColonyFromDB,
15+
getDomainDatabaseId,
16+
writeActionFromEvent,
17+
} from '~utils';
18+
import amplifyClient from '~amplifyClient';
19+
20+
export const handleDisableProxyColony = async (
21+
event: ContractEvent,
22+
operation: DisableProxyColonyOperation,
23+
): Promise<void> => {
24+
const { contractAddress: colonyAddress } = event;
25+
const { agent: initiatorAddress } = event.args;
26+
27+
const foreignChainId = operation.payload[0];
28+
29+
const colony = await getColonyFromDB(colonyAddress);
30+
if (!colony) {
31+
return;
32+
}
33+
34+
const proxyColonyId = `${colonyAddress}_${foreignChainId}`;
35+
36+
const item = await amplifyClient.query<
37+
GetProxyColonyQuery,
38+
GetProxyColonyQueryVariables
39+
>(GetProxyColonyDocument, {
40+
id: proxyColonyId,
41+
});
42+
43+
// If the proxy colony is already disabled, we early-return
44+
if (!item?.data?.getProxyColony?.isActive) {
45+
return;
46+
}
47+
48+
await amplifyClient.mutate<
49+
UpdateProxyColonyMutation,
50+
UpdateProxyColonyMutationVariables
51+
>(UpdateProxyColonyDocument, {
52+
input: {
53+
id: proxyColonyId,
54+
isActive: false,
55+
},
56+
});
57+
58+
await writeActionFromEvent(event, colonyAddress, {
59+
type: ColonyActionType.RemoveProxyColony,
60+
initiatorAddress,
61+
targetChainId: Number(foreignChainId),
62+
multiChainInfo: {
63+
completed: true,
64+
},
65+
fromDomainId: getDomainDatabaseId(colonyAddress, Id.RootDomain),
66+
});
67+
};

0 commit comments

Comments
 (0)