How to reproduce a travis-ci build environment for debugging

后端 未结 4 1976
庸人自扰
庸人自扰 2020-12-07 13:29

I am seeing a build failure on travis-ci, which I cannot reproduce on my local machine. Are there instructions somewhere for setting up a VM that is identical to the travis

相关标签:
4条回答
  • 2020-12-07 13:44

    You can use Travis Build which is a library (which means you've to place it in ~/.travis/) to generate a shell based build script (travis compile) which can be then uploaded to the VMs using SSH and executed.

    Below steps are just guidance in order to get you into the right track (if anything is missing, let me know).

    Docker

    Example command to run container (which can be found at Docker Hub):

    docker run -it travisci/ubuntu-ruby:18.04 /bin/bash
    

    Run your container, clone your repository then test it manually.

    See: Running a Container Based Docker Image Locally

    SSH access

    Check out this answer. Basically you need to setup bounce host, then configure your build to run SSH tunnel.

    Here is the example .travis.yml:

    sudo: required
    dist: trusty
    
    language: python
    python: "2.7"
    
    script:
    - echo travis:$sshpassword | sudo chpasswd
    - sudo sed -i 's/ChallengeResponseAuthentication no/ChallengeResponseAuthentication yes/' /etc/ssh/sshd_config
    - sudo service ssh restart
    - sudo apt-get install sshpass
    - sshpass -p $sshpassword ssh -R 9999:localhost:22 -o StrictHostKeyChecking=no travisci@$bouncehostip
    

    Local setup

    Here are the steps to test it on your local environment:

    cd ~
    git clone https://github.com/travis-ci/travis-build.git
    ln -s ~/travis-build/ ~/.travis/travis-build
    sudo gem install bundler
    bundle install --gemfile ~/.travis/travis-build/Gemfile
    cd repo-dir/
    travis login -g <github_token>
    vim .travis.yaml
    travis lint # to validate script
    travis compile # to transform into shell script
    

    Vagrant/VM

    After you did travis compile which would produce the bash script as result of your .travis.yml, you can use use vagrant to run this script into virtualized environment using provided Vagrantfile and the following steps:

    vagrant up
    vagrant ssh
    cd /vagrant
    bundle exec rspec spec
    

    You probably need to install more tools in order to test it.


    Here is some git hint which avoids you to generates unnecessary commits when doing trial & errors commits for Travis CI testing:

    1. Fork the repo (or use separate branch).
    2. After initial commit, keep adding --amend to replace your previous commit:

      git commit --amend -m 'Same message.' -a
      
    3. Push the amended commit by force (e.g. into already opened PR):

      git push fork -f
      
    4. Now Travis CI would re-check the same commit over and over again.


    See also: How to run travis-ci locally.

    0 讨论(0)
  • 2020-12-07 13:44

    I'm facing the same issue right now. I used to use CircleCI before, where you could just login to VM via ssh, but this doesn't work with Travis-CI VMs.

    I was able to debug it (to a certain point) by setting up Travis-ci VM clone via Travis-Cookbooks. You would need to install VirtualBox and Vagrant on your computer first before cloning this repository.

    Once you have Travis-Cookbooks cloned, open the folder, launch command prompt|terminal and type vagrant up. Once Vagrant finishes setting up VM (may take a long time) on your machine, you can connect to it via ssh by running vagrant ssh.

    From there, you would need to clone your own repository (or just copy the code to VM) and apply the steps from your .travis.yml file.

    0 讨论(0)
  • 2020-12-07 13:53

    For container-based builds, there are now instructions on how to setup a docker image locally.

    Unfortunately, quite a few steps are still manual. Here are the commands you need to get it up and running:

    # change the image according to the language chosen in .travis.yml
    $ docker run -it -u travis quay.io/travisci/travis-jvm /bin/bash
    
    # now that you are in the docker image, switch to the travis user
    sudo su - travis
    
    # Install a recent ruby (default is 1.9.3)
    rvm install 2.3.0
    rvm use 2.3.0
    
    # Install travis-build to generate a .sh out of .travis.yml
    cd builds
    git clone https://github.com/travis-ci/travis-build.git
    cd travis-build
    gem install travis
    travis # to create ~/.travis
    ln -s `pwd` ~/.travis/travis-build
    bundle install
    
    # Create project dir, assuming your project is `me/project` on GitHub
    cd ~/builds
    mkdir me
    cd me
    git clone https://github.com/me/project.git
    cd project
    # change to the branch or commit you want to investigate
    travis compile > ci.sh
    # You most likely will need to edit ci.sh as it ignores matrix and env
    bash ci.sh
    
    0 讨论(0)
  • 2020-12-07 13:56

    Eregon's answer failed for me at travis compile, there error looks like:

    /home/travis/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- travis/support (LoadError)
    

    I got it working with the following adjustments: (Adjustments marked with # CHANGED. I'm using the node environment)

    # change the image according to the language chosen in .travis.yml
    # Find images at https://quay.io/organization/travisci
    docker run -it quay.io/travisci/travis-node-js /bin/bash
    
    # now that you are in the docker image, switch to the travis user
    su travis
    
    # Install a recent ruby (default is 1.9.3) to make bundle install work
    rvm install 2.3.0 
    rvm use 2.3.0
    
    # Install travis-build to generate a .sh out of .travis.yml
    sudo mkdir builds         # CHANGED
    cd builds
    sudo git clone https://github.com/travis-ci/travis-build.git
    cd travis-build
    gem install travis
    travis # to create ~/.travis
    ln -s `pwd` ~/.travis/travis-build
    bundle install
    bundler add travis        # CHANGED
    sudo mkdir bin            # CHANGED
    sudo chmod a+w bin/       # CHANGED
    bundler binstubs travis   # CHANGED
    
    # Create project dir, assuming your project is `me/project` on GitHub
    cd ~/builds
    mkdir me
    cd me
    git clone https://github.com/me/project.git
    cd project
    # change to the branch or commit you want to investigate
    ~/.travis/travis-build/bin/travis compile > ci.sh # CHANGED
    # You most likely will need to edit ci.sh as it ignores matrix and env
    # In particular I needed to edit --branch=’’ to the branch name
    bash ci.sh
    
    0 讨论(0)
提交回复
热议问题