From 6149368b2616b6301f98bd4bfae56616b360dcef Mon Sep 17 00:00:00 2001 From: Steven Hangger Date: Fri, 23 Dec 2022 13:53:37 +0800 Subject: [PATCH 1/3] feat(base_image): add custom base_image argument --- .github/workflows/advanced-example.yml | 4 +++ .github/workflows/test.yml | 4 +-- .gitignore | 1 + README.md | 1 + action.yml | 4 +++ src/run-on-arch.js | 49 ++++++++++++++++++++------ 6 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 .gitignore diff --git a/.github/workflows/advanced-example.yml b/.github/workflows/advanced-example.yml index f16f92d2..7b52d6b9 100644 --- a/.github/workflows/advanced-example.yml +++ b/.github/workflows/advanced-example.yml @@ -21,6 +21,9 @@ jobs: distro: fedora_latest - arch: armv7 distro: archarm_latest + - arch: x86_64 + distro: ubuntu18.04 + base_image: ubuntu:18.04 steps: - uses: actions/checkout@v3 @@ -30,6 +33,7 @@ jobs: with: arch: ${{ matrix.arch }} distro: ${{ matrix.distro }} + base_image: ${{ matrix.base_image }} # Not required, but speeds up builds githubToken: ${{ github.token }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2a997310..a2bc5ccd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -85,8 +85,8 @@ jobs: install: | case "${{ matrix.distro }}" in ubuntu*|jessie|stretch|buster|bullseye) - apt-get update -q -y - apt-get install -q -y git + apt-get update -q -y --force-yes + apt-get install -q -y --force-yes git ;; fedora*) dnf -y update diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..dbe9c82b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/ \ No newline at end of file diff --git a/README.md b/README.md index fdbf823e..2bf71c40 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ The action also accepts some optional input parameters: * `dockerRunArgs`: Additional arguments to pass to `docker run`, such as volume mappings. See [`docker run` documentation](https://docs.docker.com/engine/reference/commandline/run). * `setup`: Shell commands to execute on the host before running the container, such as creating directories for volume mappings. * `install`: Shell commands to execute in the container as part of `docker build`, such as installing dependencies. This speeds up subsequent builds if `githubToken` is also used, but note that the image layer will be publicly available in your projects GitHub Package Registry, so make sure the resulting image does not have any secrets cached in logs or state. +* `base_image`: Specify a custom base image. This is optional. See the [advanced example](./.github/workflows/advanced-example.yml) in this repo. ### Basic example diff --git a/action.yml b/action.yml index ff1ae705..8ea805c0 100644 --- a/action.yml +++ b/action.yml @@ -39,6 +39,10 @@ inputs: description: 'Shell commands to execute in the container as part of docker build, such as installing dependencies. This speeds up subsequent builds if githubToken is also used, but note that the image layer will be publicly available in your projects GitHub Package Registry, so make sure the resulting image does not have any secrets cached in logs or state.' required: false default: '' + base_image: + description: 'Using the specific custom base image.' + required: false + default: '' runs: using: 'node16' diff --git a/src/run-on-arch.js b/src/run-on-arch.js index aeca1b2e..06d3d9a5 100644 --- a/src/run-on-arch.js +++ b/src/run-on-arch.js @@ -1,9 +1,13 @@ +/*jshint esversion: 9 */ + const core = require('@actions/core') const fs = require('fs'); const path = require('path') const YAML = require('yaml'); const shlex = require('shlex'); -const { exec } = require('@actions/exec') +const { + exec +} = require('@actions/exec') function slug(str) { return str.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase(); @@ -14,12 +18,30 @@ async function main() { throw new Error('run-on-arch supports only Linux') } - const arch = core.getInput('arch', { required: true }); - const distro = core.getInput('distro', { required: true }); + const arch = core.getInput('arch', { + required: true + }); + const distro = core.getInput('distro', { + required: true + }); + const base_image = core.getInput('base_image', { + required: false + }); // If bad arch/distro passed, fail fast before installing all the qemu stuff const dockerFile = path.join( __dirname, '..', 'Dockerfiles', `Dockerfile.${arch}.${distro}`); + + // If a custom base image is given, then dynamically create its Dockerfile. + if (base_image) { + let lines = []; + lines.push(`FROM ${base_image}`); + lines.push("COPY ./run-on-arch-install.sh /root/run-on-arch-install.sh"); + lines.push("RUN chmod +x /root/run-on-arch-install.sh && /root/run-on-arch-install.sh"); + console.log(`Writing custom Dockerfile to: ${dockerFile} ...`); + fs.writeFileSync(dockerFile, lines.join("\n")); + } + if (!fs.existsSync(dockerFile)) { throw new Error(`run-on-arch: ${dockerFile} does not exist.`); } @@ -61,7 +83,9 @@ async function main() { // Write container commands to a script file for running const commands = [ - `#!${shell}`, 'set -eu;', core.getInput('run', { required: true }), + `#!${shell}`, 'set -eu;', core.getInput('run', { + required: true + }), ].join('\n'); fs.writeFileSync( path.join(__dirname, 'run-on-arch-commands.sh'), @@ -74,7 +98,9 @@ async function main() { const githubToken = core.getInput('githubToken'); // Copy environment variables from parent process - const env = { ...process.env }; + const env = { + ...process.env + }; if (githubToken) { env.GITHUB_TOKEN = githubToken; @@ -85,7 +111,7 @@ async function main() { // docker run. const envYAML = core.getInput('env'); if (envYAML) { - const mapping = YAML.parse(envYAML) + const mapping = YAML.parse(envYAML); if (typeof mapping !== 'object' || mapping instanceof Array) { throw new Error(`run-on-arch: env must be a flat mapping of key/value pairs.`); } @@ -105,14 +131,15 @@ async function main() { arch, distro, ].join('-')); - console.log('Configuring Docker for multi-architecture support') + console.log('Configuring Docker for multi-architecture support'); await exec( path.join(__dirname, 'run-on-arch.sh'), - [ dockerFile, containerName, ...dockerRunArgs ], - { env }, + [dockerFile, containerName, ...dockerRunArgs], { + env + }, ); } main().catch(err => { - core.setFailed(err.message) -}) + core.setFailed(err.message); +}); \ No newline at end of file From 3231f892fa423ca8d1d91e443babb9560cc61a28 Mon Sep 17 00:00:00 2001 From: Steven Hangger Date: Fri, 23 Dec 2022 18:05:37 +0800 Subject: [PATCH 2/3] fix(*): revert formatting --- src/run-on-arch.js | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/src/run-on-arch.js b/src/run-on-arch.js index 06d3d9a5..1282840c 100644 --- a/src/run-on-arch.js +++ b/src/run-on-arch.js @@ -1,13 +1,11 @@ /*jshint esversion: 9 */ -const core = require('@actions/core') +const core = require('@actions/core'); const fs = require('fs'); -const path = require('path') +const path = require('path'); const YAML = require('yaml'); const shlex = require('shlex'); -const { - exec -} = require('@actions/exec') +const { exec } = require('@actions/exec'); function slug(str) { return str.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase(); @@ -15,18 +13,12 @@ function slug(str) { async function main() { if (process.platform !== 'linux') { - throw new Error('run-on-arch supports only Linux') + throw new Error('run-on-arch supports only Linux'); } - const arch = core.getInput('arch', { - required: true - }); - const distro = core.getInput('distro', { - required: true - }); - const base_image = core.getInput('base_image', { - required: false - }); + const arch = core.getInput('arch', { required: true }); + const distro = core.getInput('distro', { required: true }); + const base_image = core.getInput('base_image', { required: false }); // If bad arch/distro passed, fail fast before installing all the qemu stuff const dockerFile = path.join( @@ -83,9 +75,7 @@ async function main() { // Write container commands to a script file for running const commands = [ - `#!${shell}`, 'set -eu;', core.getInput('run', { - required: true - }), + `#!${shell}`, 'set -eu;', core.getInput('run', { required: true }), ].join('\n'); fs.writeFileSync( path.join(__dirname, 'run-on-arch-commands.sh'), @@ -98,9 +88,7 @@ async function main() { const githubToken = core.getInput('githubToken'); // Copy environment variables from parent process - const env = { - ...process.env - }; + const env = { ...process.env }; if (githubToken) { env.GITHUB_TOKEN = githubToken; @@ -134,12 +122,11 @@ async function main() { console.log('Configuring Docker for multi-architecture support'); await exec( path.join(__dirname, 'run-on-arch.sh'), - [dockerFile, containerName, ...dockerRunArgs], { - env - }, + [ dockerFile, containerName, ...dockerRunArgs ], + { env }, ); } main().catch(err => { core.setFailed(err.message); -}); \ No newline at end of file +}); From b0c64c5a4fdfc60cdb12a7c7a7c27701b10e1c4f Mon Sep 17 00:00:00 2001 From: Steven Hangger Date: Fri, 23 Dec 2022 18:56:26 +0800 Subject: [PATCH 3/3] docs(base_image): add docs for base_image option --- README.md | 2 +- action.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2bf71c40..6698490d 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ The action also accepts some optional input parameters: * `dockerRunArgs`: Additional arguments to pass to `docker run`, such as volume mappings. See [`docker run` documentation](https://docs.docker.com/engine/reference/commandline/run). * `setup`: Shell commands to execute on the host before running the container, such as creating directories for volume mappings. * `install`: Shell commands to execute in the container as part of `docker build`, such as installing dependencies. This speeds up subsequent builds if `githubToken` is also used, but note that the image layer will be publicly available in your projects GitHub Package Registry, so make sure the resulting image does not have any secrets cached in logs or state. -* `base_image`: Specify a custom base image. This is optional. See the [advanced example](./.github/workflows/advanced-example.yml) in this repo. +* `base_image`: Specify a custom base image, such as **busybox** or your predefined base image. This will replace the **FROM** claus in the default Dockerfile on the fly. This is optional. Hint: with this option you are able to use all the available archs other than the ones showed in this page. See the [advanced example](./.github/workflows/advanced-example.yml) in this repo. For more detials, see [PR #103](https://github.com/uraimo/run-on-arch-action/pull/103#issuecomment-1363810049). ### Basic example diff --git a/action.yml b/action.yml index 8ea805c0..77342755 100644 --- a/action.yml +++ b/action.yml @@ -40,7 +40,7 @@ inputs: required: false default: '' base_image: - description: 'Using the specific custom base image.' + description: 'Specify a custom base image, such as \"busybox\" or your predefined base image. This will replace the \"FROM\" claus in the default Dockerfile on the fly. This is optional. Hint: with this option you are able to use all the available archs other than the ones showed by default. See the [advanced example](./.github/workflows/advanced-example.yml) in this repo. For more detials, see [PR #103](https://github.com/uraimo/run-on-arch-action/pull/103#issuecomment-1363810049).' required: false default: ''