diff --git a/.github/workflows/build-gateway.yml b/.github/workflows/build-gateway.yml new file mode 100644 index 000000000..0bfe8bb47 --- /dev/null +++ b/.github/workflows/build-gateway.yml @@ -0,0 +1,99 @@ +name: build-gateway-images + +on: + workflow_dispatch: + + repository_dispatch: + types: + - webhook + + push: + paths: + - "apps/**/graph/*.graphqls" + - "apps/**/graph/struct-to-graphql/*.graphqls" + - "apps/gateway/**" + - ".github/workflows/build-gateway.yml" + +permissions: + contents: read + packages: write + +jobs: + docker-builds: + runs-on: ubuntu-latest + name: Deploy to Docker Image + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Install Task + uses: arduino/setup-task@v1 + with: + version: 3.x + repo-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Install Rover + run: | + curl -sSL https://rover.apollo.dev/nix/v0.23.0-rc.3 | sh + + # Add Rover to the $GITHUB_PATH so it can be used in another step + # https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#adding-a-system-path + echo "$HOME/.rover/bin" >> $GITHUB_PATH + + - name: Generate Schema + run: | + cd apps/gateway + task generate-schema + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Log in to the Container registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Create Image Tag from branch name + if: startsWith(github.ref, 'refs/heads/release') + run: | + set +e + IMAGE_TAG=$(echo ${GITHUB_REF#refs/heads/} | sed 's/release-//g') + echo "$IMAGE_TAG" | grep -i '\-nightly$' + if [ $? -ne 0 ]; then + IMAGE_TAG="$IMAGE_TAG-nightly" + fi + set -e + + echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV + echo "OVERRIDE_PUSHED_IMAGE=true" >> $GITHUB_ENV + + - name: Create Image Tag from tag + if: startsWith(github.ref, 'refs/tags/') + run: | + IMAGE_TAG=$(echo ${GITHUB_REF#refs/tags/}) + + echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV + echo "OVERRIDE_PUSHED_IMAGE=false" >> $GITHUB_ENV + + - name: Build & Push Image + if: startsWith(github.ref, 'refs/heads/release') || startsWith(github.ref, 'refs/tags/') + run: | + set +e + image_name="ghcr.io/${{ github.repository }}/gateway" + + docker manifest inspect $image_name:$IMAGE_TAG + exit_status=$? + if [ $exit_status -eq 0 ]; then + [ "$OVERRIDE_PUSHED_IMAGE" = "false" ] && echo "image ($image_name:$IMAGE_TAG) already exists, and override image is disable, exiting" && exit 0 + echo "image exists, but override pushed image is set to true. proceeding with building image" + fi + + set -e + + cd apps/gateway + task container:build-and-push Image=$image_name:$IMAGE_TAG \ No newline at end of file diff --git a/apps/gateway/.dockerignore b/apps/gateway/.dockerignore index 7aeeaa2af..4004ba20d 100644 --- a/apps/gateway/.dockerignore +++ b/apps/gateway/.dockerignore @@ -2,3 +2,4 @@ !package.json !src !pnpm-lock.yaml +!prod-schema.graphql diff --git a/apps/gateway/.gitignore b/apps/gateway/.gitignore new file mode 100644 index 000000000..4f42110ea --- /dev/null +++ b/apps/gateway/.gitignore @@ -0,0 +1,2 @@ +schemas +prod-schema.graphql \ No newline at end of file diff --git a/apps/gateway/Taskfile.yml b/apps/gateway/Taskfile.yml index 531927b91..10e3b549e 100644 --- a/apps/gateway/Taskfile.yml +++ b/apps/gateway/Taskfile.yml @@ -4,6 +4,9 @@ vars: app: gateway tasks: + generate-schema: + cmds: + - bash ./generate-schema.sh dev: dotenv: - "./.secrets/env" @@ -11,6 +14,14 @@ tasks: cmds: - npm start + local:build: + preconditions: + - sh: '[[ -n "{{.Image}}" ]]' + msg: "var Image must have a value, of format 'image_repository:image_tag'" + cmds: + - task generate-schema + - docker buildx build -f ./Containerfile -t {{.Image}} . + container:build-and-push: preconditions: - sh: '[[ -n "{{.Image}}" ]]' diff --git a/apps/gateway/generate-schema.sh b/apps/gateway/generate-schema.sh new file mode 100644 index 000000000..d7f4850fc --- /dev/null +++ b/apps/gateway/generate-schema.sh @@ -0,0 +1,20 @@ +#! /usr/bin/env bash + +mkdir -p schemas + +cat ../accounts/internal/app/graph/*.graphqls > ./schemas/accounts-api.schema +cat ../accounts/internal/app/graph/struct-to-graphql/*.graphqls >> ./schemas/accounts-api.schema + +cat ../console/internal/app/graph/*.graphqls > ./schemas/console-api.schema +cat ../console/internal/app/graph/struct-to-graphql/*.graphqls >> ./schemas/console-api.schema + +cat ../container-registry/internal/app/graph/*.graphqls > ./schemas/container-registry-api.schema +cat ../container-registry/internal/app/graph/struct-to-graphql/*.graphqls >> ./schemas/container-registry-api.schema + +cat ../infra/internal/app/graph/*.graphqls > ./schemas/infra-api.schema +cat ../infra/internal/app/graph/struct-to-graphql/*.graphqls >> ./schemas/infra-api.schema + +cat ../auth/internal/app/graph/*.graphqls > ./schemas/auth-api.schema +cat ../message-office/internal/app/graph/*.graphqls > ./schemas/message-office-api.schema + +rover supergraph compose --config ./supergraph.yml --output prod-schema.graphql --elv2-license accept diff --git a/apps/gateway/src/index.js b/apps/gateway/src/index.js index ff2dc747c..c909084a8 100644 --- a/apps/gateway/src/index.js +++ b/apps/gateway/src/index.js @@ -12,7 +12,7 @@ const useEnv = (key) => { return v }; -const cfgMap = yaml.load(await fs.readFile(useEnv("SUPERGRAPH_CONFIG"), 'utf8')); + class CustomDataSource extends RemoteGraphQLDataSource { // eslint-disable-next-line class-methods-use-this @@ -37,10 +37,10 @@ class CustomDataSource extends RemoteGraphQLDataSource { } } +const superGraphSchema = await fs.readFile("./prod-schema.graphql"); + const gateway = new ApolloGateway({ - supergraphSdl: new IntrospectAndCompose({ - subgraphs: cfgMap.serviceList, - }), + supergraphSdl: superGraphSchema.toString(), buildService({ name, url }) { return new CustomDataSource({ name, url }); }, diff --git a/apps/gateway/supergraph.yml b/apps/gateway/supergraph.yml new file mode 100644 index 000000000..2b7bc25de --- /dev/null +++ b/apps/gateway/supergraph.yml @@ -0,0 +1,22 @@ +federation_version: =2.3.2 +subgraphs: + auth-api: + routing_url: http://auth-api + schema: + file: ./schemas/auth-api.schema + account-api: + routing_url: https://accounts-api + schema: + file: ./schemas/accounts-api.schema + container-registry-api: + routing_url: https://container-registry-api + schema: + file: ./schemas/container-registry-api.schema + console-api: + routing_url: https://console-api + schema: + file: ./schemas/console-api.schema + infra-api: + routing_url: https://infra-api + schema: + file: ./schemas/infra-api.schema \ No newline at end of file