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 is only available in EPEL for RHEL.
if [[ $ID == rhel ]] && ! 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 make mock python3-pip rpm-build

# Install s3cmd if it is not present.
if ! s3cmd --version > /dev/null 2>&1; then
    greenprint "๐Ÿ“ฆ Installing s3cmd"
    sudo pip3 -q install s3cmd

# Jenkins sets a workspace variable as the root of its working directory.

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

# Jenkins takes the proposed PR and merges it onto master. Although this
# creates a new SHA (which is slightly confusing), it ensures that the code
# merges properly against master and it tests the code against the latest
# commit in master, which is certainly good.
POST_MERGE_SHA=$(git rev-parse --short HEAD)

# Bucket in S3 where our artifacts are uploaded

# Public URL for the S3 bucket with our artifacts.

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

# Maintain a directory for the master branch that always contains the latest
# RPM packages.

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

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

# Build source RPMs.
greenprint "๐Ÿ”ง Building source RPMs."
make srpm

if [[ $VERSION_ID == 8.2 ]]; then
    # osbuild does not exist in 8.2, so build from submodule
    make -C osbuild srpm

# Update the mock configs if we are on 8.3 beta.
if [[ $VERSION_ID == 8.3 ]]; then
    # Remove the existing (non-beta) repos from the template.
    sudo sed -i '/# repos/q' /etc/mock/templates/rhel-8.tpl

    # Add the enabled repos to the template.
    cat /etc/yum.repos.d/redhat.repo | sudo tee -a /etc/mock/templates/rhel-8.tpl

    # 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

# Compile RPMs in a mock chroot
greenprint "๐ŸŽ Building RPMs with mock"
if [[ $VERSION_ID == 8.2 ]]; then
    sudo mock -v -r $MOCK_CONFIG --resultdir $REPO_DIR --with=tests \
        rpmbuild/SRPMS/*.src.rpm osbuild/rpmbuild/SRPMS/*.src.rpm
    sudo mock -v -r $MOCK_CONFIG --resultdir $REPO_DIR --with=tests \

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

# Move the logs out of the way.
greenprint "๐Ÿงน Retaining logs from mock build"

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

# Copy the current build to the latest directory.
cp -arv ${REPO_DIR}/ ${REPO_DIR_LATEST}/

# Remove the previous latest build for this branch.
# Don't fail if the path is missing.
s3cmd --recursive rm s3://${REPO_BUCKET}/${JOB_NAME}/latest/${ID}${VERSION_ID//./}_${ARCH} || true

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

# Create a repository file.
greenprint "๐Ÿ“œ Generating dnf repository file"
tee osbuild-mock.repo << EOF
name=osbuild mock ${JOB_NAME}-${POST_MERGE_SHA} ${ID}${VERSION_ID//./}
# Default dnf repo priority is 99. Lower number means higher priority.