Factor out build job logic into a "run-as-coder" reusable workflow. #3
Workflow file for this run
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
name: Run as coder user | |
defaults: | |
run: | |
shell: bash -exo pipefail {0} | |
on: | |
workflow_call: | |
inputs: | |
image: {type: string, required: true} | |
runner: {type: string, required: true} | |
command: {type: string, required: true} | |
env: { type: object, required: false, default: {} } | |
jobs: | |
run-as-coder: | |
name: Run ${{inputs.command}} | |
runs-on: ${{inputs.runner}} | |
container: | |
options: -u root | |
image: ${{inputs.image}} | |
env: ${{inputs.env}} | |
permissions: | |
id-token: write | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v3 | |
with: | |
path: cccl | |
- name: Move files to coder user home directory | |
run: | | |
cp -R cccl /home/coder/cccl | |
chown -R coder:coder /home/coder/ | |
- name: Configure credentials and environment variables for sccache | |
uses: ./cccl/.github/actions/configure_cccl_sccache | |
- name: Run command | |
shell: su coder {0} | |
run: | | |
cd ~/cccl | |
eval ${{inputs.command}} || exit_code=$? | |
if [ ! -z "$exit_code" ]; then | |
echo "::error::Error! To checkout the corresponding code and reproduce locally, run the following commands:" | |
echo "git clone --branch $GITHUB_REF_NAME --single-branch --recurse-submodules https://github.com/$GITHUB_REPOSITORY.git && cd $(echo $GITHUB_REPOSITORY | cut -d'/' -f2) && git checkout $GITHUB_SHA" | |
echo "docker run --rm -it --gpus all --pull=always --volume \$PWD:/repo --workdir /repo ${{ inputs.image }} ${{inputs.command}}" | |
exit $exit_code | |
fi | |