Blob Blame History Raw
set -euo pipefail

# Colorful output.
function greenprint {
  echo -e "\033[1;32m${1}\033[0m"

# Get OS and architecture details.
source /etc/os-release
ARCH=$(uname -m)

# Mock configuration file to use for building RPMs.
MOCK_CONFIG="${ID}-${VERSION_ID%.*}-$(uname -m)"

if [[ $ID == centos ]]; then
  MOCK_CONFIG="centos-stream-$(uname -m)"

# The commit this script operates on.
COMMIT=$(git rev-parse HEAD)

# Bucket in S3 where our artifacts are uploaded

# Public URL for the S3 bucket with our artifacts.

# Relative path of the repository โ€“ used for constructing both the local and
# remote paths below, so that they're consistent.

# Directory to hold the RPMs temporarily before we upload them.

# Full URL to the RPM repository after they are uploaded.

# Don't rerun the build if it already exists
if curl --silent --fail --head --output /dev/null "${REPO_URL}/repodata/repomd.xml"; then
  greenprint "๐ŸŽ Repository already exists. Exiting."
  exit 0

# Mock and s3cmd is only available in EPEL for RHEL.
if [[ $ID == rhel || $ID == centos ]] && ! rpm -q epel-release; then
    greenprint "๐Ÿ“ฆ Setting up EPEL repository"
    curl -Ls --retry 5 --output /tmp/epel.rpm \
    sudo rpm -Uvh /tmp/epel.rpm

# Register RHEL if we are provided with a registration script.
if [[ -n "${RHN_REGISTRATION_SCRIPT:-}" ]] && ! sudo subscription-manager status; then
    greenprint "๐Ÿช™ Registering RHEL instance"
    sudo chmod +x "$RHN_REGISTRATION_SCRIPT"

# Install requirements for building RPMs in mock.
greenprint "๐Ÿ“ฆ Installing mock requirements"
sudo dnf -y install createrepo_c mock s3cmd

# Print some data.
greenprint "๐Ÿงฌ Using mock config: ${MOCK_CONFIG}"
greenprint "๐Ÿ“ฆ SHA: ${COMMIT}"
greenprint "๐Ÿ“ค RPMS will be uploaded to: ${REPO_URL}"

# rhel 8.4 will run off of the internal repos and does not have a redhat subscription
if [[ $VERSION_ID == 8.4 ]]; then
    greenprint "๐Ÿ“‹ Updating RHEL 8 mock template for unsubscribed image"
    sudo sed -i '/# repos/q' /etc/mock/templates/rhel-8.tpl
    # remove the subscription check
    sudo sed -i "s/config_opts\['redhat_subscription_required'\] = True/config_opts['redhat_subscription_required'] = False/" /etc/mock/templates/rhel-8.tpl
    cat "$RHEL84_NIGHTLY_REPO" | sudo tee -a /etc/mock/templates/rhel-8.tpl > /dev/null
    # We need triple quotes at the end of the template to mark the end of the repo list.
    echo '"""' | sudo tee -a /etc/mock/templates/rhel-8.tpl

greenprint "๐Ÿ”ง Building source RPM"
git archive --prefix "osbuild-composer-${COMMIT}/" --output "osbuild-composer-${COMMIT}.tar.gz" HEAD
sudo mock -r "$MOCK_CONFIG" --buildsrpm \
  --define "commit ${COMMIT}" \
  --spec ./osbuild-composer.spec \
  --sources "./osbuild-composer-${COMMIT}.tar.gz" \
  --resultdir ./srpm

greenprint "๐ŸŽ Building RPMs"
sudo mock -r "$MOCK_CONFIG" \
    --define "commit ${COMMIT}" \
    --with=tests \
    --resultdir "$REPO_DIR" \

# Change the ownership of all of our repo files from root to our CI user.
sudo chown -R "$USER" "${REPO_DIR%%/*}"

greenprint "๐Ÿงน Remove logs from mock build"
rm "${REPO_DIR}"/*.log

# Create a repo of the built RPMs.
greenprint "โ›“๏ธ Creating dnf repository"
createrepo_c "${REPO_DIR}"

# Upload repository to S3.
greenprint "โ˜ Uploading RPMs to S3"
pushd repo
    s3cmd --acl-public sync . s3://${REPO_BUCKET}/