Skip to content

Commit

Permalink
Add 'runCmdOutput' output to GitHub Action/AzDO Task when using 'runC…
Browse files Browse the repository at this point in the history
…md' input (#165)

* Add runCmdOutput output to GitHub action

* Limit runCmdOutput size to <1MB

* Add runCmdOutput output to Azure DevOps Task

* Run local build again
  • Loading branch information
natescherer authored Sep 27, 2022
1 parent 4986410 commit c591d20
Show file tree
Hide file tree
Showing 18 changed files with 244 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .azure-devops/azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ jobs:
inputs:
subFolder: github-tests/Dockerfile/run-args
runCmd: echo $HOSTNAME && [[ $HOSTNAME == "my-host" ]]
- script: |
echo "'runCmdOutput' value: $runCmdOutput"
if [["$runCmdOutput" = *my-host*]]; then
echo "'runCmdOutput' output of test_simple job doesn't contain expected value 'my-host'"
exit 1
fi
- job: test_build_args
displayName: Test build-args
Expand Down
10 changes: 10 additions & 0 deletions .github/workflows/ci_common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,19 @@ jobs:

- name: Run test
uses: ./
id: simpletest
with:
subFolder: github-tests/Dockerfile/run-args
runCmd: echo $HOSTNAME && [[ $HOSTNAME == "my-host" ]]
- name: Validate runCmdOutput output
run: |
echo "'runCmdOutput' value: $runCmdOutput"
if [["$runCmdOutput" = *my-host*]]; then
echo "'runCmdOutput' output of simpletest step doesn't contain expected value 'my-host'"
exit 1
fi
env:
runCmdOutput: ${{ steps.simpletest.outputs.runCmdOutput }}

test-gh-run-args:
name: Run GitHub run-args test
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ inputs:
cacheFrom:
required: False
description: Specify additional images to use for build caching
outputs:
runCmdOutput:
description: The output of the command specified in the runCmd input
runs:
using: 'node16'
main: 'github-action/run-main.js'
Expand Down
14 changes: 13 additions & 1 deletion azdo-task/DevcontainersCi/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,26 @@ function runMain() {
command: ['bash', '-c', runCommand],
env: inputEnvsWithDefaults,
};
const execResult = yield dev_container_cli_1.devcontainer.exec(execArgs, log);
let execLogString = '';
const execLog = (message) => {
console.log(message);
if (!message.includes('@devcontainers/cli')) {
execLogString += message;
}
};
const execResult = yield dev_container_cli_1.devcontainer.exec(execArgs, execLog);
if (execResult.outcome !== 'success') {
console.log(`### ERROR: Dev container exec: ${execResult.message} (exit code: ${execResult.code})\n${execResult.description}`);
task.setResult(task_1.TaskResult.Failed, execResult.message);
}
if (execResult.outcome !== 'success') {
return;
}
if (execLogString.length >= 25000) {
execLogString = execLogString.substring(0, 24963);
execLogString += 'TRUNCATED TO 25K CHAR MAX OUTPUT SIZE';
}
console.log(`##vso[task.setvariable variable=runCmdOutput]${execLog}`);
}
else {
console.log('No runCmd set - skipping starting/running container');
Expand Down
2 changes: 1 addition & 1 deletion azdo-task/DevcontainersCi/dist/index.js.map

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion azdo-task/DevcontainersCi/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,26 @@ function runMain() {
command: ['bash', '-c', runCommand],
env: inputEnvsWithDefaults,
};
const execResult = yield dev_container_cli_1.devcontainer.exec(execArgs, log);
let execLogString = '';
const execLog = (message) => {
console.log(message);
if (!message.includes('@devcontainers/cli')) {
execLogString += message;
}
};
const execResult = yield dev_container_cli_1.devcontainer.exec(execArgs, execLog);
if (execResult.outcome !== 'success') {
console.log(`### ERROR: Dev container exec: ${execResult.message} (exit code: ${execResult.code})\n${execResult.description}`);
task.setResult(task_1.TaskResult.Failed, execResult.message);
}
if (execResult.outcome !== 'success') {
return;
}
if (execLogString.length >= 25000) {
execLogString = execLogString.substring(0, 24963);
execLogString += 'TRUNCATED TO 25K CHAR MAX OUTPUT SIZE';
}
console.log(`##vso[task.setvariable variable=runCmdOutput]${execLog}`);
}
else {
console.log('No runCmd set - skipping starting/running container');
Expand Down
14 changes: 13 additions & 1 deletion azdo-task/DevcontainersCi/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ export async function runMain(): Promise<void> {
command: ['bash', '-c', runCommand],
env: inputEnvsWithDefaults,
};
const execResult = await devcontainer.exec(execArgs, log);
let execLogString = '';
const execLog = (message: string): void => {
console.log(message);
if (!message.includes('@devcontainers/cli')) {
execLogString += message;
}
};
const execResult = await devcontainer.exec(execArgs, execLog);
if (execResult.outcome !== 'success') {
console.log(
`### ERROR: Dev container exec: ${execResult.message} (exit code: ${execResult.code})\n${execResult.description}`,
Expand All @@ -125,6 +132,11 @@ export async function runMain(): Promise<void> {
if (execResult.outcome !== 'success') {
return;
}
if (execLogString.length >= 25000) {
execLogString = execLogString.substring(0, 24963);
execLogString += 'TRUNCATED TO 25K CHAR MAX OUTPUT SIZE';
}
console.log(`##vso[task.setvariable variable=runCmdOutput]${execLog}`);
} else {
console.log('No runCmd set - skipping starting/running container');
}
Expand Down
5 changes: 4 additions & 1 deletion azdo-task/DevcontainersCi/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@
"required": false
}
],
"outputVariables": [],
"outputVariables": [{
"name": "runCmdOutput",
"description": "The output of the command specified in the runCmd input"
}],
"execution": {
"Node10": {
"target": "run-main.js",
Expand Down
5 changes: 5 additions & 0 deletions azdo-task/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ In the example above, the devcontainer-build-run will perform the following step
| skipContainerUserIdUpdate | false | For non-root dev containers (i.e. where `remoteUser` is specified), the action attempts to make the container user UID and GID match those of the host user. Set this to true to skip this step (defaults to false) |
| cacheFrom | false | Specify additional images to use for build caching |

## Outputs

| Name | Description |
| ------------ | ------------------------------------------------------- |
| runCmdOutput | The output of the command specified in the runCmd input |

## Specifying a sub-folder

Expand Down
5 changes: 5 additions & 0 deletions docs/azure-devops-task.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ In the example above, the devcontainer-build-run will perform the following step
| skipContainerUserIdUpdate | false | For non-root dev containers (i.e. where `remoteUser` is specified), the action attempts to make the container user UID and GID match those of the host user. Set this to true to skip this step (defaults to false) |
| cacheFrom | false | Specify additional images to use for build caching |

## Outputs

| Name | Description |
| ------------ | ------------------------------------------------------- |
| runCmdOutput | The output of the command specified in the runCmd input |

## Specifying a sub-folder

Expand Down
6 changes: 6 additions & 0 deletions docs/github-action.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ The [`devcontainers/ci` action](https://github.com/marketplace/actions/devcontai
| skipContainerUserIdUpdate | false | For non-root dev containers (i.e. where `remoteUser` is specified), the action attempts to make the container user UID and GID match those of the host user. Set this to true to skip this step (defaults to false) |
| cacheFrom | false | Specify additional images to use for build caching |

## Outputs

| Name | Description |
| ------------ | ------------------------------------------------------- |
| runCmdOutput | The output of the command specified in the runCmd input |

## Specifying a sub-folder

Suppose your repo has the following structure:
Expand Down
92 changes: 85 additions & 7 deletions github-action/dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion github-action/dist/index.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions github-action/dist/licenses.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


truncate-utf8-bytes
WTFPL
16 changes: 15 additions & 1 deletion github-action/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.runPost = exports.runMain = void 0;
const core = __importStar(require("@actions/core"));
const truncate_utf8_bytes_1 = __importDefault(require("truncate-utf8-bytes"));
const path_1 = __importDefault(require("path"));
const exec_1 = require("./exec");
const dev_container_cli_1 = require("../../common/src/dev-container-cli");
Expand Down Expand Up @@ -130,11 +131,24 @@ function runMain() {
env: inputEnvsWithDefaults,
userDataFolder,
};
const result = yield dev_container_cli_1.devcontainer.exec(args, log);
let execLogString = '';
const execLog = (message) => {
core.info(message);
if (!message.includes('@devcontainers/cli')) {
execLogString += message;
}
};
const result = yield dev_container_cli_1.devcontainer.exec(args, execLog);
if (result.outcome !== 'success') {
core.error(`Dev container exec: ${result.message} (exit code: ${result.code})\n${result.description}`);
core.setFailed(result.message);
}
core.setOutput('runCmdOutput', execLogString);
if (Buffer.byteLength(execLogString, 'utf-8') > 1000000) {
execLogString = truncate_utf8_bytes_1.default(execLogString, 999966);
execLogString += 'TRUNCATED TO 1 MB MAX OUTPUT SIZE';
}
core.setOutput('runCmdOutput', execLogString);
return result;
}));
if (execResult.outcome !== 'success') {
Expand Down
Loading

0 comments on commit c591d20

Please sign in to comment.