Thursday, March 27, 2014

Creating local yum repository and using it

Sometimes it is needed to create a local yum repository when you want yum to download packages from a local repository instead of the main repository. It is very much needed when you don't want to commit your changes to a branch which is being developed by other teams. This approach is very useful when some of the packages are available in both the repositories, but you only want yum to download from your local repository for those packages.

Step-by-step guide

Before following the steps, you should have your jenkins setup in a VM (i.e. avoid the main jenkins server). Perform the steps in the same vm.

  • Creating the yum repo
    • Install createrepo: "yum install createrepo"
    • Create the repository directory: "mkdir -p /opt/repo/RPMS/noarch"
    • [Follow this page for more info: http://www.techrepublic.com/blog/linux-and-open-source/create-your-own-yum-repository/609/ ]
    • To be able to use the repository through http, create a file /etc/httpd/conf.d/repo.conf:
                     Alias /repo "/opt/repo/RPMS/"

                     <Directory "/opt/repo/RPMS/">
                     Options +Indexes
                     FileETag MTime Size
                     AllowOverride None
                     Order allow,deny
                     Allow from all
                     </Directory>

    • Restart httpd: sudo service httpd restart
    • Now you'll be able to access the repository through: http://<server_address>/repo/noarch/

  • [This step is only required if you want jenkins to build rpm and automatically put in your repository. If you put your rpms manually then skip this step] Create the jenkins job for the specific branch in your jenkins installation
    • In the post step of the job configuration, select "Run only if build succeeds" and put "execute shell":

                     if [ -f ${WORKSPACE}/target/rpm/RPMS/noarch/*\.rpm ];
                     then
                     /bin/cp -f ${WORKSPACE}/target/rpm/RPMS/noarch/*\.rpm /opt/repo/RPMS/noarch /usr/bin/createrepo /opt/repo/RPMS/noarch
                     fi

    • The above will copy the rpm built rpm to the yum repository directory.
  • In the target server (i.e. where you want to run yum) install yum-priorities: 
                     yum install yum-priorities

          This plugin enables yum to search for packages based on priority.

  • Setup the repo file like (for example /etc/yum.repos.d/my-repo.repo):


                     [main-repo]
                     name=main-repo
                     baseurl=http://<server_address_of_main_repo>/repo/LATEST/
                     gpgcheck=0
                     enabled=1
                     metadata_expire=1
                     priority=2

                     [my-repo]
                     name=my-repo
                     baseurl=http://<server_address>/repo/noarch/
                     gpgcheck=0
                     enabled=1
                     metadata_expire=1
                     priority=1

          The above tells yum to first check in my-repo repo, if not found then to go to main-repo (note the priority=1).

Now you are ready to use your yum repository. As soon as the rpm is built by jenkins, it can be fetched using yum.