Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: image factory runner images #2

Merged
merged 66 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
9b4ee60
add: image factory runner container image
Sep 18, 2024
425b373
add: test runner workflow
Sep 18, 2024
07aa5c1
Revert "add: test runner workflow"
Sep 18, 2024
4f48621
Merge branch 'main' into feature/github-container-runners-base
Sep 18, 2024
8e1d92e
add: podman settings and packer
Sep 18, 2024
d70d2f3
add: initial packer files
Sep 18, 2024
8dc514b
add: commands for image definition
Sep 19, 2024
baf1e9e
add: trivy software
Sep 19, 2024
ae100ca
Merge branch 'main' into feature/github-container-runners-base
Sep 19, 2024
8c2a01f
feat: change runner arch
Sep 19, 2024
aeaabec
add: runner name
Sep 19, 2024
1bee20d
update azure resource names
Sep 19, 2024
0d54086
add: test action
Sep 19, 2024
ecbdcb6
add: test docker build
Sep 19, 2024
79332fd
add: test docker runner build
Sep 19, 2024
252b76b
add: docker build action
Sep 19, 2024
42df7aa
revert test workflow
Sep 19, 2024
28909c0
rename action
Sep 19, 2024
4c5f903
add: push triggers
Sep 19, 2024
420b40d
add: test image tag
Sep 19, 2024
64b26f0
add: image tag equal to run number
Sep 19, 2024
8739d66
comment out azure login
Sep 19, 2024
56aebfb
add: latest tag
Sep 19, 2024
612baa5
add: docker tag
Sep 19, 2024
a101c8c
add: tag with latest only on main
Sep 19, 2024
f04d5ee
fix: latest tag for main
Sep 19, 2024
635a19b
add: acr test
Sep 19, 2024
8e46a26
fix: registry var
Sep 19, 2024
e1a04b5
remove docker hub login
Sep 19, 2024
4e5201a
add: test jinja action packer
Sep 19, 2024
0818f24
fix: push path
Sep 19, 2024
b16bb81
fix: push path
Sep 19, 2024
ff83345
add: test jinja action
Sep 19, 2024
83bbe84
test jinja action
Sep 19, 2024
68abbf5
add: azure vars
Sep 19, 2024
4da204d
add: repo vars
Sep 19, 2024
9f0bee7
remove image folder
Sep 19, 2024
323c4c9
remove vars file
Sep 19, 2024
8494e25
add: azure login
Sep 19, 2024
da4f38e
format azure credentials
Sep 19, 2024
bee45e2
add: create image definition
Sep 19, 2024
700886c
test supress errors
Sep 19, 2024
333ca87
test action
Sep 19, 2024
7e3db5e
test action
Sep 19, 2024
3e3386e
test az action
Sep 19, 2024
c9b8fdd
test custom parameters
Sep 19, 2024
98e3737
add: packer init
Sep 19, 2024
83b86f4
fix: typo
Sep 19, 2024
7642253
fix: typo
Sep 19, 2024
b23c11a
add: packer build
Sep 19, 2024
b8b623d
fix: typo
Sep 19, 2024
134394b
fix: typo
Sep 19, 2024
18dd0e9
test packer vars from env
Sep 19, 2024
2b2ef75
fix: remove plugins warning
Sep 19, 2024
57f9ae4
update packer build command
Sep 19, 2024
c13a13f
fix: gallery image version
Sep 19, 2024
86913ab
test action
Sep 19, 2024
a48e1de
fix: image name
Sep 19, 2024
709cb0e
fix: push triggers
Sep 19, 2024
d4e6918
add: packer script
Sep 19, 2024
ee615f3
fix: image name
Sep 19, 2024
1c32b8f
add: vm initialization script
Sep 19, 2024
71d305d
fix: debian frontend
Sep 19, 2024
446e7a6
add inline_shebang
Sep 19, 2024
8b7d7ad
add new line [ci skip]
Sep 19, 2024
6289304
fix: trivy install
Sep 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/docker-build-if-runner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Build Image Factory Runner

on:
push:
paths:
- ".github/workflows/docker-build-if-runner.yml"
- "images/docker/image-factory-runner/**"
workflow_dispatch:

jobs:
docker-build:
runs-on: ubuntu-latest

env:
IMAGE_NAME: "image-factory-runner"
IMAGE_TAG: "${{ github.run_number }}"
REGISTRY: ${{ secrets.REGISTRY }}

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Log in to Azure Container Registry
uses: azure/docker-login@v1
with:
login-server: ${{ secrets.REGISTRY }}
username: ${{ secrets.ACR_USERNAME }}
password: ${{ secrets.ACR_PASSWORD }}

- name: Build Docker Image
run: docker build -t $REGISTRY/$IMAGE_NAME:$IMAGE_TAG images/docker/image-factory-runner

- name: Push Docker image
run: |
docker push $REGISTRY/$IMAGE_NAME:$IMAGE_TAG

- name: Tag and push Docker image as latest
# if: github.ref == 'refs/heads/main' # TODO: uncomment once we are done with development
run: |
docker tag $REGISTRY/$IMAGE_NAME:$IMAGE_TAG $REGISTRY/$IMAGE_NAME:latest
docker push $REGISTRY/$IMAGE_NAME:latest
95 changes: 95 additions & 0 deletions .github/workflows/packer-build-if-vm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: Build Image Factory VM

on:
push:
paths:
- ".github/workflows/packer-build-if-vm.yml"
- "images/packer/image-factory-vm/**"
workflow_dispatch:

jobs:

packer-build:
runs-on: ubuntu-latest

env:
AZ_CLI_VERSION: 2.64.0
PACKER_VERSION: 1.9.4

AZURE_LOCATION: ${{ vars.AZURE_LOCATION }}
AZURE_RESOURCE_GROUP: ${{ vars.AZURE_RESOURCE_GROUP }}
AZURE_ACG: ${{ vars.AZURE_ACG }}
AZURE_CREDENTIALS: |
{
"clientId": "${{ secrets.AZURE_CLIENT_ID }}",
"clientSecret": "${{ secrets.AZURE_CLIENT_SECRET }}",
"subscriptionId": "${{ secrets.AZURE_SUBSCRIPTION_ID }}",
"tenantId": "${{ secrets.AZURE_TENANT_ID }}"
}

IMAGE_NAME: "image-factory-vm"
IMAGE_PUBLISHER: "wp10-image-factory"
IMAGE_OFFER: "wp10-image-factory-vm"
IMAGE_SKU: "v1"
IMAGE_OS_TYPE: "linux"
IMAGE_VERSION: "${{ github.run_number }}"

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Azure Login
uses: azure/login@v2
with:
creds: ${{ env.AZURE_CREDENTIALS }}

- name: Create Azure Image Definition
uses: azure/cli@v2
with:
azcliversion: ${{ env.AZ_CLI_VERSION }}
inlineScript: |
set +e
az sig image-definition show \
--resource-group "$AZURE_RESOURCE_GROUP" \
--gallery-name "$AZURE_ACG" \
--gallery-image-definition "$IMAGE_NAME" \
--query "name" -o tsv
az_exit_code=$?
set -e
if [ $az_exit_code -eq 3 ]; then
echo "Image definition does not exist. Creating it..."
az sig image-definition create \
--resource-group "$AZURE_RESOURCE_GROUP" \
--gallery-name "$AZURE_ACG" \
--gallery-image-definition "$IMAGE_NAME" \
--publisher "$IMAGE_PUBLISHER" \
--offer "$IMAGE_OFFER" \
--sku "$IMAGE_SKU" \
--os-type "$IMAGE_OS_TYPE"
else
echo "Image definition '$IMAGE_NAME' already exists."
fi

- name: Template Packer vars file
uses: cuchi/[email protected]
with:
template: images/packer/image-factory-vm/values.auto.pkrvars.hcl.j2
output_file: images/packer/image-factory-vm/values.auto.pkrvars.hcl

- name: Setup Packer
uses: hashicorp/setup-packer@main
with:
version: ${{ env.PACKER_VERSION }}

- name: Packer Init
run: packer init images/packer/image-factory-vm

- name: Packer Build
run: |
packer build \
-var "client_id=${{ secrets.AZURE_CLIENT_ID }}" \
-var "client_secret=${{ secrets.AZURE_CLIENT_SECRET }}" \
-var "tenant_id=${{ secrets.AZURE_TENANT_ID }}" \
-var "subscription_id=${{ secrets.AZURE_SUBSCRIPTION_ID }}" \
images/packer/image-factory-vm

87 changes: 87 additions & 0 deletions images/docker/image-factory-runner/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
FROM ubuntu:22.04

ARG RUNNER_VERSION=2.319.1
ARG RUNNER_ARCH=x64

ENV DEBIAN_FRONTEND=noninteractive \
USER_NAME=gha \
USER_GROUP=gha \
USER_HOME=/opt/gha

# Update and install packadges and dependencies
RUN apt-get update -y && \
apt-get upgrade -y && \
apt-get install --no-install-recommends -y \
apt-transport-https \
buildah \
ca-certificates \
curl \
git \
gnupg \
jq \
libcap2-bin \
lsb-release \
podman \
slirp4netns \
software-properties-common \
unzip \
vim \
wget && \
rm -rf /var/lib/apt/lists/*


# Create runner user
RUN mkdir -p ${USER_HOME} && \
useradd -r -d ${USER_HOME} -s /sbin/nologin -c "GitHub Actions User" ${USER_NAME} && \
chown ${USER_GROUP}:${USER_NAME} ${USER_HOME}

# Add podman configuration file(s)
ADD files/containers.conf /etc/containers/containers.conf
RUN chmod 644 /etc/containers/containers.conf

# Setup for rootless podman
RUN usermod --add-subuids 100000-165535 --add-subgids 100000-165535 ${USER_NAME}

# Enable rootless podman to setup namespace using newuidmap
# - referrence: https://github.com/containers/podman/issues/2788#issuecomment-1016301663
RUN chmod u-s /usr/bin/newuidmap /usr/bin/newgidmap && \
setcap cap_setuid+eip /usr/bin/newuidmap && \
setcap cap_setgid+eip /usr/bin/newgidmap

# Change to runner workdir
WORKDIR ${USER_HOME}

# Download GitHub Actions runner
RUN mkdir actions-runner && \
cd actions-runner && \
curl -o actions-runner-linux-${RUNNER_ARCH}-${RUNNER_VERSION}.tar.gz -L https://github.com/actions/runner/releases/download/v${RUNNER_VERSION}/actions-runner-linux-${RUNNER_ARCH}-${RUNNER_VERSION}.tar.gz && \
tar xzf ./actions-runner-linux-${RUNNER_ARCH}-${RUNNER_VERSION}.tar.gz && \
chown -R ${USER_NAME} ${USER_HOME}

# Install additional dependencies
RUN actions-runner/bin/installdependencies.sh

# Add start script and make it executable
ADD scripts/start-github-runner.sh start-github-runner.sh
RUN chmod +x start-github-runner.sh

# Install packer
RUN curl -fsSL https://apt.releases.hashicorp.com/gpg | apt-key add - && \
apt-add-repository "deb [arch=${RUNNER_ARCH}] https://apt.releases.hashicorp.com $(lsb_release -cs) main" && \
apt-get update -y && \
apt-get install packer

# Install Azure CLI
RUN curl -sL https://aka.ms/InstallAzureCLIDeb | bash

# Install Trivy
RUN wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | apt-key add - && \
echo deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main | tee -a /etc/apt/sources.list.d/trivy.list && \
apt-get update && \
apt-get install trivy

# Set runner user
USER ${USER_NAME}

# Set start script as an entrypoint
ENTRYPOINT ["./start-github-runner.sh"]
2 changes: 2 additions & 0 deletions images/docker/image-factory-runner/files/containers.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[containers]
# further configuration will be needed to support podman run command
28 changes: 28 additions & 0 deletions images/docker/image-factory-runner/scripts/start-github-runner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

GITHUB_OWNER=$GITHUB_OWNER
GITHUB_REPOSITORY=$GITHUB_REPOSITORY
GITHUB_TOKEN=$(cat /.pat/.token)
GITHUB_RUNNER_NAME="image-factory-runner"

echo "Getting runner registration token from GitHub..."
REG_TOKEN=$(curl -sX POST -H "Accept: application/vnd.github.v3+json" -H "Authorization: token ${GITHUB_TOKEN}" https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPOSITORY}/actions/runners/registration-token | jq .token --raw-output)

# Add this part once PAT token generation is set up on the runner host VM
# - new PAT token will be generated as a pre task to each runner container start
#
# echo "Removing PAT token from runner filesystem"
# rm -rf /.pat/.token

echo "Connect runner to GitHub:"
cd actions-runner
./config.sh \
--url https://github.com/${GITHUB_OWNER}/${GITHUB_REPOSITORY} \
--token ${REG_TOKEN} \
--name ${RUNNER_NAME} \
--unattended \
--ephemeral \
--replace \
--disableupdate

./run.sh & wait $!
31 changes: 31 additions & 0 deletions images/packer/image-factory-vm/azure.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
source "azure-arm" "vm" {
client_id = var.client_id
client_secret = var.client_secret
subscription_id = var.subscription_id
tenant_id = var.tenant_id
location = var.location

managed_image_name = "${var.gallery_image_name}-${formatdate("DD-MMM-YYYY-hh-mm-ss", timestamp())}"
managed_image_resource_group_name = var.resource_group

communicator = "ssh"
os_type = "Linux"
image_publisher = "Canonical"
image_offer = "0001-com-ubuntu-server-jammy"
image_sku = "22_04-lts-gen2"

vm_size = "Standard_B2ms"

public_ip_sku = "Standard"

shared_image_gallery_destination {
subscription = var.subscription_id
resource_group = var.resource_group
gallery_name = var.gallery_name
image_name = var.gallery_image_name
image_version = var.gallery_image_version
target_region {
name = var.location
}
}
}
21 changes: 21 additions & 0 deletions images/packer/image-factory-vm/build.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
build {
sources = [
"source.azure-arm.vm"
]

provisioner "file" {
source = "images/packer/image-factory-vm/scripts/setup-vm.sh"
destination = "/tmp/setup.sh"
}

provisioner "shell" {
execute_command = "chmod +x {{ .Path }}; {{ .Vars }} sudo -E sh '{{ .Path }}'"

inline = [
"chmod +x /tmp/setup.sh",
"/tmp/setup.sh"
]

inline_shebang = "/bin/sh -x"
}
}
8 changes: 8 additions & 0 deletions images/packer/image-factory-vm/plugins.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
packer {
required_plugins {
azure = {
source = "github.com/hashicorp/azure"
version = "~> 1"
}
}
}
43 changes: 43 additions & 0 deletions images/packer/image-factory-vm/scripts/setup-vm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

set -e

# Set the DEBIAN_FRONTEND to noninteractive
export DEBIAN_FRONTEND=noninteractive

# Update and upgrade the system
apt-get update
apt-get upgrade -y

# Deprovision user
/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync

# Install required packages
apt-get install --no-install-recommends -y \
apt-transport-https \
buildah \
ca-certificates \
curl \
git \
gnupg \
jq \
libcap2-bin \
lsb-release \
podman \
slirp4netns \
software-properties-common \
unzip \
vim \
wget

# Clean up
rm -rf /var/lib/apt/lists/*

# Install Azure CLI
curl -sL https://aka.ms/InstallAzureCLIDeb | bash

# Set up Trivy repository and install Trivy
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | apt-key add -
echo deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main | tee -a /etc/apt/sources.list.d/trivy.list
apt-get update
apt-get install -y trivy
5 changes: 5 additions & 0 deletions images/packer/image-factory-vm/values.auto.pkrvars.hcl.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
location = "{{ env['AZURE_LOCATION'] }}"
resource_group = "{{ env['AZURE_RESOURCE_GROUP'] }}"
gallery_name = "{{ env['AZURE_ACG'] }}"
gallery_image_name = "{{ env['IMAGE_NAME'] }}"
gallery_image_version = "1.0.{{ env['IMAGE_VERSION'] }}"
Loading