diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 15df757..064f274 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -12,28 +12,56 @@ permissions: contents: read jobs: + compute-matrix: + runs-on: ubuntu-latest + outputs: + MATRIX: ${{ steps.compute-matrix.outputs.MATRIX }} + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Compute matrix + id: compute-matrix + run: | + MATRIX=$(ci/compute-matrix.sh) + echo "MATRIX=${MATRIX}" | tee --append ${GITHUB_OUTPUT} build: + needs: compute-matrix strategy: + matrix: ${{ fromJSON(needs.compute-matrix.outputs.MATRIX) }} fail-fast: false - matrix: - KUBERNETES_VERSION: - - "1.27" - DRIVER_VERSION: - - "525.125.06" runs-on: ubuntu-latest container: image: hashicorp/packer:latest steps: - - name: Configure AWS Credentials - uses: aws-actions/configure-aws-credentials@v2 - with: - role-to-assume: ${{ vars.PACKER_AWS_ROLE_ARN }} - aws-region: ${{ vars.AWS_REGION }} - - uses: actions/checkout@v3 - - name: Build image - run: | - packer init . - packer build \ - -var "skip_create_ami=${{ inputs.skip_create_ami }}" \ - -var "driver_version=${{ matrix.DRIVER_VERSION }}" \ - . + - run: + - run: | + echo "DRIVER_VERSION=$DRIVER_VERSION" + echo "OS=$OS" + echo "RUNNER_VERSION=$RUNNER_VERSION" + echo "ARCH=$ARCH" + echo "ENV=$ENV" + echo "SKIP_CREATE_AMI=$SKIP_CREATE_AMI" + echo "DOMAIN=$DOMAIN" + echo "PACKER_SOURCE=$PACKER_SOURCE" + env: + DRIVER_VERSION: ${{ matrix.DRIVER_VERSION }} + OS: ${{ matrix.OS }} + RUNNER_VERSION: ${{ matrix.RUNNER_VERSION }} + ARCH: ${{ matrix.ARCH }} + ENV: ${{ matrix.ENV }} + SKIP_CREATE_AMI: ${{ inputs.skip_create_ami }} + DOMAIN: ${{ matrix.domain }} + PACKER_SOURCE: ${{ matrix.packer_source }} + # - name: Configure AWS Credentials + # uses: aws-actions/configure-aws-credentials@v2 + # with: + # role-to-assume: ${{ vars.PACKER_AWS_ROLE_ARN }} + # aws-region: ${{ vars.AWS_REGION }} + # - uses: actions/checkout@v4 + # - name: Build image + # run: | + # packer init . + # packer build \ + # -var "skip_create_ami=${{ inputs.skip_create_ami }}" \ + # -var "driver_version=${{ matrix.DRIVER_VERSION }}" \ + # . diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml index 9fce15b..933bf4e 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/pr.yaml @@ -14,13 +14,8 @@ concurrency: cancel-in-progress: true jobs: - # TODO: update PR build job - # build: - # uses: ./.github/workflows/build.yaml - # with: - # skip_create_ami: true - # secrets: inherit - temp: - runs-on: ubuntu-latest - steps: - - run: echo "temp job" + build: + uses: ./.github/workflows/build.yaml + with: + skip_create_ami: true + secrets: inherit diff --git a/build.pkr.hcl b/build.pkr.hcl index 74c8df6..5eafc24 100644 --- a/build.pkr.hcl +++ b/build.pkr.hcl @@ -22,7 +22,7 @@ build { "NV_ARCH=${var.arch}", "NV_DRIVER_VERSION=${var.driver_version}", "NV_HELPER_SCRIPTS=${local.helpers_directory}", - "NV_VARIANT=${var.variant}", + "NV_VARIANT=${local.variant}", ] scripts = [ diff --git a/ci/compute-matrix.jq b/ci/compute-matrix.jq new file mode 100644 index 0000000..318d52e --- /dev/null +++ b/ci/compute-matrix.jq @@ -0,0 +1,26 @@ +# Checks the current entry to see if it matches the given exclude +def matches($entry; $exclude): + all($exclude | to_entries | .[]; $entry[.key] == .value); + +def filter_excludes($entry; $excludes): + select(any($excludes[]; matches($entry; .)) | not); + +def lists2dict($keys; $values): + reduce range($keys | length) as $ind ({}; . + {($keys[$ind]): $values[$ind]}); + +def add_config($entry; $config): + $entry + $config[$entry.ENV]; + +def compute_matrix($matrix; $config): + ($matrix.exclude // []) as $excludes | + $matrix | del(.exclude) | + keys_unsorted as $matrix_keys | + to_entries | + map(.value) | + [ + combinations | + lists2dict($matrix_keys; .) | + filter_excludes(.; $excludes) | + add_config(.; $config) + ] | + {include: .}; diff --git a/ci/compute-matrix.sh b/ci/compute-matrix.sh new file mode 100755 index 0000000..d794d38 --- /dev/null +++ b/ci/compute-matrix.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -euo pipefail + +# This script computes the matrix for the GHAs matrix job. The values in +# config.yaml are injected into each matrix entry according to its `ENV` key. + +yq -o json \ + eval-all \ + '{"matrix": select(filename == "matrix.yaml")} + {"config": select(filename == "config.yaml")}' \ + matrix.yaml config.yaml | \ + jq -c 'include "ci/compute-matrix"; compute_matrix(.matrix; .config)' diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..5981f35 --- /dev/null +++ b/config.yaml @@ -0,0 +1,6 @@ +aws: + domain: arc-eks + packer_source: amazon-ebs +premise: + domain: arc-nvks + packer_source: qemu diff --git a/locals.pkr.hcl b/locals.pkr.hcl index 7f27f25..79dbab1 100644 --- a/locals.pkr.hcl +++ b/locals.pkr.hcl @@ -1,6 +1,7 @@ locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") - driver_major_version = var.variant == "cpu" ? null : "${split(".", var.driver_version)[0]}" + variant = var.driver_version == "" ? "cpu" : "gpu" + driver_major_version = local.variant == "cpu" ? null : "${split(".", var.driver_version)[0]}" instance_type = var.arch == "amd64" ? "m7i.large" : "m7g.large" helpers_directory = "/home/runner/helpers" } diff --git a/matrix.yaml b/matrix.yaml index d4df16d..4d47c54 100644 --- a/matrix.yaml +++ b/matrix.yaml @@ -3,6 +3,8 @@ OS: # - win DRIVER_VERSION: + # keep this blank entry. an empty driver version corresponds to CPU machines + - "" - "525.147.05" RUNNER_VERSION: @@ -10,17 +12,10 @@ RUNNER_VERSION: ARCH: - amd64 - - arm64 + # - arm64 -VARIANT: - - cpu - - gpu +ENV: + - aws + # - premise -FORMAT: - - qcow2 - - ami - -# Specify additional excludes here. -# ci/generate-matrix.yaml will automatically exclude 'cpu' variants and -# `driver_version` combinations. exclude: [] diff --git a/source.pkr.hcl b/source.pkr.hcl index 61a8acb..3415683 100644 --- a/source.pkr.hcl +++ b/source.pkr.hcl @@ -41,7 +41,7 @@ source "amazon-ebs" "ubuntu" { "driver-version" = local.driver_major_version "os" = var.os "runner-version" = var.runner_version - "variant" = var.variant + "variant" = local.variant }: k => v if v != null } } diff --git a/variables.auto.pkrvars.hcl.sample b/variables.auto.pkrvars.hcl.sample index 5c80b5d..a92a8d4 100644 --- a/variables.auto.pkrvars.hcl.sample +++ b/variables.auto.pkrvars.hcl.sample @@ -3,4 +3,3 @@ // os = "linux" runner_version = "2.311.0" // skip_create_ami = true -// variant = "gpu" diff --git a/variables.pkr.hcl b/variables.pkr.hcl index 368968c..f86f53f 100644 --- a/variables.pkr.hcl +++ b/variables.pkr.hcl @@ -46,13 +46,3 @@ variable "skip_create_ami" { type = bool default = true } - -variable "variant" { - type = string - default = "cpu" - - validation { - condition = can(regex("^[cg]pu$", var.variant)) - error_message = "The variant value must be either 'cpu' or 'gpu'." - } -}