#!/bin/bash # AppSRE runs this script to build an ami and share it with an account set -exv COMMIT_SHA="${COMMIT_SHA:-$(git rev-parse HEAD)}" COMMIT_BRANCH="${COMMIT_BRANCH:-$(git rev-parse --abbrev-ref HEAD)}" SKIP_CREATE_AMI="${SKIP_CREATE_AMI:-false}" BUILD_RPMS="${BUILD_RPMS:-true}" # RHEL workers build their own rpms. ANSIBLE_TAGS="${ANSIBLE_TAGS:-rhel}" # Build rhel only PACKER_ONLY_EXCEPT="${PACKER_ONLY_EXCEPT:---only=amazon-ebs.rhel-9-x86_64,amazon-ebs.rhel-9-aarch64}" if [ -n "$GIT_BRANCH" ]; then # Use jenkins CI variables if available COMMIT_BRANCH="${GIT_BRANCH#*/}" fi # decide whether podman or docker should be used if which podman 2>/dev/null >&2; then CONTAINER_RUNTIME=podman elif which docker 2>/dev/null >&2; then CONTAINER_RUNTIME="docker --config=$PWD/.docker" else echo No container runtime found, install podman or docker. exit 2 fi function greenprint { echo -e "\033[1;32m[$(date -Isecond)] ${1}\033[0m" } function cleanup { set +e $CONTAINER_RUNTIME rmi "packer:$COMMIT_SHA" } trap cleanup EXIT # prepare ansible inventories function write_inventories { for item in templates/packer/ansible/inventory/*; do local distro_arch distro_arch="$(basename "$item")" # strip arch local distro="${distro_arch%-*}" # write rpmrepo_distribution variable local rpmrepo_distribution="$distro" if [[ $rpmrepo_distribution == rhel-9 ]]; then rpmrepo_distribution=rhel-9-cdn fi cat >"$item/group_vars/all.yml" <