diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..c71414b --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,69 @@ +name: build-test + +on: + push: + paths: + - "**/Dockerfile" + - "**/docker-entrypoint.sh" + - genMatrix.js + - ".github/workflows/build-test.yml" + + pull_request: + paths: + - "**/Dockerfile" + - "**/docker-entrypoint.sh" + - genMatrix.js + - ".github/workflows/build-test.yml" + +jobs: + gen-matrix: + name: generate-matrix + runs-on: ubuntu-latest + + steps: + - name: Calculate file differences + uses: lots0logs/gh-action-get-changed-files@2.1.4 + id: diff + with: + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Checkout + uses: actions/checkout@v2 + + - name: Generate testing matrix + uses: actions/github-script@v6 + id: generator + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const script = require(`${process.env.GITHUB_WORKSPACE}/genMatrix.js`) + return script( + ${{ steps.diff.outputs.added }}, + ${{ steps.diff.outputs.modified }}, + ${{ steps.diff.outputs.renamed }}, + ); + outputs: + matrix: ${{ steps.generator.outputs.result }} + + build: + if: ${{ fromJson(needs.gen-matrix.outputs.matrix) }} + needs: gen-matrix + name: build + runs-on: ubuntu-latest + timeout-minutes: 60 + strategy: + fail-fast: false + matrix: ${{ fromJson(needs.gen-matrix.outputs.matrix) }} + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Build image + uses: docker/build-push-action@v2 + with: + push: false + load: true + context: ./${{ matrix.variant }} + file: ./${{ matrix.variant }}/Dockerfile + tags: ${{ matrix.variant }}:latest diff --git a/codeplayer-frontend/Dockerfile b/codeplayer-frontend/Dockerfile index 29f0e19..1337fd1 100755 --- a/codeplayer-frontend/Dockerfile +++ b/codeplayer-frontend/Dockerfile @@ -1,13 +1,24 @@ +# --- Build the frontend --- FROM node:12.16.1-alpine3.9 as build MAINTAINER Saurabh Srivastava (saurass.in) +ARG API_SERVER_HOST +ARG SOCKETIO_SERVER_HOST +ARG RAZOR_PAY_KEY_ID +ARG RECAPTCHA_KEY +ENV REACT_APP_BACKEND=$API_SERVER_HOST +ENV REACT_APP_SOCKETIO_SERVER=$SOCKETIO_SERVER_HOST +ENV REACT_APP_RAZORPAY_API_ID=$RAZOR_PAY_KEY_ID +ENV REACT_APP_RECAPTCHA_KEY=$RECAPTCHA_KEY + WORKDIR /codeplayer_frontend COPY package*.json /codeplayer_frontend/ RUN yarn COPY . /codeplayer_frontend RUN yarn build +# Step 2: Packages assets for serving FROM nginx:alpine MAINTAINER Saurabh Srivastava (saurass.in) diff --git a/genMatrix.js b/genMatrix.js new file mode 100644 index 0000000..127567f --- /dev/null +++ b/genMatrix.js @@ -0,0 +1,80 @@ +'use strict'; +const path = require('path'); +const fs = require('fs'); + +const testFiles = [ + 'genMatrix.js', + '.github/workflows/build-test.yml', +]; + +const nodeDirRegex = /^\d+$/; + +// const areTestFilesChanged = (changedFiles) => changedFiles +// .some((file) => testFiles.includes(file)); + +// // Returns a list of the child directories in the given path +// const getChildDirectories = (parent) => fs.readdirSync(parent, { withFileTypes: true }) +// .filter((dirent) => dirent.isDirectory()) +// .map(({ name }) => path.resolve(parent, name)); + +// const getNodeVerionDirs = (base) => getChildDirectories(base) +// .filter((childPath) => nodeDirRegex.test(path.basename(childPath))); + +// Returns the paths of Dockerfiles that are at: base/*/Dockerfile +// const getDockerfilesInChildDirs = (base) => getChildDirectories(base) +// .map((childDir) => path.resolve(childDir, 'Dockerfile')); + +// const getAllDockerfiles = (base) => getNodeVerionDirs(base).flatMap(getDockerfilesInChildDirs); + +const getAffectedDockerfiles = (filesAdded, filesModified, filesRenamed) => { + console.log(filesAdded) + const files = [ + ...filesAdded, + ...filesModified, + ...filesRenamed, + ]; + + // If the test files were changed, include everything + // if (areTestFilesChanged(files)) { + // console.log('Test files changed so scheduling all Dockerfiles'); + // return getAllDockerfiles(__dirname); + // } + + const modifiedDockerfiles = files.filter((file) => file.endsWith('/Dockerfile')); + + // Get Dockerfiles affected by modified docker-entrypoint.sh files + const entrypointAffectedDockerfiles = files + .filter((file) => file.endsWith('/docker-entrypoint.sh')) + .map((file) => path.resolve(path.dirname(file), 'Dockerfile')); + + return [ + ...modifiedDockerfiles, + ...entrypointAffectedDockerfiles, + ]; +}; + +// const getFullNodeVersionFromDockerfile = (file) => fs.readFileSync(file, 'utf8') +// .match(/^ENV NODE_VERSION (\d*\.*\d*\.\d*)/m)[1]; + +const getDockerfileMatrixEntry = (file) => { + const [variant] = path.dirname(file).split(path.sep).slice(-1); + + // const version = getFullNodeVersionFromDockerfile(file); + + return { + // version, + variant, + }; +}; + +const generateBuildMatrix = (filesAdded, filesModified, filesRenamed) => { + const dockerfiles = [...new Set(getAffectedDockerfiles(filesAdded, filesModified, filesRenamed))]; + const entries = dockerfiles.map(getDockerfileMatrixEntry); + + // Return null if there are no entries so we can skip the matrix step + return entries.length + ? { include: entries } + : null; +}; + +module.exports = generateBuildMatrix;