diff --git a/.dockerignore b/.dockerignore index 0916c8ecc..6ba1a3dc0 100644 --- a/.dockerignore +++ b/.dockerignore @@ -3,3 +3,7 @@ apps/**/node_modules/* tmp/* .dockerignore dockerfiles +node_modules +.git +.github +.vscode diff --git a/.github/workflows/cypress-run.yml b/.github/workflows/cypress-run.yml index 4c5481b76..15f1f896e 100644 --- a/.github/workflows/cypress-run.yml +++ b/.github/workflows/cypress-run.yml @@ -1,3 +1,4 @@ +name: Cypress Run on: workflow_call: inputs: @@ -17,6 +18,7 @@ jobs: fail-fast: false matrix: project: ${{ fromJSON(inputs.projects) }} + name: ${{ matrix.project }} runs-on: self-hosted-runner timeout-minutes: 30 steps: diff --git a/.github/workflows/cypress-pr.yml b/.github/workflows/pr-trigger.yml similarity index 51% rename from .github/workflows/cypress-pr.yml rename to .github/workflows/pr-trigger.yml index dc37d3e3a..b31bd618d 100644 --- a/.github/workflows/cypress-pr.yml +++ b/.github/workflows/pr-trigger.yml @@ -1,4 +1,4 @@ -name: Cypress tests - PR +name: PR Validations on: push: @@ -20,14 +20,16 @@ jobs: with: fetch-depth: 0 + - name: Check node version + id: node-version + run: | + npmVersion=$(cat .nvmrc | head -n 1) + echo ::set-output name=npmVersion::${npmVersion} + - name: Setup node uses: actions/setup-node@v3 - - - name: Remove package.json & yarn.lock to avoid installing everything - run: rm package.json yarn.lock - - - name: Install nx - run: yarn add nx + with: + node-version: ${{ steps.node-version.outputs.npmVersion }} # Check SHAs - name: Derive appropriate SHAs for base and head for `nx affected` commands @@ -39,38 +41,51 @@ jobs: # See affected apps - name: See affected apps run: | + nx_version=$(cat package.json | grep '"nx"' | cut -d ':' -f 2 | tr -d '",[:space:]') + rm package.json yarn.lock + yarn add nx@$nx_version affected=$(yarn nx print-affected --base=${{ env.NX_BASE }} --head=${{ env.NX_HEAD }} --select=projects) echo -n "Affected projects: $affected" - projects="" - if [[ $affected == *"governance"* ]]; then projects+='"governance-e2e" '; fi - if [[ $affected == *"trading"* ]]; then projects+='"trading-e2e" '; fi - if [[ $affected == *"explorer"* ]]; then projects+='"explorer-e2e" '; fi - if [[ -z "$projects" ]]; then projects+='"governance-e2e" "trading-e2e" "explorer-e2e" '; fi - projects=${projects%?} - projects=[${projects// /,}] - echo PROJECTS=$projects >> $GITHUB_ENV + projects_e2e="" + if [[ $affected == *"governance"* ]]; then projects_e2e+='"governance-e2e" '; fi + if [[ $affected == *"trading"* ]]; then projects_e2e+='"trading-e2e" '; fi + if [[ $affected == *"explorer"* ]]; then projects_e2e+='"explorer-e2e" '; fi + if [[ -z "$projects_e2e" ]]; then projects_e2e+='"governance-e2e" "trading-e2e" "explorer-e2e" '; fi + projects_e2e=${projects_e2e%?} + projects_e2e=[${projects_e2e// /,}] + echo PROJECTS_E2E=$projects_e2e >> $GITHUB_ENV + echo PROJECTS=$(echo $projects_e2e | sed 's|-e2e||g') >> $GITHUB_ENV outputs: projects: ${{ env.PROJECTS }} + projects-e2e: ${{ env.PROJECTS_E2E }} - run: + run-cypress: needs: pr if: ${{ needs.pr.outputs.projects != '[]' }} uses: ./.github/workflows/cypress-run.yml secrets: inherit with: - projects: ${{ needs.pr.outputs.projects }} + projects: ${{ needs.pr.outputs.projects-e2e }} tags: '@smoke @regression' + run-docker-build: + needs: pr + if: ${{ needs.pr.outputs.projects != '[]' }} + uses: ./.github/workflows/publish-docker-containers.yml + secrets: inherit + with: + projects: ${{ needs.pr.outputs.projects }} + # Report single result at the end, to avoid mess with required checks in PR result: if: ${{ always() }} - needs: run + needs: run-cypress runs-on: ubuntu-latest name: Cypress result steps: - run: | - result="${{ needs.run.result }}" + result="${{ needs.run-cypress.result }}" if [[ $result == "success" || $result == "skipped" ]]; then exit 0 else diff --git a/.github/workflows/publish-docker-containers.yml b/.github/workflows/publish-docker-containers.yml index d1bed4b58..3797feaf6 100644 --- a/.github/workflows/publish-docker-containers.yml +++ b/.github/workflows/publish-docker-containers.yml @@ -1,16 +1,19 @@ -name: Publish docker containers +name: Docker build -'on': - pull_request: +on: + workflow_call: + inputs: + projects: + required: true + type: string jobs: master: strategy: fail-fast: false - # to be replaced by nix: https://github.com/vegaprotocol/frontend-monorepo/blob/develop/tools/ipfs-deploy.js#L106-L108 matrix: - app: ${{ fromJson('["explorer"]') }} - name: Build the ${{ matrix.app }} image + app: ${{ fromJSON(inputs.projects) }} + name: ${{ matrix.app }} runs-on: ubuntu-latest steps: - name: Check out code @@ -45,13 +48,10 @@ jobs: - name: Build and export to local Docker uses: docker/build-push-action@v3 with: - context: . - push: false - file: dockerfiles/Dockerfile + load: true build-args: | APP=${{ matrix.app }} NODE_VERSION=${{ steps.tags.outputs.npmVersion }} - load: true tags: | ghcr.io/vegaprotocol/frontend/${{ matrix.app }}:local @@ -75,9 +75,7 @@ jobs: id: docker_build uses: docker/build-push-action@v3 with: - context: . push: true - file: dockerfiles/Dockerfile build-args: | APP=${{ matrix.app }} NODE_VERSION=${{ steps.tags.outputs.npmVersion }} @@ -85,5 +83,12 @@ jobs: ghcr.io/vegaprotocol/frontend/${{ matrix.app }}:latest ghcr.io/vegaprotocol/frontend/${{ matrix.app }}:${{ steps.tags.outputs.version }} + - name: Add preview label + uses: actions-ecosystem/action-add-labels@v1 + if: ${{ github.event_name == 'pull_request' }} + with: + labels: ${{ matrix.app }}-preview + number: ${{ github.event.number }} + - name: Image digest run: echo ${{ steps.docker_build.outputs.digest }} diff --git a/dockerfiles/Dockerfile b/Dockerfile similarity index 83% rename from dockerfiles/Dockerfile rename to Dockerfile index 68be661e6..326e7958d 100644 --- a/dockerfiles/Dockerfile +++ b/Dockerfile @@ -4,13 +4,15 @@ FROM --platform=amd64 node:${NODE_VERSION}-alpine3.16 as build WORKDIR /app # Argument to allow building of different apps ARG APP -ENV PATH /app/node_modules/.bin:$PATH -COPY package.json ./ -COPY yarn.lock ./ +RUN apk add --update --no-cache \ + python3 \ + make \ + gcc \ + g++ COPY . ./ -RUN apk add python3 make gcc g++ RUN yarn --network-timeout 100000 --pure-lockfile -RUN yarn nx build ${APP} --network-timeout 100000 --pure-lockfile +# work around for different build process in trading +RUN sh ./docker-build.sh # Server environment # if this fails you need to docker pull nginx:1.23-alpine and pin new SHA @@ -25,7 +27,7 @@ CMD ["/entrypoint.sh"] # Copy dist WORKDIR /usr/share/nginx/html -COPY --from=build /app/dist/apps/${APP} /usr/share/nginx/html COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf +COPY --from=build /app/dist/apps/${APP} /usr/share/nginx/html COPY ./apps/${APP}/.env .env RUN ipfs init && echo "$(ipfs add -rQ .)" > ipfs-hash diff --git a/docker-build.sh b/docker-build.sh new file mode 100755 index 000000000..2a4772948 --- /dev/null +++ b/docker-build.sh @@ -0,0 +1,10 @@ +#!/bin/sh -eux +export PATH="/app/node_modules/.bin:$PATH" +if [ "${APP}" = "trading" ]; then + yarn nx export ${APP} --network-timeout 100000 --pure-lockfile + mv /app/dist/apps/trading/exported/ /app/tmp + rm -rf /app/dist/apps/trading + mv /app/tmp /app/dist/apps/trading +else + yarn nx build ${APP} --network-timeout 100000 --pure-lockfile +fi diff --git a/entrypoint.sh b/entrypoint.sh index c6b1bd5b1..d0923b6eb 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -7,6 +7,10 @@ mkdir -p $(dirname $env_file) rm -rf $env_file || echo "no file to delete" touch $env_file +env_vars_file=/usr/share/nginx/html/.env +sed -i '/^#/d' $env_vars_file # remove comment lines +sed -i '/^$/d' $env_vars_file # remove empty lines + # Add assignment echo "window._env_ = {" >> $env_file @@ -29,7 +33,7 @@ do if [ ! -z "$varname" ]; then echo " $varname: \"$value\"," >> $env_file fi -done < /usr/share/nginx/html/.env +done < $env_vars_file echo "}" >> $env_file