name: (CD) Publish docker + s3 on: workflow_call: inputs: projects: required: true type: string jobs: publish-dist: strategy: fail-fast: false matrix: app: ${{ fromJSON(inputs.projects) }} name: ${{ matrix.app }} runs-on: ubuntu-22.04 timeout-minutes: 25 steps: - name: Check out code uses: actions/checkout@v3 with: ref: ${{ github.event.pull_request.head.sha || github.sha }} - name: Set up QEMU id: quemu uses: docker/setup-qemu-action@v2 - name: Available platforms run: echo ${{ steps.qemu.outputs.platforms }} - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - name: Log in to the Container registry (ghcr) if: ${{ github.event_name == 'pull_request' || github.event_name == 'pull_request_target' }} uses: docker/login-action@v2 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Log in to the Container registry (docker hub) uses: docker/login-action@v2 if: ${{ matrix.app == 'trading' && github.event_name == 'push' && ( endsWith(github.ref, 'main') || endsWith(github.ref, 'release/testnet') ) }} with: # registry: registry.hub.docker.com username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Setup node uses: actions/setup-node@v3 with: node-version-file: '.nvmrc' # https://stackoverflow.com/questions/61010294/how-to-cache-yarn-packages-in-github-actions cache: yarn - name: Cache node modules uses: actions/cache@v3 with: path: node_modules key: ${{ runner.os }}-cache-node-modules-${{ hashFiles('yarn.lock') }} # https://docs.github.com/en/actions/learn-github-actions/contexts - name: Define dist variables if: ${{ github.event_name == 'push' }} run: | envName='' domain="vega.rocks" bucketName='' if [[ "${{ github.ref }}" =~ .*release/.* ]]; then envName="$(echo ${{ github.ref }} | rev | cut -d '/' -f 1 | rev)" elif [[ "${{ github.ref }}" =~ .*develop$ ]]; then envName="stagnet1" if [[ "${{ matrix.app }}" = "multisig-signer" ]]; then envName="mainnet" bucketName="tools.vega.xyz" fi if [[ "${{ matrix.app }}" = "static" ]]; then envName="mainnet" bucketName="static.vega.xyz" fi if [[ "${{ matrix.app }}" = "ui-toolkit" ]]; then envName="mainnet" bucketName="ui.vega.rocks" fi elif [[ "${{ github.ref }}" =~ .*main$ ]]; then envName="mainnet" fi if [[ "${envName}" = "mainnet" ]]; then domain="vega.xyz" if [[ -z "${bucketName}" ]]; then bucketName="${{ matrix.app }}.${domain}" fi elif [[ "${envName}" = "testnet" ]]; then domain="fairground.wtf" if [[ -z "${bucketName}" ]]; then bucketName="${{ matrix.app }}.${domain}" fi fi if [[ -z "${bucketName}" ]]; then bucketName="${{ matrix.app }}.${envName}.${domain}" fi echo "bucket name: ${bucketName}" echo "env name: ${envName}" echo BUCKET_NAME=${bucketName} >> $GITHUB_ENV echo ENV_NAME=${envName} >> $GITHUB_ENV - name: Build local dist run: | flags="" if [[ ! -z "${{ env.ENV_NAME }}" ]]; then flags="--env=${{ env.ENV_NAME }}" fi if [ "${{ matrix.app }}" = "trading" ]; then yarn nx export trading $flags || (yarn install && yarn nx export trading $flags) DIST_LOCATION=dist/apps/trading/exported elif [ "${{ matrix.app }}" = "ui-toolkit" ]; then NODE_ENV=production yarn nx run ui-toolkit:build-storybook DIST_LOCATION=dist/storybook/ui-toolkit else yarn nx build ${{ matrix.app }} $flags || (yarn install && yarn nx build ${{ matrix.app }} $flags) DIST_LOCATION=dist/apps/${{ matrix.app }} fi mv $DIST_LOCATION dist-result tree dist-result - name: Build and export to local Docker id: docker_build uses: docker/build-push-action@v3 with: context: . file: docker/node-outside-docker.Dockerfile load: true build-args: | APP=${{ matrix.app }} ENV_NAME=${{ env.ENV_NAME }} tags: | ghcr.io/vegaprotocol/frontend/${{ matrix.app }}:local - name: Image digest if: ${{ github.event_name == 'pull_request' || github.event_name == 'pull_request_target' }} run: echo ${{ steps.docker_build.outputs.digest }} - name: Sanity check docker image run: | echo "Check ipfs-hash" docker run --rm ghcr.io/vegaprotocol/frontend/${{ matrix.app }}:local cat /ipfs-hash docker run --rm ghcr.io/vegaprotocol/frontend/${{ matrix.app }}:local cat /ipfs-hash > ${{ matrix.app }}-ipfs-hash echo "List html directory" docker run --rm ghcr.io/vegaprotocol/frontend/${{ matrix.app }}:local sh -c 'apk add --update tree; tree /usr/share/nginx/html' - name: Publish dist as docker image (ghcr) uses: docker/build-push-action@v3 continue-on-error: true id: ghcr-push if: ${{ github.event_name == 'pull_request' || github.event_name == 'pull_request_target' }} with: context: . file: docker/node-outside-docker.Dockerfile push: true build-args: | APP=${{ matrix.app }} ENV_NAME=${{ env.ENV_NAME }} tags: | ghcr.io/vegaprotocol/frontend/${{ matrix.app }}:${{ github.event.pull_request.head.sha || github.sha }} - name: Publish dist as docker image (docker hub) uses: docker/build-push-action@v3 continue-on-error: true id: dockerhub-push if: ${{ matrix.app == 'trading' && github.event_name == 'push' && ( endsWith(github.ref, 'main') || endsWith(github.ref, 'release/testnet') ) }} with: context: . file: docker/node-outside-docker.Dockerfile push: true build-args: | APP=${{ matrix.app }} ENV_NAME=${{ env.ENV_NAME }} tags: | vegaprotocol/${{ matrix.app }}:${{ github.sha }} vegaprotocol/${{ matrix.app }}:${{ endsWith(github.ref, 'main') && 'mainnet' || endsWith(github.ref, 'release/testnet') && 'testnet' || '' }} - name: Publish dist as docker image (ghcr - retry) uses: docker/build-push-action@v3 if: ${{ steps.ghcr-push.outcome == 'failure' }} with: context: . file: docker/node-outside-docker.Dockerfile push: true build-args: | APP=${{ matrix.app }} ENV_NAME=${{ env.ENV_NAME }} tags: | ghcr.io/vegaprotocol/frontend/${{ matrix.app }}:${{ github.event.pull_request.head.sha || github.sha }} - name: Publish dist as docker image (docker hub - retry) uses: docker/build-push-action@v3 if: ${{ steps.dockerhub-push.outcome == 'failure' }} with: context: . file: docker/node-outside-docker.Dockerfile push: true build-args: | APP=${{ matrix.app }} ENV_NAME=${{ env.ENV_NAME }} tags: | vegaprotocol/${{ matrix.app }}:${{ github.sha }} vegaprotocol/${{ matrix.app }}:${{ endsWith(github.ref, 'main') && 'mainnet' || endsWith(github.ref, 'release/testnet') && 'testnet' || '' }} # bucket creation in github.com/vegaprotocol/terraform//frontend - name: Publish dist to s3 uses: jakejarvis/s3-sync-action@master # s3 releases are not happening for trading on mainnet - it's IPFS if: ${{ github.event_name == 'push' && ( matrix.app != 'trading' || (matrix.app == 'trading' && !( endsWith(github.ref, 'main') || endsWith(github.ref, 'release/testnet') ) ) ) }} with: args: --acl private --follow-symlinks --delete env: AWS_S3_BUCKET: ${{ env.BUCKET_NAME }} AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} AWS_REGION: 'eu-west-1' SOURCE_DIR: 'dist-result' - name: Install aws CLI if: ${{ github.event_name == 'push' && ( matrix.app != 'trading' || (matrix.app == 'trading' && !( endsWith(github.ref, 'main') || endsWith(github.ref, 'release/testnet') ) ) ) }} uses: unfor19/install-aws-cli-action@master - name: Perform cache invalidation if: ${{ github.event_name == 'push' && ( matrix.app != 'trading' || (matrix.app == 'trading' && !( endsWith(github.ref, 'main') || endsWith(github.ref, 'release/testnet') ) ) ) }} env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} AWS_REGION: 'eu-west-1' run: | id=$(aws cloudfront list-distributions | jq -Mc '.DistributionList.Items | .[] | select(.DefaultCacheBehavior.TargetOriginId == "${{ env.BUCKET_NAME }}") | .Id') aws cloudfront create-invalidation --distribution-id "${id}" --paths "/*" - name: Add preview label uses: actions-ecosystem/action-add-labels@v1 if: ${{ github.event_name == 'pull_request' || github.event_name == 'pull_request_target' }} with: labels: ${{ matrix.app }}-preview number: ${{ github.event.number }} - name: Trigger fleek deployment # release to ipfs happens only on mainnet (represented by main branch) for trading if: ${{ matrix.app == 'trading' && github.event_name == 'push' && ( endsWith(github.ref, 'main') || endsWith(github.ref, 'release/testnet') ) }} run: | if echo ${{ github.ref }} | grep -q main; then # display info about app curl --fail -H "Authorization: ${{ secrets.FLEEK_API_KEY }}" \ -H "Content-Type: application/json" \ -d '{"query": "query{getSiteById(siteId:\"f8f2e051-f18e-49e6-b876-0a39369dc0d8\"){id latestDeploy{id status}}}"}' \ https://api.fleek.co/graphql # trigger new deployment as base image is always set to vegaprotocol/trading:mainnet curl --fail -H "Authorization: ${{ secrets.FLEEK_API_KEY }}" \ -H "Content-Type: application/json" \ -d '{"query": "mutation{triggerDeploy(siteId:\"f8f2e051-f18e-49e6-b876-0a39369dc0d8\"){id status}}"}' \ https://api.fleek.co/graphql elif echo ${{ github.ref }} | grep -q release/testnet; then # display info about app curl --fail -H "Authorization: ${{ secrets.FLEEK_API_KEY }}" \ -H "Content-Type: application/json" \ -d '{"query": "query{getSiteById(siteId:\"79baaeca-1952-4ae7-a256-f668cfc1d68e\"){id latestDeploy{id status}}}"}' \ https://api.fleek.co/graphql # trigger new deployment as base image is always set to vegaprotocol/trading:mainnet curl --fail -H "Authorization: ${{ secrets.FLEEK_API_KEY }}" \ -H "Content-Type: application/json" \ -d '{"query": "mutation{triggerDeploy(siteId:\"79baaeca-1952-4ae7-a256-f668cfc1d68e\"){id status}}"}' \ https://api.fleek.co/graphql fi - name: Check out ipfs-redirect if: ${{ matrix.app == 'trading' && github.event_name == 'push' && ( endsWith(github.ref, 'main') || endsWith(github.ref, 'testnet') ) }} uses: actions/checkout@v3 with: repository: 'vegaprotocol/ipfs-redirect' path: 'ipfs-redirect' fetch-depth: '0' token: ${{ secrets.VEGA_CI_BOT_GITHUB_TOKEN }} - name: Update interstitial page to point to the new console if: ${{ matrix.app == 'trading' && github.event_name == 'push' && ( endsWith(github.ref, 'main') || endsWith(github.ref, 'testnet') ) }} env: GH_TOKEN: ${{ secrets.VEGA_CI_BOT_GITHUB_TOKEN }} run: | # set CID curl -L https://dist.ipfs.tech/kubo/v0.20.0/kubo_v0.20.0_linux-amd64.tar.gz -o kubo.tgz tar -xzf kubo.tgz export PATH="$PATH:$PWD/kubo" which ipfs new_hash=$(cat ${{ matrix.app }}-ipfs-hash) new_cid=$(ipfs cid format -v 1 -b base32 $new_hash) ( cd ipfs-redirect # configure git git status cat .git/config git config --global user.email "vega-ci-bot@vega.xyz" git config --global user.name "vega-ci-bot" # update CID files if echo ${{ github.ref }} | grep -q main; then echo $new_hash > cidv0-mainnet.txt echo $new_cid > cidv1-mainnet.txt git add cidv0-mainnet.txt cidv1-mainnet.txt elif echo ${{ github.ref }} | grep -q release/testnet; then echo $new_hash > cidv0-fairground.txt echo $new_cid > cidv1-fairground.txt git add cidv0-fairground.txt cidv1-fairground.txt fi # create commit commit_msg="Automated hash update from ${{ github.ref }}" git commit -m "$commit_msg" git push -u origin "main" )