Skip to content

Commit

Permalink
ci: test images on PR and push (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego authored Nov 25, 2024
1 parent b6152ef commit a04badd
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 1 deletion.
98 changes: 98 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: build-test

on:
push:
paths:
- "**/Dockerfile"
- genMatrix.js
- compose.yml
- ".github/workflows/build-test.yml"

pull_request:
paths:
- "**/Dockerfile"
- genMatrix.js
- compose.yml
- ".github/workflows/build-test.yml"

jobs:
gen-matrix:
name: generate-matrix
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Calculate file differences
id: diff
uses: tj-actions/changed-files@v45
with:
json: true
escape_json: false

- name: Generate testing matrix
uses: actions/github-script@v7
id: generator
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const script = require(`${process.env.GITHUB_WORKSPACE}/genMatrix.js`)
return script(
${{ steps.diff.outputs.added_files }},
${{ steps.diff.outputs.modified_files }},
${{ steps.diff.outputs.renamed_files }},
);
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: Get short node version
uses: actions/github-script@v7
id: short-version
with:
result-encoding: string
script: |
const version = "${{ matrix.version }}".split('.');
return `${version[0]}.${version[1]}`
- name: Checkout
uses: actions/checkout@v4

- name: Build image
uses: docker/build-push-action@v6
with:
push: false
load: true
context: .
file: ./${{ steps.short-version.outputs.result }}/Dockerfile
tags: rocket.chat:${{ matrix.version }}

- name: Run Image
env:
IMAGE: rocket.chat
RELEASE: ${{ matrix.version }}
run: docker compose up -d

- name: Wait for Rocket.Chat to start up
uses: cygnetdigital/wait_for_response@v2.0.0
with:
url: 'http://localhost:3000/health'
responseCode: '200'
timeout: 60000
interval: 1000

- name: Show logs if failed
if: failure()
run: docker compose logs
2 changes: 1 addition & 1 deletion compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ volumes:

services:
rocketchat:
image: registry.rocket.chat/rocketchat/rocket.chat:${RELEASE:-latest}
image: ${IMAGE:-registry.rocket.chat/rocketchat/rocket.chat}:${RELEASE:-latest}
restart: always
labels:
traefik.enable: "true"
Expand Down
68 changes: 68 additions & 0 deletions genMatrix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';
const path = require('path');
const fs = require('fs');

const testFiles = [
'genMatrix.js',
'.github/workflows/build-test.yml',
'compose.yml',
];

const rcDirRegex = /^\d+\.\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 getRocketChatVersionDirs = (base) => getChildDirectories(base)
.filter((childPath) => rcDirRegex.test(path.basename(childPath)));

// Returns the paths of Dockerfiles that are at: base/*/Dockerfile
const getDockerfilesInChildDirs = (base) => path.resolve(base, 'Dockerfile');

const getAllDockerfiles = (base) => getRocketChatVersionDirs(base)
.flatMap(getDockerfilesInChildDirs);

const getAffectedDockerfiles = (filesAdded, filesModified, filesRenamed) => {
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);
}

return files.filter((file) => file.endsWith('/Dockerfile'));
};

const getFullRocketChatVersionFromDockerfile = (file) => fs.readFileSync(file, 'utf8')
.match(/^ENV RC_VERSION (\d*\.*\d*\.\d*)/m)[1];

const getDockerfileMatrixEntry = (file) => {
const version = getFullRocketChatVersionFromDockerfile(file);

return {
version,
};
};

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;

0 comments on commit a04badd

Please sign in to comment.