Merge pull request #1 from matifali/activate

support `--activate` flag to push inactive templates
This commit is contained in:
Muhammad Atif Ali 2023-06-05 01:39:00 +03:00 committed by GitHub
commit 56bfec8c51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 97 additions and 47 deletions

View file

@ -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` |
| 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` |
## Example
## Examples
```yaml
name: Update Coder Template
1. Update template with latest commit hash as version and activate it.
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)"
```yaml
name: Update Coder Template
- 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 }}
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 }}
```

View file

@ -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:
@ -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 }}

View file

@ -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}."