add --create and --message and major refactor

This commit is contained in:
Muhammad Atif Ali 2023-08-11 11:21:22 +03:00
parent 6abcdd2856
commit 928dd88265
4 changed files with 73 additions and 73 deletions

View file

@ -1,18 +0,0 @@
FROM ubuntu:latest
LABEL "com.github.actions.name"="GitHub Action for Pushing Changes to your Coder Template"
LABEL "com.github.actions.description"="An action to deploy changes to your coder template automatically"
LABEL "com.github.actions.icon"="arrow-up"
LABEL "com.github.actions.color"="purple"
LABEL "repository"="http://github.com/matifali/update-coder-template"
LABEL "maintainer"="Muhammad Atif Ali <matifali@live.com>"
# Install curl
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
# Install the coder binary
RUN curl -L https://coder.com/install.sh | sh
# Entry point
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

View file

@ -6,36 +6,50 @@ branding:
icon: arrow-up-circle
color: green
# specify the inputs that this action accepts
inputs:
CODER_TEMPLATE_NAME:
description: "Template name"
id:
description: "Template identifier (e.g. my-template)"
required: true
CODER_URL:
url:
description: "Coder access URL (e.g. https://coder.example.com)"
required: true
CODER_SESSION_TOKEN:
coder_session_token:
description: "Coder session token"
required: true
CODER_TEMPLATE_DIR:
dir:
description: "Template directory name (path to the directory containing the main.tf file default: TEMPLATE_NAME)"
required: false
CODER_TEMPLATE_VERSION:
description: "Template version"
name:
description: "Template version name (e.g. v1.0.0, commit hash, etc.), should be unique, default: a random string"
required: false
CODER_TEMPLATE_ACTIVATE:
description: "Makes the current template active"
activate:
description: "Marks the current template version as active"
required: false
default: "true"
message:
description: "update message"
required: false
default: "Updated via update-coder-template action"
create:
description: "Creates a new template if it does not exist"
required: false
default: "true"
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
runs:
using: "docker"
image: "Dockerfile"
using: composite
env:
CODER_SESSION_TOKEN: ${{ inputs.CODER_SESSION_TOKEN }}
CODER_URL: ${{ inputs.CODER_URL }}
CODER_TEMPLATE_NAME: ${{ inputs.CODER_TEMPLATE_NAME }}
CODER_TEMPLATE_DIR: ${{ inputs.CODER_TEMPLATE_DIR }}
CODER_TEMPLATE_VERSION: ${{ inputs.CODER_TEMPLATE_VERSION }}
CODER_TEMPLATE_ACTIVATE: ${{ inputs.CODER_TEMPLATE_ACTIVATE }}
CODER_SESSION_TOKEN: ${{ inputs.coder_session_token }}
CODER_URL: ${{ inputs.url }}
CODER_TEMPLATE_ID: ${{ inputs.id }}
CODER_TEMPLATE_DIR: ${{ inputs.dir }}
CODER_TEMPLATE_VERSION_NAME: ${{ inputs.name }}
CODER_TEMPLATE_ACTIVATE: ${{ inputs.activate }}
CODER_TEMPLATE_MESSAGE: ${{ inputs.message }}
CODER_TEMPLATE_CREATE: ${{ inputs.create }}
steps:
- run: curl -fsSL $CODER_URL/bin/coder-linux-amd64 -o /usr/local/bin/coder && chmod +x /usr/local/bin/coder
shell: bash
- run: push_template.sh
shell: bash

View file

@ -1,36 +0,0 @@
#!/bin/bash -l
set -euo pipefail
# Check if required variables are set
: "${CODER_SESSION_TOKEN:?Variable not set or empty}"
echo "CODER_SESSION_TOKEN is set."
: "${CODER_URL:?Variable not set or empty}"
echo "CODER_URL: ${CODER_URL}"
echo "Pushing ${CODER_TEMPLATE_NAME} to ${CODER_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
# Add activate flag to the push command if it is false
if [ "${CODER_TEMPLATE_ACTIVATE}" = "false" ]; then
push_command+=" --activate=false"
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_URL}."

40
push_template.sh Normal file
View file

@ -0,0 +1,40 @@
#!/bin/bash -l
set -euo pipefail
# check if required variables are set
: "${CODER_SESSION_TOKEN:?CODER_SESSION_TOKEN not set or empty}"
echo "CODER_SESSION_TOKEN is set."
: "${CODER_URL:?CODER_URL not set or empty}"
echo "CODER_URL is set."
: "${CODER_TEMPLATE_ID:?CODER_TEMPLATE_ID not set or empty}"
echo "CODER_TEMPLATE_ID: ${CODER_TEMPLATE_ID}"
# Set default values if variables are empty
CODER_TEMPLATE_DIR=${CODER_TEMPLATE_DIR:-$CODER_TEMPLATE_ID}
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}" --message ${CODER_TEMPLATE_MESSAGE}
# Append --create flag to the push command if CODER_TEMPLATE_CREATE is true
if [ "${CODER_TEMPLATE_CREATE}" = "true" ]; then
push_command+=" --create"
fi
# Add version to the push command if specified
if [ -n "${CODER_TEMPLATE_VERSION_NAME}" ]; then
push_command+=" --name ${CODER_TEMPLATE_VERSION_NAME}"
fi
# Add activate flag to the push command if it is false
if [ "${CODER_TEMPLATE_ACTIVATE}" = "false" ]; then
push_command+=" --activate=false"
fi
# Add confirmation flag to the push command
push_command+=" --yes"
# Execute the push command
${push_command}
echo "A new version of ${CODER_TEMPLATE_DIR} is pushed to ${CODER_URL} successfully."