How to publish artifacts in Travis CI?

后端 未结 10 1860
一向
一向 2020-12-07 16:47

I would like to use Travis CI for my open-source project. The issue that Travis doesn\'t provide any ways to publish produced artifacts (though, they have this in their futu

相关标签:
10条回答
  • 2020-12-07 17:16

    Update: GitHub disable the Download API now, so below answer is idea.

    My solution is using "secure environment variables" provided by travis-ci and "GitHub repo Download API" with related script

    Each repo in GitHub has download pages, it is also the good place to publish your artifacts, and it has related "Repo Download API" http://developer.github.com/v3/repos/downloads/

    • Secure environment variable can refer to another question: Add secret environment variable to Travis CI
    • Use the nice script github-upload.rb from https://github.com/wereHamster/ghup to manage the GitHub API.

    In the end, in the .travis-ci.yml it looks like below

    env:
      global:
        - secure: "qkE5/TVKQV/+xBEW5M7ayWMMtFwhu44rQb9zh3n0LH4CkVb+b748lOuW3htc\nXfnXU8aGzOsQBeCJZQstfzsHFPkll+xfhk38cFqNQp7tpMo/AOZIkqd2AIUL\n0bgaFD+1kFAxKTu02m11xzkDNw6FuHMVvoMEQu/fo115i2YmWHo="  
    
    after_script:
      - ./github-upload.rb sdcamp.zh.pdf larrycai/sdcamp --description "generated by travis-ci, $TRAVIS_JOB_ID" --force --name sdcamp.zh.snapshot.pdf --skip-ssl-verification -t $GITHUB_TOKEN
    

    see my detail blog: http://larrycaiyu.com/blog/2012/10/25/publish-the-artifacts-inside-travis-ci-to-github/

    0 讨论(0)
  • 2020-12-07 17:17

    The integration SBT-Travis-Sonatype consists of the following main steps:

    1. Adding sbt-pgp plugin;
    2. Generating key pair for signing your artifacts and publishing it on a public key server;
    3. Encrypting the key pair and sonatype credential files and adding them to your project;
    4. Creating travis configuration and adding the encrypted key used by Travis to unpack your secret files.

    I put together a simple instruction on how to integrate SBT with Travis-CI and Sonatype, it is available here and contains the necessary steps from configuring the project plugins to encrypting the files and providing Travis configuration. It is mostly based on John Duffel’s developer blog combined with sbt-pgp reference docs.

    0 讨论(0)
  • 2020-12-07 17:18

    GitHub releases step-by-step

    The method was mentioned at https://stackoverflow.com/a/24100779/895245, and is poorly documented at: https://docs.travis-ci.com/user/deployment/releases/ , so here goes a more detailed step-by-step.

    It uploads artifacts to GitHub releases https://github.com/<username>/<repo>/releases which exist for every Git tag you push.

    1. Get a Personal Access Token under https://github.com/settings/tokens

      Only enable "public_repo" access for public repositories, "repo" for private.

      Save the token somewhere as you can only see it once.

    2. Install the travis gem:

      gem install travis
      # See: https://stackoverflow.com/a/33119804/895245
      gem update --system
      

      Then cd into your repository and:

      travis encrypt <api-token>
      

      but more recently people have reported that travis encrypt -r githubusername/repositoryname --org is needed instead, see: https://github.com/travis-ci/travis-ci/issues/8128

      This will produce an output like:

      secure: "<encrypted-token>"
      

      Note down the large encrypted token.

    3. Use a .travis.yml as follows:

      script:
        # This command generates a release.zip file.
        - make dist
      deploy:
        provider: releases
        api_key:
          secure: "<encrypted-token>"
        file: 'release.zip'
        skip_cleanup: true
        on:
          tags
      

      What happens is that Travis replaces every something: secure: <encrypted-string> with just something: <decrypted-string> as explained at: http://docs.travis-ci.com/user/encryption-keys/

      This is safe because only authorized pushes by you can decrypt the string, so if a malicious user tries to make a pull request to get your string, it would should just show the encrypted string.

      Now whenever you push a commit with a tag, Travis will upload release.zip to the release:

      git commit -m 1.0
      git tag -m 1.0 1.0
      git push --tags
      

      If you had already pushed the commit and the tag after, you might have to click the "Restart build" button on the Travis UI for it to upload.

    https://stackoverflow.com/a/38037626/895245 has some screenshots of the process.

    Alternative method: environment variable

    1. Instead of an encrypted string, we could also use a hidden environment variable.

      On the Travis settings for the repository https://travis-ci.org/<me>/<myrepo>/settings create an environment variable:

      GITHUB_API_KEY=<token>
      

      and make sure to mark "Display value in build log" as "Off", and use:

      api_key: '$GITHUB_API_KEY'
      

      While this will not show on logs for pull requests, this method is riskier, as you could my mistake list the environment of a build.

      The upside is that this method is simpler to understand.

    A simple example of mine that uploads images generated from Gnuplot to GitHub releases:

    • https://github.com/cirosantilli/gnuplot-examples/blob/b76d5a7c7aa2af973accdc9639220df74c36285e/.travis.yml#L23
    • https://github.com/cirosantilli/gnuplot-cheat/releases

    Question about GitHub Pages deployment: How to publish to Github Pages from Travis CI?

    0 讨论(0)
  • 2020-12-07 17:18

    The first question you should ask yourself is whether you want to publish artifacts from CI builds or whether you want to deploy releases (i.e. to GitHub).

    Since there are plenty of answers here regarding uploading releases to GitHub, I will not tackle this topic further.

    If you want to get hold of snapshot/CI build artifacts, the easiest way is to have them uploaded to AWS S3. Unfortunately, the documentation for this is a bit rough, especially when you are unexperienced with AWS. So here is what you have to do:

    1. Create a AWS IAM user for Travis

    To do so, go to https://console.aws.amazon.com/iam/home?#/users and create a new user account for Travis with type Programmatic access. Grant it access to S3 by applying the existing policy AmazonS3FullAccess on the permissions tab.

    Once the user is created make sure to copy the Access Key ID as well the secret access key for that user!

    2. Create a AWS S3 Bucket for Travis to upload to

    That one is pretty straight forward. Only thing to watch out for is to avoid Signature v4-only regions like us-east-2, eu-central-1, etc. (because of https://github.com/travis-ci/artifacts/issues/57). A solid choice is us-east-1, which also happens to be the default expected by Travis and thus saves you a little additional configuration. Of course you can also use an existing bucket that meets this requirement.

    3. Add environment variables to Travis CI repository settings

    Next, go to the settings for your repository in Travis. Create the following new environment variables:

    ARTIFACTS_KEY=(AWS access key id from step 1)
    ARTIFACTS_SECRET=(AWS secret access key from step 1)
    ARTIFACTS_BUCKET=(S3 bucket name from step 2)
    

    4. Enable artifacts addon

    Finally, in your repository add the following lines to the .travis.yml in order to activate the artifacts addon

    addons:
      artifacts: true
    

    If everything went well, you should see your build artifacts popping up in the S3 bucket. You may want to adjust the paths being scanned and so on, as desecribed in the documentation.

    Hope that helps someone or another.

    0 讨论(0)
提交回复
热议问题