Skip to content

Commit

Permalink
Convert runners to be org runners
Browse files Browse the repository at this point in the history
  • Loading branch information
mattops committed Jan 9, 2025
1 parent 115db12 commit 32fb0fa
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
6 changes: 3 additions & 3 deletions .github/actions/cloud-platform-deploy/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ inputs:
gh_auth_token:
description: Auth token for Github
required: true
github_repository:
description: The repository in which the runners are deployed
github_org:
description: The github org in which the runners are deployed
required: true
runner_labels:
description: The labels for the runners
Expand Down Expand Up @@ -73,5 +73,5 @@ runs:
--wait \
--set generic-service.env.GH_AUTH_TOKEN="${{ inputs.gh_auth_token }}" \
--set generic-service.env.RUNNER_LABELS="${{ inputs.runner_labels }}" \
--set generic-service.env.GITHUB_REPOSITORY="${{ inputs.github_repository }}"
--set generic-service.env.GH_ORG="${{ inputs.github_org }}"
12 changes: 6 additions & 6 deletions .github/actions/runner-cleanup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ inputs:
qty_runners:
description: 'Number of expected runners'
default: '1'
github_repository:
description: 'The repository in which the runners are deployed'
github_org:
description: 'The github org in which the runners are deployed'

runs:
using: "composite"
Expand All @@ -29,11 +29,11 @@ runs:
# Attempts to remove any offline runners - giving time for old ones to expire
def main():
expected_number_of_runners=os.getenv('qty_runners',1)
github_repository=os.getenv('github_repository')
github_org=os.getenv('github_org')
auth = Auth.Token(os.getenv('gh_auth_token'))
print(f'Repo name is: {github_repository}')
print(f'Repo name is: {github_org}')
g = Github(auth=auth)
repo = g.get_repo(github_repository)
repo = g.get_repo(github_org)
offline_deleted=False
qty_runners=retry_count=0
while qty_runners!=expected_number_of_runners and retry_count < 10:
Expand All @@ -53,5 +53,5 @@ runs:
main()
env:
github_repository: ${{ inputs.github_repository }}
github_org: ${{ inputs.github_org }}
gh_auth_token: ${{ inputs.gh_auth_token }}
4 changes: 2 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ jobs:
namespace: ${{ secrets.KUBE_NAMESPACE }}
token: ${{ secrets.KUBE_TOKEN }}
gh_auth_token: ${{ steps.generate-github-app-tokens.outputs.access-token }}
github_repository: ${{ vars.GH_REPOSITORY }}
github_org: ${{ github.repository_owner }}
runner_labels: ${{ vars.RUNNER_LABELS }}

- name: Remove offline runners
uses: ./.github/actions/runner-cleanup
with:
gh_auth_token: ${{ steps.generate-github-app-tokens.outputs.access-token }}
github_repository: ${{ vars.GH_REPOSITORY }}
github_org: ${{ github.repository_owner }}

2 changes: 1 addition & 1 deletion .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:

# Only need to deploy to production nowadays
deploy_to_prod:
if : github.ref == 'refs/heads/main'
#if : github.ref == 'refs/heads/main'
name: Deploy to prod
uses: ./.github/workflows/deploy.yml
needs:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# hmpps-github-actions-runner
This deploys a self-hosted Github Runner to a single repository named in the repository variables.
This deploys a self-hosted Github Runner to the organisation.

Documentation about it is [here](https://tech-docs.hmpps.service.justice.gov.uk/sreinternaldocs)

Expand All @@ -22,5 +22,5 @@ It's deployed to Cloud Platforms, using Helm.
### Repo environment variables

- GH_APP_ID - the corresponding AppId for the Github App
- GH_REPOSITORY - the repo to which the runner should be registered
- GH_ORG - the org to which the runner should be registered
- RUNNER_LABELS - the label by which the runner is invoked
12 changes: 7 additions & 5 deletions src/usr/local/bin/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ACTIONS_RUNNER_DIRECTORY="/actions-runner"
EPHEMERAL="${EPHEMERAL:-"false"}"

echo "Runner parameters:"
echo " Repository: ${GITHUB_REPOSITORY}"
echo " GitHub org: ${GH_ORG}"
echo " Runner Name: $(hostname)"
echo " Runner Labels: ${RUNNER_LABELS}"

Expand All @@ -16,9 +16,10 @@ getRegistrationToken=$(
--silent \
--location \
--request "POST" \
--header "Accept: application/vnd.github+json" \
--header "X-GitHub-Api-Version: 2022-11-28" \
--header "Authorization: Bearer ${GH_AUTH_TOKEN}" \
https://api.github.com/repos/"${GITHUB_REPOSITORY}"/actions/runners/registration-token | jq -r '.token'
https://api.github.com/orgs/${GH_ORG}"/actions/runners/registration-token | jq -r '.token'
)
export getRegistrationToken
Expand All @@ -39,16 +40,17 @@ else
fi
echo "Checking the runner"
bash "${ACTIONS_RUNNER_DIRECTORY}/config.sh" --check --url "https://github.com/${GITHUB_REPOSITORY}" --pat ${GH_AUTH_TOKEN}
bash "${ACTIONS_RUNNER_DIRECTORY}/config.sh" --check --url "https://github.com/${GH_ORG}"" --pat ${GH_AUTH_TOKEN}
echo "Configuring runner"
bash "${ACTIONS_RUNNER_DIRECTORY}/config.sh" ${EPHEMERAL_FLAG} \
--unattended \
--disableupdate \
--url "https://github.com/${GITHUB_REPOSITORY}" \
--url "https://github.com/${GH_ORG}"" \
--token "${REPO_TOKEN}" \
--name "$(hostname)" \
--labels "${RUNNER_LABELS}"
--labels "${RUNNER_LABELS}" \
--runnergroup "hmpps-runners-restricted"
echo "Setting the 'ready' flag for Kubernetes liveness probe"
touch /tmp/runner.ready
Expand Down

0 comments on commit 32fb0fa

Please sign in to comment.