Skip to content
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

support --activate flag to push inactive templates #1

Merged
merged 3 commits into from
Jun 4, 2023
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
105 changes: 67 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,70 @@ Update coder templates automatically

## Inputs

| Name | Description | Default |
| ---- | ----------- | ------- |
| `CODER_URL` | **Required** The url of coder (e.g. <https://dev.coder.com>). | - |
| `CODER_TEMPLATE_NAME` | **Required** The name of template. | - |
| `CODER_TEMPLATE_DIR` | The directory of template. |`CODER_TEMPLATE_NAME`|
| `CODER_TEMPLATE_VERSION` | The version of template. | - |
| `CODER_SESSION_TOKEN` | **Required** The session token of coder. | `secrets.CODER_SESSION_TOKEN` |

## Example

```yaml
name: Update Coder Template

on:
push:
branches:
- main

jobs:
update:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Get latest commit hash
id: latest_commit
run: echo "::set-output name=hash::$(git rev-parse --short HEAD)"

- name: Update Coder Template
uses: matifali/update-coder-template@latest
with:
CODER_TEMPLATE_NAME: "my-template"
CODER_TEMPLATE_DIR: "my-template"
CODER_URL: "https://dev.coder.com"
CODER_TEMPLATE_VERSION: "${{ steps.latest_commit.outputs.hash }}"
CODER_SESSION_TOKEN: ${{ secrets.CODER_SESSION_TOKEN }}

```
| Name | Description | Default |
| ------------------------- | ------------------------------------------------------------------------ | ----------------------------- |
| `CODER_ACCESS_URL` | **Required** The url of coder deployment (e.g. <https://dev.coder.com>). | - |
| `CODER_SESSION_TOKEN` | **Required** The session token of coder. | `secrets.CODER_SESSION_TOKEN` |
| `CODER_TEMPLATE_NAME` | **Required** The name of template. | - |
| `CODER_TEMPLATE_DIR` | The directory of template. | `CODER_TEMPLATE_NAME` |
| `CODER_TEMPLATE_VERSION` | The version of template. | - |
| `CODER_TEMPLATE_ACTIVATE` | Activate the template after update. | `true` |

## Examples

1. Update template with latest commit hash as version and activate it.

```yaml
name: Update Coder Template

on:
push:
branches:
- main

jobs:
update:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Get latest commit hash
id: latest_commit
run: echo "::set-output name=hash::$(git rev-parse --short HEAD)"

- name: Update Coder Template
uses: matifali/update-coder-template@latest
with:
CODER_TEMPLATE_NAME: "my-template"
CODER_TEMPLATE_DIR: "my-template"
CODER_ACCESS_URL: "https://coder.example.com"
CODER_TEMPLATE_VERSION: "${{ steps.latest_commit.outputs.hash }}"
CODER_SESSION_TOKEN: ${{ secrets.CODER_SESSION_TOKEN }}
```

2. Update template with a random version name and don't activate it.

```yaml
name: Update Coder Template

on:
push:
branches:
- main

jobs:
update:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Update Coder Template
uses: matifali/update-coder-template@latest
with:
CODER_TEMPLATE_NAME: "my-template"
CODER_TEMPLATE_DIR: "my-template"
CODER_ACCESS_URL: "https://coder.example.com"
CODER_TEMPLATE_ACTIVATE: "false"
CODER_SESSION_TOKEN: ${{ secrets.CODER_SESSION_TOKEN }}
```
11 changes: 8 additions & 3 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ inputs:
CODER_TEMPLATE_NAME:
description: "Template name"
required: true
CODER_URL:
description: "Coder URL (e.g. https://coder.example.com)"
CODER_ACCESS_URL:
description: "Coder access URL (e.g. https://coder.example.com)"
required: true
CODER_SESSION_TOKEN:
description: "Coder session token"
required: true
CODER_TEMPLATE_DIR:
description: "Template directory name defaults to TEMPLATE_NAME"
description: "Template directory name (path to the directory containing the main.tf file default: TEMPLATE_NAME)"
required: false
CODER_TEMPLATE_VERSION:
description: "Template version"
required: false
CODER_TEMPLATE_ACTIVATE:
description: "Makes the current template active"
required: false
default: "true"

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
runs:
Expand All @@ -34,3 +38,4 @@ runs:
CODER_TEMPLATE_NAME: ${{ inputs.CODER_TEMPLATE_NAME }}
CODER_TEMPLATE_DIR: ${{ inputs.CODER_TEMPLATE_DIR }}
CODER_TEMPLATE_VERSION: ${{ inputs.CODER_TEMPLATE_VERSION }}
CODER_TEMPLATE_MAKE_ACTIVE: ${{ inputs.CODER_TEMPLATE_MAKE_ACTIVE }}
38 changes: 27 additions & 11 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
#!/bin/bash -l
set -e

# Check if CODER_SESSION_TOKEN is set
# Check if required variables are set
: "${CODER_SESSION_TOKEN:?Variable not set or empty}"
echo "CODER_SESSION_TOKEN is set."

echo "Pushing ${CODER_TEMPLATE_NAME} to ${CODER_URL}..."
: "${CODER_ACCESS_URL:?Variable not set or empty}"
echo "CODER_ACCESS_URL is set."

# if the CODRR_TEMPLATE_DIR is empty string, then use the TEMPLATE_NAME as the directory
if [ -z "${CODER_TEMPLATE_DIR}" ]; then
CODER_TEMPLATE_DIR="${CODER_TEMPLATE_NAME}"
echo "Pushing ${CODER_TEMPLATE_NAME} to ${CODER_ACCESS_URL}..."

# Set default values if variables are empty
CODER_TEMPLATE_DIR=${CODER_TEMPLATE_DIR:-$CODER_TEMPLATE_NAME}
echo "CODER_TEMPLATE_DIR is set to ${CODER_TEMPLATE_DIR}"

# Construct push command
push_command="coder templates push ${CODER_TEMPLATE_NAME} --directory ./${CODER_TEMPLATE_DIR}"

# Add version to the push command if specified
if [ -n "${CODER_TEMPLATE_VERSION}" ]; then
push_command+=" --name ${CODER_TEMPLATE_VERSION}"
fi

# if the CODER_TEMPLATE_VERSION is empty string then let coder use a random name
if [ -z "${CODER_TEMPLATE_VERSION}" ];
then
coder templates push ${CODER_TEMPLATE_NAME} --directory ./${CODER_TEMPLATE_DIR} --url ${CODER_URL} --yes
else
coder templates push ${CODER_TEMPLATE_NAME} --directory ./${CODER_TEMPLATE_DIR} --url ${CODER_URL} --name ${CODER_TEMPLATE_VERSION} --yes
# Add activate flag to the push command if specified
if [ -n "${CODER_TEMPLATE_ACTIVATE}" ]; then
push_command+=" --activate=${CODER_TEMPLATE_ACTIVATE}"
fi

# Add confirmation flag to the push command
push_command+=" --yes"

# Execute the push command
${push_command}

echo "Template ${CODER_TEMPLATE_NAME} pushed to ${CODER_ACCESS_URL}."