Skip to content

docs: API tokens & CI automation #4510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion .github/workflows/dogfood.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
workflow_dispatch:

jobs:
deploy:
deploy_image:
runs-on: ubuntu-latest
steps:
- name: Get branch name
Expand Down Expand Up @@ -47,3 +47,27 @@ jobs:
tags: "codercom/oss-dogfood:${{ steps.docker-tag-name.outputs.tag }},codercom/oss-dogfood:latest"
cache-from: type=registry,ref=codercom/oss-dogfood:latest
cache-to: type=inline
deploy_template:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Get short commit SHA
id: vars
run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
- name: "Install latest Coder"
run: |
curl -L https://coder.com/install.sh | sh
# env:
# VERSION: 0.x
- name: "Push template"
run: |
coder templates push $CODER_TEMPLATE_NAME --directory $CODER_TEMPLATE_DIR --yes --name=$CODER_TEMPLATE_VERSION
env:
# Consumed by Coder CLI
CODER_URL: https://dev.coder.com
CODER_SESSION_TOKEN: ${{ secrets.CODER_SESSION_TOKEN }}
# Template source & details
CODER_TEMPLATE_NAME: ${{ secrets.CODER_TEMPLATE_NAME }}
CODER_TEMPLATE_VERSION: ${{ steps.vars.outputs.sha_short }}
CODER_TEMPLATE_DIR: ./dogfood
2 changes: 1 addition & 1 deletion cli/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func createToken() *cobra.Command {
cmd.Println(cliui.Styles.Code.Render(strings.TrimSpace(res.Key)))
cmd.Println()
cmd.Println(cliui.Styles.Wrap.Render(
fmt.Sprintf("You can use this token by setting the --%s CLI flag, the %s environment variable, or the %q HTTP header.", varToken, envSessionToken, codersdk.SessionTokenKey),
fmt.Sprintf("You can use this token by setting the --%s CLI flag, the %s environment variable, or the %q HTTP header.", varToken, envSessionToken, codersdk.SessionCustomHeader),
))

return nil
Expand Down
37 changes: 37 additions & 0 deletions docs/admin/automation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Automation

We recommend automating Coder deployments through the CLI. Examples include [updating templates via CI/CD pipelines](../templates/change-management.md).

## Tokens

Long-lived tokens can be generated to perform actions on behalf of your user account:

```sh
coder tokens create
```

## CLI

You can use tokens with the CLI by setting the `--token` CLI flag or the `CODER_SESSION_TOKEN`
environment variable.

```sh
export CODER_URL=https://coder.example.com
export CODER_SESSION_TOKEN=*****
coder workspaces ls
```

## REST API

You can use tokens with the Coder's REST API using the `Coder-Session-Token` HTTP header.

```sh
curl 'https://dev.coder.com/api/v2/workspaces' \
-H 'Coder-Session-Token: *****'
```

> At this time, we do not publish an API reference. However, [codersdk](https://github.com/coder/coder/tree/main/codersdk) can be grepped to find the necessary routes and payloads.

## Golang SDK

Coder publishes a public [Golang SDK](https://pkg.go.dev/github.com/coder/coder@main/codersdk) for Coder. This is consumed by the [CLI package](https://github.com/coder/coder/tree/main/cli).
4 changes: 4 additions & 0 deletions docs/images/icons/git.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/images/icons/plug.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@
"path": "./templates/authentication.md",
"icon_path": "./images/icons/key.svg"
},
{
"title": "Change Management",
"description": "Learn how to source-control templates with git and CI",
"path": "./templates/change-management.md",
"icon_path": "./images/icons/git.svg"
},
{
"title": "Resource Metadata",
"description": "Learn how to expose resource data to users",
Expand Down Expand Up @@ -212,6 +218,12 @@
"icon_path": "./images/icons/upgrade.svg",
"path": "./admin/upgrade.md"
},
{
"title": "Automation",
"description": "Learn how to automate Coder with the CLI and API",
"icon_path": "./images/icons/plug.svg",
"path": "./admin/automation.md"
},
{
"title": "Audit Logs",
"description": "Learn how to use Audit Logs in your Coder deployment.",
Expand Down
9 changes: 2 additions & 7 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,9 @@ practices:

Template permissions can be used to give users and groups access to specific templates. [Learn more about RBAC](./admin/rbac.md).

## Change Management

We recommend source controlling your templates as you would other code.

CI is as simple as running `coder templates push` with the appropriate
credentials.

## Next Steps

- Learn about [Authentication & Secrets](templates/authentication.md)
- Learn about [Change Management](templates/change-management.md)
- Learn about [Resource Metadata](templates/resource-metadata.md)
- Learn about [Workspaces](workspaces.md)
23 changes: 23 additions & 0 deletions docs/templates/change-management.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Template Change Management

We recommend source controlling your templates as you would other code. [Install Coder](../install/) in CI/CD pipelines to push new template versions.

```sh
# Install the Coder CLI
curl -L https://coder.com/install.sh | sh
# curl -L https://coder.com/install.sh | sh -s -- --version=0.x

# To create API tokens, use `coder tokens create`.
# These variables are consumed by Coder
export CODER_URL=https://coder.example.com
export CODER_SESSION_TOKEN=*****

# Template details
export CODER_TEMPLATE_NAME=kubernetes
export CODER_TEMPLATE_DIR=.coder/templates/kubernetes
export CODER_TEMPLATE_VERSION=$(git rev-parse --short HEAD)

coder templates push --yes $CODER_TEMPLATE_NAME \
--directory $CODER_TEMPLATE_DIR \
--name=$CODER_TEMPLATE_VERSION # Version name is optional
```
Comment on lines +3 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really bought into this being the way we suggest customers manage these - at least until we hear feedback otherwise.