|
| 1 | +--- |
| 2 | +layout: classic-docs |
| 3 | +title: Publishing Packages to packagecloud |
| 4 | +categories: [how-to] |
| 5 | +description: How to publish packages to packagecloud using CircleCI |
| 6 | +--- |
| 7 | + |
| 8 | +[Packagecloud](https://packagecloud.io) is a hosted package repository service. It allows users to host npm, Java/Maven, python, apt, yum and rubygem repositories without any pre-configuration. |
| 9 | + |
| 10 | +* TOC |
| 11 | +{:toc} |
| 12 | + |
| 13 | +## Configure Environment Variables |
| 14 | + |
| 15 | +### Set the `$PACKAGECLOUD_TOKEN` |
| 16 | + |
| 17 | +Under project settings in CircleCI, create an environment variable with the name `PACKAGECLOUD_TOKEN`, containing the value of a packagecloud API token. This environment variable will be used to authenticate with the packagecloud API directly, or using the packagecloud CLI. |
| 18 | + |
| 19 | +The packagecloud CLI will automatically read this environment variable from the system when interacting with repositories. |
| 20 | + |
| 21 | +Alternatively, if you prefer to keep your sensitive environment variables checked into git, but encrypted, you can follow the process outlined at [circleci/encrypted-files](https://github.com/circleci/encrypted-files). |
| 22 | + |
| 23 | +{:.no_toc} |
| 24 | + |
| 25 | +### Set the `$PACKAGECLOUD_URL` for packagecloud:enterprise |
| 26 | + |
| 27 | +_**Only set the `$PACKAGECLOUD_URL` if you're a packagecloud:enterprise customer**_ |
| 28 | + |
| 29 | +This setting is only for packagecloud:enterprise customers. Under project settings in CircleCI, set the `$PACKAGECLOUD_URL` environment variable to the URL of the packagecloud:enterprise installation. |
| 30 | + |
| 31 | +## Install the packagecloud CLI |
| 32 | + |
| 33 | +To use the packagecloud CLI from CircleCI, install it using RubyGems by adding the following `run` step to your `.circleci/config.yml` under the job that is configured to deploy the package: |
| 34 | + |
| 35 | +``` |
| 36 | +- run: |
| 37 | + name: Install packagecloud CLI |
| 38 | + command: gem install package_cloud |
| 39 | +``` |
| 40 | + |
| 41 | +The CLI will automatically use the `$PACKAGECLOUD_TOKEN` environment variable to authenticate against the packagecloud service. |
| 42 | + |
| 43 | +### Using Dependency Caching |
| 44 | + |
| 45 | +If you want to cache this dependency between builds, you can add the `package_cloud` gem to a `Gemfile` and follow CircleCI's guide for [Caching Dependencies]({{ site.baseurl }}/2.0/caching/). |
| 46 | + |
| 47 | +## Pushing Packages with the packagecloud CLI |
| 48 | + |
| 49 | +The build processes for package types will vary, but pushing them into a packagecloud repository is quite simple. To add packages to a repository from your CircleCI builds, add a step in your `deploy` configuration that uses the packagecloud CLI. |
| 50 | + |
| 51 | +The following is a full example `.circleci/config.yml` that will checkout a git repository, run a `make` task (this command can be anything configured to build your package), then deploy the package to a packagecloud repo. |
| 52 | + |
| 53 | +```yaml |
| 54 | +version: 2 |
| 55 | +defaults: &defaults |
| 56 | + working_directory: ~/repo |
| 57 | + docker: |
| 58 | + - image: circleci/ruby:2.3-jessie |
| 59 | +jobs: |
| 60 | + build: |
| 61 | + <<: *defaults |
| 62 | + steps: |
| 63 | + - checkout |
| 64 | + - run: |
| 65 | + name: Build the package |
| 66 | + command: make |
| 67 | + - persist_to_workspace: |
| 68 | + root: ~/repo |
| 69 | + paths: . |
| 70 | + deploy: |
| 71 | + <<: *defaults |
| 72 | + steps: |
| 73 | + - attach_workspace: |
| 74 | + at: ~/repo |
| 75 | + - run: |
| 76 | + name: Install packagecloud CLI |
| 77 | + command: gem install package_cloud |
| 78 | + - run: |
| 79 | + name: Push deb package |
| 80 | + command: package_cloud push example-user/example-repo/debian/jessie debs/packagecloud-test_1.1-2_amd64.deb |
| 81 | +workflows: |
| 82 | + version: 2 |
| 83 | + package-deploy: |
| 84 | + jobs: |
| 85 | + - build |
| 86 | + - deploy: |
| 87 | + requires: |
| 88 | + - build |
| 89 | +``` |
| 90 | +
|
| 91 | +## Deploy `npm` Packages |
| 92 | + |
| 93 | +CircleCI users can deploy packages directly to npm registries hosted on packagecloud. |
| 94 | + |
| 95 | +### Configure the Test Job |
| 96 | + |
| 97 | +This job will retrieve the project code, install it's dependencies and run any tests in the NodeJS project: |
| 98 | + |
| 99 | +```yaml |
| 100 | +jobs: |
| 101 | + test: |
| 102 | + <<: *defaults |
| 103 | + steps: |
| 104 | + - checkout |
| 105 | +
|
| 106 | + - restore_cache: |
| 107 | + keys: |
| 108 | + - v1-dependencies-. |
| 109 | + # fallback to using the latest cache if no exact match is found |
| 110 | + - v1-dependencies- |
| 111 | +
|
| 112 | + - run: npm install |
| 113 | + - run: |
| 114 | + name: Run tests |
| 115 | + command: npm test |
| 116 | +
|
| 117 | + - save_cache: |
| 118 | + paths: |
| 119 | + - node_modules |
| 120 | + key: v1-dependencies- |
| 121 | +
|
| 122 | + - persist_to_workspace: |
| 123 | + root: ~/repo |
| 124 | + paths: . |
| 125 | +``` |
| 126 | + |
| 127 | +### Configure the Deploy Job |
| 128 | + |
| 129 | +The next job configured is the deploy job. This job will authenticate and publish to the packagecloud npm registry: |
| 130 | + |
| 131 | +```yaml |
| 132 | +jobs: |
| 133 | +... |
| 134 | + deploy: |
| 135 | + <<: *defaults |
| 136 | + steps: |
| 137 | + - attach_workspace: |
| 138 | + at: ~/repo |
| 139 | + - run: |
| 140 | + name: Set registry URL |
| 141 | + command: npm set registry https://packagecloud.io/example-user/example-repo/npm/ |
| 142 | + - run: |
| 143 | + name: Authenticate with registry |
| 144 | + command: echo "//packagecloud.io/example-user/example-repo/npm/:_authToken=$PACKAGECLOUD_TOKEN" > ~/repo/.npmrc |
| 145 | + - run: |
| 146 | + name: Publish package |
| 147 | + command: npm publish |
| 148 | +``` |
| 149 | + |
| 150 | +* *Set registry URL* : This command sets the registry to URL that will be used by the `npm` CLI. |
| 151 | +* *Authenticate with the registry* : This command will set the `authToken` to be used by the `npm` CLI to the environment variable configured in the project settings. |
| 152 | +* *Publish package* : Publish the package to the configured npm registry on packagecloud. |
| 153 | + |
| 154 | +The packagecloud npm registry URL is in the following format: |
| 155 | + |
| 156 | +``` |
| 157 | +https://packagecloud.io/:username/:repo_name/npm/ |
| 158 | +``` |
| 159 | + |
| 160 | +The full `.circleci/config.yml` should look something like this: |
| 161 | + |
| 162 | +```yaml |
| 163 | +version: 2 |
| 164 | +defaults: &defaults |
| 165 | + working_directory: ~/repo |
| 166 | + docker: |
| 167 | + - image: circleci/node:8.9.1 |
| 168 | +jobs: |
| 169 | + test: |
| 170 | + <<: *defaults |
| 171 | + steps: |
| 172 | + - checkout |
| 173 | +
|
| 174 | + - restore_cache: |
| 175 | + keys: |
| 176 | + - v1-dependencies-. |
| 177 | + # fallback to using the latest cache if no exact match is found |
| 178 | + - v1-dependencies- |
| 179 | +
|
| 180 | + - run: npm install |
| 181 | + - run: |
| 182 | + name: Run tests |
| 183 | + command: npm test |
| 184 | +
|
| 185 | + - save_cache: |
| 186 | + paths: |
| 187 | + - node_modules |
| 188 | + key: v1-dependencies- |
| 189 | +
|
| 190 | + - persist_to_workspace: |
| 191 | + root: ~/repo |
| 192 | + paths: . |
| 193 | + deploy: |
| 194 | + <<: *defaults |
| 195 | + steps: |
| 196 | + - attach_workspace: |
| 197 | + at: ~/repo |
| 198 | + - run: |
| 199 | + name: Set registry URL |
| 200 | + command: npm set registry https://packagecloud.io/example-user/example-repo/npm/ |
| 201 | + - run: |
| 202 | + name: Authenticate with registry |
| 203 | + command: echo "//packagecloud.io/example-user/example-repo/npm/:_authToken=$PACKAGECLOUD_TOKEN" > ~/repo/.npmrc |
| 204 | + - run: |
| 205 | + name: Publish package |
| 206 | + command: npm publish |
| 207 | +workflows: |
| 208 | + version: 2 |
| 209 | + test-deploy: |
| 210 | + jobs: |
| 211 | + - test |
| 212 | + - deploy: |
| 213 | + requires: |
| 214 | + - test |
| 215 | +``` |
| 216 | + |
| 217 | +The workflows section will tie together both the `test` and `deploy` jobs into sequential steps in the build process. |
| 218 | + |
| 219 | +You can read more about publishing npm packages to packagecloud on the CircleCI blog post: [Publishing npm Packages Using CircleCI 2.0](https://circleci.com/blog/publishing-npm-packages-using-circleci-2-0/) |
| 220 | + |
| 221 | +## Using the packagecloud API |
| 222 | + |
| 223 | +Packagecloud also provides a robust API to manage package repositories. You can read more about the [packagecloud API](https://packagecloud.io/docs/api) and how to upload, delete, and promote packages across repositories. |
| 224 | + |
| 225 | +{:.no_toc} |
| 226 | + |
| 227 | +## See Also |
| 228 | + |
| 229 | +[Storing and Accessing Artifacts]({{ site.baseurl }}/2.0/artifacts/) |
0 commit comments