From 03d1369f5f2cbae29844c6bbecce3cd1f850c7a0 Mon Sep 17 00:00:00 2001 From: Philip Welz Date: Fri, 26 Jan 2024 14:39:26 +0100 Subject: [PATCH] feat: initial commit --- .github/workflows/action.yaml | 20 +++++++++ .github/workflows/docker-build.yaml | 64 +++++++++++++++++++++++++++++ gh-runner/Dockerfile | 15 +++++++ gh-runner/entrypoint.sh | 29 +++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 .github/workflows/action.yaml create mode 100644 .github/workflows/docker-build.yaml create mode 100644 gh-runner/Dockerfile create mode 100644 gh-runner/entrypoint.sh diff --git a/.github/workflows/action.yaml b/.github/workflows/action.yaml new file mode 100644 index 0000000..f19f955 --- /dev/null +++ b/.github/workflows/action.yaml @@ -0,0 +1,20 @@ +name: Action + +on: + workflow_dispatch: + push: + branches: + - main + +defaults: + run: + working-directory: ./ + shell: bash + +jobs: + build: + name: gh-runner + uses: ./.github/workflows/docker-build.yaml + with: + REGISTRY: ghcr.io + ORGANISATION_NAME: philwelz \ No newline at end of file diff --git a/.github/workflows/docker-build.yaml b/.github/workflows/docker-build.yaml new file mode 100644 index 0000000..374de02 --- /dev/null +++ b/.github/workflows/docker-build.yaml @@ -0,0 +1,64 @@ +name: Build & Publish to Registry + +on: + workflow_call: + inputs: + ### Values from CICD Workflow ### + REGISTRY: + required: true + type: string + description: 'The Environemnt which should be used.' + ORGANISATION_NAME: + type: string + required: true + description: 'The Subscription ID which should be used.' + +env: + IMAGE: '${{ inputs.REGISTRY }}/${{ inputs.ORGANISATION_NAME }}/gh-runner' + +jobs: + docker-build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v3.0.0 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3.0.0 + with: + registry: ${{ inputs.REGISTRY }} + username: ${{ github.actor }} + password: ${{ github.token }} + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5.5.0 + with: + images: | + ${{ env.IMAGE }} + tags: | + type=raw,latest + type=sha,prefix=,suffix=,short=true + + - name: Build and push the fred version of the WebApp + uses: docker/build-push-action@v5.1.0 + with: + builder: ${{ steps.buildx.outputs.name }} + platforms: linux/amd64 + push: true + context: ./gh-runner + file: ./gh-runner/Dockerfile + tags: ${{ steps.meta.outputs.tags }} + + - name: Display image tag in summary + run: | + echo "### Build & release! :rocket:" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "Released image:" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "- ${{steps.meta.outputs.tags}}" >> $GITHUB_STEP_SUMMARY \ No newline at end of file diff --git a/gh-runner/Dockerfile b/gh-runner/Dockerfile new file mode 100644 index 0000000..545a189 --- /dev/null +++ b/gh-runner/Dockerfile @@ -0,0 +1,15 @@ +FROM ghcr.io/actions/actions-runner:2.311.0 + +USER root + +# install curl and jq +RUN apt-get update && apt-get install -y curl jq && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + +COPY entrypoint.sh ./entrypoint.sh +RUN chmod +x ./entrypoint.sh + +USER runner + +ENTRYPOINT ["./entrypoint.sh"] \ No newline at end of file diff --git a/gh-runner/entrypoint.sh b/gh-runner/entrypoint.sh new file mode 100644 index 0000000..3d1b62e --- /dev/null +++ b/gh-runner/entrypoint.sh @@ -0,0 +1,29 @@ +#!/bin/sh -l + +# Retrieve a short lived runner registration token using the PAT +REGISTRATION_TOKEN="$(curl -X POST -fsSL \ + -H 'Accept: application/vnd.github.v3+json' \ + -H "Authorization: Bearer $GITHUB_PAT" \ + -H 'X-GitHub-Api-Version: 2022-11-28' \ + "$REGISTRATION_TOKEN_API_URL" \ + | jq -r '.token')" + +# Configure +if [[ "${ephemeralRunner}" == "true" ]]; then + + ./config.sh \ + --url $REPO_URL \ + --token $REGISTRATION_TOKEN \ + --unattended \ + --ephemeral \ + && ./run.sh + +else + + ./config.sh \ + --url $REPO_URL \ + --token $REGISTRATION_TOKEN \ + --unattended \ + && ./run.sh + +fi \ No newline at end of file