-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: test images on PR and push (#216)
- Loading branch information
1 parent
b6152ef
commit a04badd
Showing
3 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |