From 548f10673b9850283f3e7ba4ff68fdb04c5b8598 Mon Sep 17 00:00:00 2001 From: arielly-parussulo Date: Tue, 30 May 2023 17:16:48 -0300 Subject: [PATCH] init runner as ec2-user add pre script to setup tools before running the runner --- .github/workflows/package.yml | 28 ------------------ .github/workflows/pr.yml | 21 -------------- .vscode/launch.json | 13 --------- .vscode/settings.json | 8 ------ action.yml | 4 +++ dist/index.js | 53 ++++++++++++++++++++--------------- src/aws.js | 52 +++++++++++++++++++--------------- src/config.js | 1 + 8 files changed, 66 insertions(+), 114 deletions(-) delete mode 100644 .github/workflows/package.yml delete mode 100644 .github/workflows/pr.yml delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/settings.json diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml deleted file mode 100644 index 092ba896..00000000 --- a/.github/workflows/package.yml +++ /dev/null @@ -1,28 +0,0 @@ -on: - push: - branches: - - main - -name: Package - -jobs: - package: - name: Package distribution files - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - with: - ref: main - - name: Install packages - run: npm ci - - name: Run linter - run: npm run lint - - name: Package - run: npm run package - - name: Commit - run: | - git config --global user.name "GitHub Actions" - git add dist/ - git commit -m "Update dist" || echo "No changes to commit" - git push origin main diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml deleted file mode 100644 index f98cc71b..00000000 --- a/.github/workflows/pr.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: PR automations -on: - pull_request: - branches: - - main -jobs: - lint-code: - name: Lint code - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - - name: Cache dependencies - uses: actions/cache@v3 - with: - path: '**/node_modules' - key: ec2-github-runner-${{ hashFiles('**/package-lock.json') }} - - name: Install packages - run: npm install - - name: Run linter - run: npm run lint diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 1bf3655e..00000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "name": "debug index", - "type": "node", - "request": "launch", - "cwd": "${workspaceFolder}", - "runtimeExecutable": "npm", - "runtimeArgs": ["run-script", "index"] - } - ] -} diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 9f612af5..00000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "editor.formatOnSave": true, - "editor.defaultFormatter": "esbenp.prettier-vscode", - "[javascript]": { - "editor.formatOnSave": true, - "editor.defaultFormatter": "esbenp.prettier-vscode" - } -} diff --git a/action.yml b/action.yml index f5900e78..f4fe27ad 100644 --- a/action.yml +++ b/action.yml @@ -74,6 +74,10 @@ inputs: description: >- The host id to provide a dedicated host in your account. It is necessary for macos instances. required: false + pre-script: + description: >- + A script that will be executed before starting the Github Runner. + required: false timeout: description: The time (in minutes) that this actions will wait the the runner be created. The default value is 5 minutes. diff --git a/dist/index.js b/dist/index.js index 8c8d6032..a9940e1c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -62807,28 +62807,36 @@ function buildUserDataScript(githubRegistrationToken, label) { if (config.input.runnerHomeDir) { // If runner home directory is specified, we expect the actions-runner software (and dependencies) // to be pre-installed in the AMI, so we simply cd into that directory and then start the runner - return [ - '#!/bin/bash', - `cd "${config.input.runnerHomeDir}"`, - 'export RUNNER_ALLOW_RUNASROOT=1', - `./config.sh --url ${config.github.url} --token ${githubRegistrationToken} --labels ${label} --name ${label} --runnergroup default --work "${config.input.runnerHomeDir}" --replace`, - './run.sh', - ]; + return ``` +#!/bin/bash + +${config.input.preScript} + +su ec2-user -c 'echo "export RUNNER_ALLOW_RUNASROOT=1" >> $HOME/.zshrc' +su ec2-user -c '${config.input.runnerHomeDir}/./config.sh --url ${config.github.url} --token ${githubRegistrationToken} --labels ${label} --name ${label} --runnergroup default --work "${config.input.runnerHomeDir}" --replace' +su ec2-user -c '${config.input.runnerHomeDir}/./run.sh' + ``` } else { - return [ - '#!/bin/bash', - 'cd /opt && mkdir actions-runner && cd actions-runner', - 'case $(uname -m) in aarch64|arm64) ARCH="arm64" ;; amd64|x86_64) ARCH="x64" ;; esac && export RUNNER_ARCH=${ARCH}', - 'case $(uname -a) in Darwin*) OS="osx" ;; Linux*) OS="linux" ;; esac && export RUNNER_OS=${OS}', - 'export VERSION="2.303.0"', - 'curl -O -L https://github.com/actions/runner/releases/download/v${VERSION}/actions-runner-${RUNNER_OS}-${RUNNER_ARCH}-${VERSION}.tar.gz', - 'export LC_ALL=en_US.UTF-8', - 'export LANG=en_EN.UTF-8', - 'tar xzf ./actions-runner-${RUNNER_OS}-${RUNNER_ARCH}-${VERSION}.tar.gz', - 'export RUNNER_ALLOW_RUNASROOT=1', - `./config.sh --url ${config.github.url} --token ${githubRegistrationToken} --labels ${label} --name ${label} --runnergroup default --work $(pwd) --replace`, - './run.sh', - ]; + return ``` +#!/bin/bash + +${config.input.preScript} + +case $(uname -m) in aarch64|arm64) ARCH="arm64" ;; amd64|x86_64) ARCH="x64" ;; esac && export RUNNER_ARCH=${ARCH} +case $(uname -a) in Darwin*) OS="osx" ;; Linux*) OS="linux" ;; esac && export RUNNER_OS=${OS} +export VERSION="2.303.0" + +su ec2-user -c "curl -O -L https://github.com/actions/runner/releases/download/v${VERSION}/actions-runner-${RUNNER_OS}-${RUNNER_ARCH}-${VERSION}.tar.gz -o /tmp/actions-runner-${RUNNER_OS}-${RUNNER_ARCH}-${VERSION}.tar.gz" +su ec2-user -c 'echo "export LC_ALL=en_US.UTF-8" >> $HOME/.zshrc' +su ec2-user -c 'echo "export LANG=en_US.UTF-8" >> $HOME/.zshrc' +su ec2-user -c 'echo "export RUNNER_ALLOW_RUNASROOT=1" >> $HOME/.zshrc' +su ec2-user -c 'mkdir /tmp/actions-runner' +su ec2-user -c "tar xzf /tmp/actions-runner-${RUNNER_OS}-${RUNNER_ARCH}-${VERSION}.tar.gz -C /tmp/actions-runner --strip-components=1" +su ec2-user -c 'mv /tmp/actions-runner $HOME/actions-runner' +tar xzf ./actions-runner-${RUNNER_OS}-${RUNNER_ARCH}-${VERSION}.tar.gz +su ec2-user -c '$HOME/actions-runner/./config.sh --url ${config.github.url} --token ${githubRegistrationToken} --labels ${label} --name ${label} --runnergroup default --work $(pwd) --replace' +su ec2-user -c '$HOME/actions-runner/./run.sh &' +```; } } @@ -62843,7 +62851,7 @@ async function startEc2Instance(label, githubRegistrationToken) { InstanceType: config.input.ec2InstanceType, MinCount: 1, MaxCount: 1, - UserData: Buffer.from(userData.join('\n')).toString('base64'), + UserData: Buffer.from(userData).toString('base64'), SubnetId: config.input.subnetId, SecurityGroupIds: [config.input.securityGroupId], IamInstanceProfile: { Name: config.input.iamRoleName }, @@ -62932,6 +62940,7 @@ class Config { runnerHomeDir: core.getInput('runner-home-dir'), scope: core.getInput('scope'), hostId: core.getInput('host-id'), + preScript: core.getInput('pre-script') || '', }; this.GITHUB_SCOPES = { diff --git a/src/aws.js b/src/aws.js index 34d28a83..79028d72 100644 --- a/src/aws.js +++ b/src/aws.js @@ -7,28 +7,36 @@ function buildUserDataScript(githubRegistrationToken, label) { if (config.input.runnerHomeDir) { // If runner home directory is specified, we expect the actions-runner software (and dependencies) // to be pre-installed in the AMI, so we simply cd into that directory and then start the runner - return [ - '#!/bin/bash', - `cd "${config.input.runnerHomeDir}"`, - 'export RUNNER_ALLOW_RUNASROOT=1', - `./config.sh --url ${config.github.url} --token ${githubRegistrationToken} --labels ${label} --name ${label} --runnergroup default --work "${config.input.runnerHomeDir}" --replace`, - './run.sh', - ]; + return ``` +#!/bin/bash + +${config.input.preScript} + +su ec2-user -c 'echo "export RUNNER_ALLOW_RUNASROOT=1" >> $HOME/.zshrc' +su ec2-user -c '${config.input.runnerHomeDir}/./config.sh --url ${config.github.url} --token ${githubRegistrationToken} --labels ${label} --name ${label} --runnergroup default --work "${config.input.runnerHomeDir}" --replace' +su ec2-user -c '${config.input.runnerHomeDir}/./run.sh' + ``` } else { - return [ - '#!/bin/bash', - 'cd /opt && mkdir actions-runner && cd actions-runner', - 'case $(uname -m) in aarch64|arm64) ARCH="arm64" ;; amd64|x86_64) ARCH="x64" ;; esac && export RUNNER_ARCH=${ARCH}', - 'case $(uname -a) in Darwin*) OS="osx" ;; Linux*) OS="linux" ;; esac && export RUNNER_OS=${OS}', - 'export VERSION="2.303.0"', - 'curl -O -L https://github.com/actions/runner/releases/download/v${VERSION}/actions-runner-${RUNNER_OS}-${RUNNER_ARCH}-${VERSION}.tar.gz', - 'export LC_ALL=en_US.UTF-8', - 'export LANG=en_EN.UTF-8', - 'tar xzf ./actions-runner-${RUNNER_OS}-${RUNNER_ARCH}-${VERSION}.tar.gz', - 'export RUNNER_ALLOW_RUNASROOT=1', - `./config.sh --url ${config.github.url} --token ${githubRegistrationToken} --labels ${label} --name ${label} --runnergroup default --work $(pwd) --replace`, - './run.sh', - ]; + return ``` +#!/bin/bash + +${config.input.preScript} + +case $(uname -m) in aarch64|arm64) ARCH="arm64" ;; amd64|x86_64) ARCH="x64" ;; esac && export RUNNER_ARCH=${ARCH} +case $(uname -a) in Darwin*) OS="osx" ;; Linux*) OS="linux" ;; esac && export RUNNER_OS=${OS} +export VERSION="2.303.0" + +su ec2-user -c "curl -O -L https://github.com/actions/runner/releases/download/v${VERSION}/actions-runner-${RUNNER_OS}-${RUNNER_ARCH}-${VERSION}.tar.gz -o /tmp/actions-runner-${RUNNER_OS}-${RUNNER_ARCH}-${VERSION}.tar.gz" +su ec2-user -c 'echo "export LC_ALL=en_US.UTF-8" >> $HOME/.zshrc' +su ec2-user -c 'echo "export LANG=en_US.UTF-8" >> $HOME/.zshrc' +su ec2-user -c 'echo "export RUNNER_ALLOW_RUNASROOT=1" >> $HOME/.zshrc' +su ec2-user -c 'mkdir /tmp/actions-runner' +su ec2-user -c "tar xzf /tmp/actions-runner-${RUNNER_OS}-${RUNNER_ARCH}-${VERSION}.tar.gz -C /tmp/actions-runner --strip-components=1" +su ec2-user -c 'mv /tmp/actions-runner $HOME/actions-runner' +tar xzf ./actions-runner-${RUNNER_OS}-${RUNNER_ARCH}-${VERSION}.tar.gz +su ec2-user -c '$HOME/actions-runner/./config.sh --url ${config.github.url} --token ${githubRegistrationToken} --labels ${label} --name ${label} --runnergroup default --work $(pwd) --replace' +su ec2-user -c '$HOME/actions-runner/./run.sh &' +```; } } @@ -43,7 +51,7 @@ async function startEc2Instance(label, githubRegistrationToken) { InstanceType: config.input.ec2InstanceType, MinCount: 1, MaxCount: 1, - UserData: Buffer.from(userData.join('\n')).toString('base64'), + UserData: Buffer.from(userData).toString('base64'), SubnetId: config.input.subnetId, SecurityGroupIds: [config.input.securityGroupId], IamInstanceProfile: { Name: config.input.iamRoleName }, diff --git a/src/config.js b/src/config.js index d16a7f61..87891774 100644 --- a/src/config.js +++ b/src/config.js @@ -16,6 +16,7 @@ class Config { runnerHomeDir: core.getInput('runner-home-dir'), scope: core.getInput('scope'), hostId: core.getInput('host-id'), + preScript: core.getInput('pre-script') || '', }; this.GITHUB_SCOPES = {