Skip to content

Commit 9a1f845

Browse files
authored
docs: expand jfrog platform and example template (#9073)
1 parent d54b387 commit 9a1f845

File tree

3 files changed

+123
-39
lines changed

3 files changed

+123
-39
lines changed

docs/platforms/jfrog.md

+79-21
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ The full example template can be found [here](https://github.com/coder/coder/tre
1111

1212
- A JFrog Artifactory instance
1313
- An admin-level access token for Artifactory
14-
- 1:1 mapping of users in Coder to users in Artifactory by email address
15-
- An npm repository in Artifactory named "npm"
14+
- 1:1 mapping of users in Coder to users in Artifactory by email address and username
15+
- Repositories configured in Artifactory for each package manager you want to use
1616

1717
<blockquote class="info">
1818
The admin-level access token is used to provision user tokens and is never exposed to
@@ -40,14 +40,14 @@ terraform {
4040
}
4141
artifactory = {
4242
source = "registry.terraform.io/jfrog/artifactory"
43-
version = "6.22.3"
43+
version = "~> 8.4.0"
4444
}
4545
}
4646
}
4747
48-
variable "jfrog_url" {
48+
variable "jfrog_host" {
4949
type = string
50-
description = "The URL of the JFrog instance."
50+
description = "JFrog instance hostname. e.g. YYY.jfrog.io"
5151
}
5252
5353
variable "artifactory_access_token" {
@@ -57,15 +57,15 @@ variable "artifactory_access_token" {
5757
5858
# Configure the Artifactory provider
5959
provider "artifactory" {
60-
url = "${var.jfrog_url}/artifactory"
60+
url = "https://${var.jfrog_host}/artifactory"
6161
access_token = "${var.artifactory_access_token}"
6262
}
6363
```
6464

65-
When pushing the template, you can pass in the variables using the `-V` flag:
65+
When pushing the template, you can pass in the variables using the `--var` flag:
6666

6767
```sh
68-
coder templates push --var 'jfrog_url=https://YYY.jfrog.io' --var 'artifactory_access_token=XXX'
68+
coder templates push --var 'jfrog_host=YYY.jfrog.io' --var 'artifactory_access_token=XXX'
6969
```
7070

7171
## Installing JFrog CLI
@@ -88,7 +88,21 @@ In our Docker-based example, we install `jf` by adding these lines to our `Docke
8888
RUN curl -fL https://install-cli.jfrog.io | sh && chmod 755 $(which jf)
8989
```
9090

91-
and use this `coder_agent` block:
91+
## Configuring Coder workspace to use JFrog Artifactory repositories
92+
93+
Create a `locals` block to store the Artifactory repository keys for each package manager you want to use in your workspace. For example, if you want to use artifactory repositories with keys `npm`, `pypi`, and `go`, you can create a `locals` block like this:
94+
95+
```hcl
96+
locals {
97+
artifactory_repository_keys = {
98+
npm = "npm"
99+
python = "pypi"
100+
go = "go"
101+
}
102+
}
103+
```
104+
105+
To automatically configure `jf` CLI and Artifactory repositories for each user, add the following lines to your `startup_script` in the `coder_agent` block:
92106

93107
```hcl
94108
resource "coder_agent" "main" {
@@ -107,9 +121,28 @@ resource "coder_agent" "main" {
107121
export CI=true
108122
109123
jf c rm 0 || true
110-
echo ${artifactory_access_token.me.access_token} | \
111-
jf c add --access-token-stdin --url ${var.jfrog_url} 0
124+
echo ${artifactory_scoped_token.me.access_token} | \
125+
jf c add --access-token-stdin --url https://${var.jfrog_host} 0
126+
127+
# Configure the `npm` CLI to use the Artifactory "npm" repository.
128+
cat << EOF > ~/.npmrc
129+
email = ${data.coder_workspace.me.owner_email}
130+
registry = https://${var.jfrog_host}/artifactory/api/npm/${local.artifactory_repository_keys["npm"]}
131+
EOF
132+
jf rt curl /api/npm/auth >> .npmrc
133+
134+
# Configure the `pip` to use the Artifactory "python" repository.
135+
mkdir -p ~/.pip
136+
cat << EOF > ~/.pip/pip.conf
137+
[global]
138+
index-url = https://${local.artifactory_username}:${artifactory_scoped_token.me.access_token}@${var.jfrog_host}/artifactory/api/pypi/${local.artifactory_repository_keys["python"]}/simple
139+
EOF
140+
112141
EOT
142+
# Set GOPROXY to use the Artifactory "go" repository.
143+
env = {
144+
GOPROXY : "https://${local.artifactory_username}:${artifactory_scoped_token.me.access_token}@${var.jfrog_host}/artifactory/api/go/${local.artifactory_repository_keys["go"]}"
145+
}
113146
}
114147
```
115148

@@ -119,12 +152,12 @@ running `jf c show`. It should display output like:
119152
```text
120153
coder@jf:~$ jf c show
121154
Server ID: 0
122-
JFrog Platform URL: https://cdr.jfrog.io/
123-
Artifactory URL: https://cdr.jfrog.io/artifactory/
124-
Distribution URL: https://cdr.jfrog.io/distribution/
125-
Xray URL: https://cdr.jfrog.io/xray/
126-
Mission Control URL: https://cdr.jfrog.io/mc/
127-
Pipelines URL: https://cdr.jfrog.io/pipelines/
155+
JFrog Platform URL: https://YYY.jfrog.io/
156+
Artifactory URL: https://YYY.jfrog.io/artifactory/
157+
Distribution URL: https://YYY.jfrog.io/distribution/
158+
Xray URL: https://YYY.jfrog.io/xray/
159+
Mission Control URL: https://YYY.jfrog.io/mc/
160+
Pipelines URL: https://YYY.jfrog.io/pipelines/
128161
User: ammar@....com
129162
Access token: ...
130163
Default: true
@@ -151,11 +184,11 @@ Note that this method will only work if your developers use code-server.
151184
Add the following line to your `startup_script` to configure `npm` to use
152185
Artifactory:
153186

154-
```sh
187+
```shell
155188
# Configure the `npm` CLI to use the Artifactory "npm" registry.
156189
cat << EOF > ~/.npmrc
157190
email = ${data.coder_workspace.me.owner_email}
158-
registry=${var.jfrog_url}/artifactory/api/npm/npm/
191+
registry = https://${var.jfrog_host}/artifactory/api/npm/npm/
159192
EOF
160193
jf rt curl /api/npm/auth >> .npmrc
161194
```
@@ -165,8 +198,33 @@ use Artifactory as the package registry. You can verify that `npm` is configured
165198
correctly by running `npm install --loglevel=http react` and checking that
166199
npm is only hitting your Artifactory URL.
167200
168-
You can apply the same concepts to Docker, Go, Maven, and other package managers
169-
supported by Artifactory.
201+
## Configuring pip
202+
203+
Add the following lines to your `startup_script` to configure `pip` to use
204+
Artifactory:
205+
206+
```shell
207+
mkdir -p ~/.pip
208+
cat << EOF > ~/.pip/pip.conf
209+
[global]
210+
index-url = https://${data.coder_workspace.me.owner}:${artifactory_scoped_token.me.access_token}@${var.jfrog_host}/artifactory/api/pypi/pypi/simple
211+
EOF
212+
```
213+
214+
Now, your developers can run `pip install` and transparently use Artifactory as the package registry. You can verify that `pip` is configured correctly by running `pip install --verbose requests` and checking that pip is only hitting your Artifactory URL.
215+
216+
## Configuring Go
217+
218+
Add the following environment variable to your `coder_agent` block to configure `go` to use Artifactory:
219+
220+
```hcl
221+
env = {
222+
GOPROXY : "https://${data.coder_workspace.me.owner}:${artifactory_scoped_token.me.access_token}@${var.jfrog_host}/artifactory/api/go/go"
223+
}
224+
```
225+
226+
You can apply the same concepts to Docker, Maven, and other package managers
227+
supported by Artifactory. See the [JFrog documentation](https://jfrog.com/help/r/jfrog-artifactory-documentation/package-management) for more information.
170228
171229
## More reading
172230

examples/templates/jfrog-docker/build/Dockerfile

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@ RUN apt-get update \
44
&& apt-get install -y \
55
curl \
66
git \
7-
golang \
7+
python3-pip \
88
sudo \
99
vim \
1010
wget \
1111
npm \
1212
&& rm -rf /var/lib/apt/lists/*
1313

14+
ARG GO_VERSION=1.20.7
15+
RUN mkdir --parents /usr/local/go && curl --silent --show-error --location \
16+
"https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz" -o /usr/local/go.tar.gz && \
17+
tar --extract --gzip --directory=/usr/local/go --file=/usr/local/go.tar.gz --strip-components=1
18+
19+
ENV PATH=$PATH:/usr/local/go/bin
20+
1421
ARG USER=coder
1522
RUN useradd --groups sudo --no-create-home --shell /bin/bash ${USER} \
1623
&& echo "${USER} ALL=(ALL) NOPASSWD:ALL" >/etc/sudoers.d/${USER} \

examples/templates/jfrog-docker/main.tf

+36-17
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,23 @@ terraform {
1010
}
1111
artifactory = {
1212
source = "registry.terraform.io/jfrog/artifactory"
13-
version = "6.22.3"
13+
version = "~> 8.4.0"
1414
}
1515
}
1616
}
1717

1818
locals {
19-
username = data.coder_workspace.me.owner
19+
# if the jfrog username is same as the coder username, you can use the following
20+
# artifactory_username = data.coder_workspace.me.owner
21+
# if the username is same as email, you can use the following
22+
# artifactory_username = urlencode(data.coder_workspace.me.owner_email)
23+
artifactory_username = data.coder_workspace.me.owner
24+
artifactory_repository_keys = {
25+
"npm" = "npm"
26+
"python" = "python"
27+
"go" = "go"
28+
}
29+
workspace_user = data.coder_workspace.me.owner
2030
}
2131

2232
data "coder_provisioner" "me" {
@@ -28,27 +38,24 @@ provider "docker" {
2838
data "coder_workspace" "me" {
2939
}
3040

31-
variable "jfrog_url" {
41+
variable "jfrog_host" {
3242
type = string
33-
description = "The URL of the JFrog instance."
43+
description = "JFrog instance hostname. For example, 'YYY.jfrog.io'."
3444
}
3545

3646
variable "artifactory_access_token" {
3747
type = string
3848
description = "The admin-level access token to use for JFrog."
3949
}
4050

41-
4251
# Configure the Artifactory provider
4352
provider "artifactory" {
44-
url = "${var.jfrog_url}/artifactory"
53+
url = "https://${var.jfrog_host}/artifactory"
4554
access_token = var.artifactory_access_token
4655
}
4756

48-
resource "artifactory_access_token" "me" {
49-
username = data.coder_workspace.me.owner_email
50-
# The token should live for the duration of the workspace.
51-
end_date_relative = "0s"
57+
resource "artifactory_scoped_token" "me" {
58+
username = local.artifactory_username
5259
}
5360

5461
resource "coder_agent" "main" {
@@ -67,23 +74,35 @@ resource "coder_agent" "main" {
6774
export CI=true
6875
6976
jf c rm 0 || true
70-
echo ${artifactory_access_token.me.access_token} | \
71-
jf c add --access-token-stdin --url ${var.jfrog_url} 0
77+
echo ${artifactory_scoped_token.me.access_token} | \
78+
jf c add --access-token-stdin --url https://${var.jfrog_host} 0
7279
73-
# Configure the `npm` CLI to use the Artifactory "npm" registry.
80+
# Configure the `npm` CLI to use the Artifactory "npm" repository.
7481
cat << EOF > ~/.npmrc
7582
email = ${data.coder_workspace.me.owner_email}
76-
registry=${var.jfrog_url}/artifactory/api/npm/npm/
83+
registry = https://${var.jfrog_host}/artifactory/api/npm/${local.artifactory_repository_keys["npm"]}
7784
EOF
7885
jf rt curl /api/npm/auth >> .npmrc
86+
87+
# Configure the `pip` to use the Artifactory "python" repository.
88+
mkdir -p ~/.pip
89+
cat << EOF > ~/.pip/pip.conf
90+
[global]
91+
index-url = https://${local.artifactory_username}:${artifactory_scoped_token.me.access_token}@${var.jfrog_host}/artifactory/api/pypi/${local.artifactory_repository_keys["python"]}/simple
92+
EOF
93+
7994
EOT
95+
# Set GOPROXY to use the Artifactory "go" repository.
96+
env = {
97+
GOPROXY : "https://${local.artifactory_username}:${artifactory_scoped_token.me.access_token}@${var.jfrog_host}/artifactory/api/go/${local.artifactory_repository_keys["go"]}"
98+
}
8099
}
81100

82101
resource "coder_app" "code-server" {
83102
agent_id = coder_agent.main.id
84103
slug = "code-server"
85104
display_name = "code-server"
86-
url = "http://localhost:13337/?folder=/home/${local.username}"
105+
url = "http://localhost:13337/?folder=/home/${local.workspace_user}"
87106
icon = "/icon/code.svg"
88107
subdomain = false
89108
share = "owner"
@@ -108,7 +127,7 @@ resource "docker_image" "main" {
108127
build {
109128
context = "./build"
110129
build_args = {
111-
USER = local.username
130+
USER = local.workspace_user
112131
}
113132
}
114133
triggers = {
@@ -130,7 +149,7 @@ resource "docker_container" "workspace" {
130149
ip = "host-gateway"
131150
}
132151
volumes {
133-
container_path = "/home/${local.username}"
152+
container_path = "/home/${local.workspace_user}"
134153
volume_name = docker_volume.home_volume.name
135154
read_only = false
136155
}

0 commit comments

Comments
 (0)