Skip to content

SHA based github Action Added for docker build. #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
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
11 changes: 11 additions & 0 deletions codeplayer-frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -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)
80 changes: 80 additions & 0 deletions genMatrix.js
Original file line number Diff line number Diff line change
@@ -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;