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

Activate CI builds #5

Merged
merged 15 commits into from
Nov 1, 2023
93 changes: 93 additions & 0 deletions .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
name: Build and Deploy
on:
push:
branches:
- main

jobs:

define_images:
# Define a build matrix based on all changed *.hcl files so we can run packer only for image definitions that have actually changed
# So if e.g. ubuntu/focal/variables.auto.pkrvars.hcl or ubuntu/focal/src-ubuntu.pkr.hcl changes, add "ubuntu/focal" to the matrix
# Also ensure that if an image template (for instance, the general template for ubuntu images 'ubuntu/src-ubuntu.pkr.hcl') changes,
# an entry is added to the matrix for each image definition that relies on (i.e. symlinks) it.
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
path: "${{ github.repository }}"
- name: Get changed image definitions
# Find changed .hcl files in all <os>/<version> directories
id: changed-images-yaml
uses: tj-actions/changed-files@v39
with:
dir_names: true
dir_names_max_depth: 3
quotepath: false
files_yaml: |
images:
- '*/*/*.hcl'
- name: Get changed image definition templates
# Find all changed base templates in all <os>/ directories
id: changed-symlink-yaml
uses: tj-actions/changed-files@v39
with:
quotepath: false
files_yaml: |
symlinks:
- '*/*.hcl'
- name: Get image directories based on changed templates
# For every changed template in an <os> directory, collect all <os>/<version> directories that contain a symlink to the template.
# For instance, if ubuntu/src-ubunut.pkr.hcl changes, find all subdirectories of 'ubuntu' that contain links to that file
# -> the result would be e.g. 'ubuntu/focal ubuntu/jammy ubuntu/focal_desktop'
id: get-symlink-dirs
working-directory: "${{ github.repository }}"
run: |
for file in ${{ steps.changed-symlink-yaml.outputs.symlinks_all_changed_files }}; do
dir=$(dirname $file)
links=$(find "$dir" -type l)
for link in $links; do
if [[ $(readlink -f "$link") == $(realpath "$file") ]]; then
CHANGED="$CHANGED $(dirname $link)"
fi
done
done
echo changed_dirs="$CHANGED" >> "$GITHUB_OUTPUT"
- id: set-images-matrix
run: | # Add together the directories found in the changed-images-yaml and get-symlink-dirs steps into a single matrix
ALL_DIRS="${{ steps.changed-images-yaml.outputs.images_all_changed_files }} ${{ steps.get-symlink-dirs.outputs.changed_dirs }}"
MATRIX=($(echo "$ALL_DIRS" | xargs -n 1 | sort -u)) # Construct a unique array out of ALL_DIRS
MATRIX=$(printf '%s\n' "${MATRIX[@]}" | jq -R . | jq -c -s .) # Construct a valid json array out of the bash array
echo matrix={\"images\": $MATRIX} >> $GITHUB_OUTPUT
outputs:
images_matrix: ${{ steps.set-images-matrix.outputs.matrix }}

build_and_deploy:
needs: define_images
runs-on: ubuntu-latest
if: ${{ needs.define_images.outputs.images_matrix != '[]' && needs.define_images.outputs.images_matrix != '' }}
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.define_images.outputs.images_matrix) }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build image
id: build-image
run: IMG=${{ matrix.images }} ./pack.sh docker
- name: Get image name and tag # grep in the image directory's variables file to find the image name/tag to push to
id: get-image-name
run: |
echo "docker_repo=`grep docker_repo ${{ matrix.images }}/variables.auto.pkrvars.hcl | awk '{print $3}'`" >> "$GITHUB_OUTPUT"
- name: Push image
run: docker push ${{ steps.get-image-name.outputs.docker_repo }}
10 changes: 5 additions & 5 deletions pack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ then
exit 1
fi

if [ -z $IMG ]
if [ -z "$IMG" ]
then
IMG="ubuntu/focal"
fi

if [ -z $ARCH ]
if [ -z "$ARCH" ]
then
ARCH="linux/amd64"
else
Expand All @@ -26,10 +26,10 @@ TARGETS=()

for i in "$@" ; do
if [[ $i == "docker" ]] ; then
TARGETS+=($DOCKER_SRC)
TARGETS+=("$DOCKER_SRC")
fi
if [[ $i == "vagrant" ]] ; then
TARGETS+=($VAGRANT_SRC)
TARGETS+=("$VAGRANT_SRC")
fi
done

Expand All @@ -43,6 +43,6 @@ else
printf -v joined_targets '%s,' "${TARGETS[@]}"
fi

CMD="packer fmt $IMG && packer build -var 'enabled_sources=[$joined_targets]' -var 'target_arch=$ARCH' $IMG"
CMD="packer init $IMG && packer fmt $IMG && packer build -var 'enabled_sources=[$joined_targets]' -var 'target_arch=$ARCH' $IMG"
echo "Running: $CMD"
eval "$CMD";
3 changes: 2 additions & 1 deletion ubuntu/focal/variables.auto.pkrvars.hcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
img_name = "src-ubuntu_focal"
docker_repo = "ghcr.io/utrechtuniversity/src-test-workspace:ubuntu_focal"
img_tag = "ubuntu_focal"
docker_base_img = "ubuntu:focal"
vagrant_base_img = "ubuntu/focal64"
extra_packages = ""
Expand Down
3 changes: 2 additions & 1 deletion ubuntu/focal_desktop/variables.auto.pkrvars.hcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
img_name = "src-ubuntu_focal_desktop"
docker_repo = "ghcr.io/utrechtuniversity/src-test-workspace:ubuntu_focal-desktop"
img_tag = "ubuntu_focal-desktop"
docker_base_img = "ubuntu:focal"
vagrant_base_img = "ubuntu/focal64"
extra_packages = "xfce4"
Expand Down
3 changes: 2 additions & 1 deletion ubuntu/jammy/variables.auto.pkrvars.hcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
img_name = "src-ubuntu_jammy"
docker_repo = "ghcr.io/utrechtuniversity/src-test-workspace:ubuntu_jammy"
img_tag = "ubuntu_jammy"
docker_base_img = "ubuntu:jammy"
vagrant_base_img = "ubuntu/jammy64"
extra_packages = ""
Expand Down
47 changes: 41 additions & 6 deletions ubuntu/src-ubuntu.pkr.hcl
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
variable "source_repo" {
default = "https://github.com/utrechtuniversity/src-test-workspace"
type = string
}

variable "docker_repo" {
default = ""
type = string
}

variable "ansible_host" {
default = "packer-src-workspace"
default = "packer-src"
type = string
}

Expand Down Expand Up @@ -80,10 +90,32 @@ variable "vagrant_base_img" {
}

variable "img_name" {
default = "src-workspace"
default = "src-test-workspace"
type = string
}

variable "img_tag" {
default = ""
type = string
}

local "ansible_host" {
expression = "${var.ansible_host}-${var.img_tag}"
}

local "dummy_plugin_args" {
expression = {
"remote_ansible_version" = "${var.workspace_ansible_version}",
"remote_plugin" = {
"script_type" = "Ansible PlayBook",
"script_folder" = "../dummy-plugin",
"path" = "dummy.yml",
"parameters" = {},
"arguments" = "-i 127.0.0.1,"
}
}
}

packer {
required_plugins {
docker = {
Expand All @@ -106,7 +138,10 @@ source "docker" "ubuntu" {
platform = var.target_arch
pull = true
commit = true
run_command = ["-d", "-i", "-t", "--name", var.ansible_host, "{{.Image}}", "/bin/bash"]
run_command = ["-d", "-i", "-t", "--name", local.ansible_host, "{{.Image}}", "/bin/bash"]
changes = [
"LABEL org.opencontainers.image.source=${var.source_repo}"
]
}

source "vagrant" "ubuntu" {
Expand Down Expand Up @@ -141,7 +176,7 @@ build {
playbook_file = "./plugin-os/plugin-os.yml"
extra_arguments = concat(var.common_ansible_args, [
"--extra-vars",
"rsc_os_ip=127.0.0.1 rsc_os_fqdn=${var.ansible_host}.test",
"rsc_os_ip=127.0.0.1 rsc_os_fqdn=${local.ansible_host}.test",
])
}
# End Vagrant specific provisioning
Expand All @@ -162,7 +197,7 @@ build {
playbook_file = "./plugin-external-plugin/plugin-external-plugin.yml"
extra_arguments = concat(var.common_ansible_args, [
"--extra-vars",
"{'remote_ansible_version': '${var.workspace_ansible_version}', 'remote_plugin': {'script_type': 'Ansible PlayBook', 'script_folder': '../dummy-plugin', 'path': 'dummy.yml', 'parameters': {}, 'arguments': '-i 127.0.0.1,'}}",
jsonencode(local.dummy_plugin_args),
])
}

Expand All @@ -174,7 +209,7 @@ build {

post-processor "docker-tag" {
except = ["vagrant.ubuntu"]
repository = var.img_name
repository = var.docker_repo
}
post-processor "shell-local" {
except = ["vagrant.ubuntu"]
Expand Down