Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
template-skeleton-2
Manage
Activity
Members
Plan
Wiki
External wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
to be continuous...
tools
template-skeleton-2
Commits
38dd0a69
Commit
38dd0a69
authored
1 year ago
by
Pierre Smeyers
Browse files
Options
Downloads
Patches
Plain Diff
refactor(script): retrieve function params directly from env
parent
029b0d6a
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
{{cookiecutter.project_slug}}/templates/gitlab-ci-{{cookiecutter.project_slug}}.yml
+36
-43
36 additions, 43 deletions
...g}}/templates/gitlab-ci-{{cookiecutter.project_slug}}.yml
with
36 additions
and
43 deletions
{{cookiecutter.project_slug}}/templates/gitlab-ci-{{cookiecutter.project_slug}}.yml
+
36
−
43
View file @
38dd0a69
...
...
@@ -369,13 +369,26 @@ stages:
awk '!/# *nosubst/{while(match($0,"[$%]{[^}]*}")) {var=substr($0,RSTART+2,RLENGTH-3);val=ENVIRON[var]; gsub(/["\\]/,"\\\\&",val); gsub("\n","\\n",val);gsub("\r","\\r",val); gsub("[$%]{"var"}",val)}}1'
}
# login to the hosting platform
function {{ cookiecutter.template_prefix }}_login() {
api_url=${ENV_API_URL:-${{ cookiecutter.template_PREFIX }}_API_URL}
api_token=${ENV_API_TOKEN:-${{ cookiecutter.template_PREFIX }}_API_TOKEN}
project=$ENV_PROJECT
assert_defined "$api_url" 'Missing required API url'
assert_defined "$api_token" 'Missing required API token'
assert_defined "$project" 'Missing required project'
{{ cookiecutter.cli_tool }} login --url="$api_url" --token="$api_token" --project="$project"
}
# application deployment function
function deploy() {
export environment_type=$
1
export environment_name=$
2
environment_url=$
3
function
{{ cookiecutter.template_prefix }}_
deploy() {
export environment_type=$
ENV_TYPE
export environment_name=$
{ENV_APP_NAME:-{{ '${' }}{{ cookiecutter.template_PREFIX }}_BASE_APP_NAME}${ENV_APP_SUFFIX}}
environment_url=$
{ENV_URL:-${{ cookiecutter.template_PREFIX }}_ENVIRONMENT_URL}
# also export environment_name in SCREAMING_SNAKE_CASE format (may be useful with Kubernetes env variables)
environment_name_ssc=$(to_ssc "$
2
")
environment_name_ssc=$(to_ssc "$
environment_name
")
export environment_name_ssc
# variables expansion in $environment_url
...
...
@@ -410,11 +423,11 @@ stages:
}
# environment cleanup function
function delete() {
export environment_type=$
1
export environment_name=$
2
function
{{ cookiecutter.template_prefix }}_
delete() {
export environment_type=$
ENV_TYPE
export environment_name=$
{ENV_APP_NAME:-{{ '${' }}{{ cookiecutter.template_PREFIX }}_BASE_APP_NAME}${ENV_APP_SUFFIX}}
# also export environment_name in SCREAMING_SNAKE_CASE format (may be useful with Kubernetes env variables)
environment_name_ssc=$(to_ssc "$
2
")
environment_name_ssc=$(to_ssc "$
environment_name
")
export environment_name_ssc
log_info "--- \\e[32mcleanup\\e[0m"
...
...
@@ -558,6 +571,10 @@ stages:
{
%
- elif cookiecutter.template_type == 'deploy' %
}
# job prototype
# defines default Docker image, tracking probe, cache policy and tags
# Required vars for login:
# @var ENV_API_URL : env-specific {{ cookiecutter.template_name }} API url
# @var ENV_API_TOKEN : env-specific {{ cookiecutter.template_name }} API token
# @var ENV_PROJECT : env-specific project name
.{{ cookiecutter.template_prefix }}-base
:
image
:
${{ cookiecutter.template_PREFIX }}_IMAGE
services
:
...
...
@@ -566,33 +583,22 @@ stages:
before_script
:
-
*
{{
cookiecutter.template_prefix
}}
-scripts
-
install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
-
{{
cookiecutter.template_prefix
}}
_login
# Deploy job prototype
# Can be extended to define a concrete environment
#
# @arg ENV_TYPE : environment type
# @arg ENV_APP_NAME : env-specific application name
# @arg ENV_APP_SUFFIX: env-specific application suffix
# @arg ENV_URL : env-specific application url
# @arg ENV_API_URL : env-specific {{ cookiecutter.template_name }} API url
# @arg ENV_API_TOKEN : env-specific {{ cookiecutter.template_name }} API token
# @arg ENV_PROJECT : env-specific project name
# @var ENV_TYPE : environment type
# @var ENV_APP_NAME : env-specific application name
# @var ENV_APP_SUFFIX: env-specific application suffix
# @var ENV_URL : env-specific application url
.{{ cookiecutter.template_prefix }}-deploy
:
extends
:
.{{ cookiecutter.template_prefix }}-base
stage
:
deploy
variables
:
ENV_APP_SUFFIX
:
"
-$CI_ENVIRONMENT_SLUG"
before_script
:
-
*
{{
cookiecutter.template_prefix
}}
-scripts
-
install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
-
assert_defined "${ENV_API_URL:-${{ cookiecutter.template_PREFIX }}_API_URL}" 'Missing required {{ cookiecutter.template_PREFIX }} API url'
-
assert_defined "${ENV_API_TOKEN:-${{ cookiecutter.template_PREFIX }}_API_TOKEN}" 'Missing required {{ cookiecutter.template_PREFIX }} API token'
-
assert_defined "$ENV_PROJECT" 'Missing required {{ cookiecutter.template_PREFIX }} project'
-
eval_secret "ENV_API_TOKEN"
-
eval_secret "{{ cookiecutter.template_PREFIX }}_API_TOKEN"
-
{{
cookiecutter.cli_tool
}}
login --url=${ENV_API_URL:-${{ cookiecutter.template_PREFIX }}_API_URL} --token=${ENV_API_TOKEN:-${{ cookiecutter.template_PREFIX }}_API_TOKEN} --project=$ENV_PROJECT
script
:
-
deploy "$ENV_TYPE" "${ENV_APP_NAME:-{{ '${' }}{{ cookiecutter.template_PREFIX }}_BASE_APP_NAME}${ENV_APP_SUFFIX}}" "${ENV_URL:-${{ cookiecutter.template_PREFIX }}_ENVIRONMENT_URL}"
-
{{
cookiecutter.template_prefix
}}
_deploy
artifacts
:
name
:
"
$ENV_TYPE
env
url
for
$CI_PROJECT_NAME
on
$CI_COMMIT_REF_SLUG"
# TODO: propagate deployed env url in a environment_url.txt file
...
...
@@ -607,12 +613,9 @@ stages:
# Cleanup job prototype
# Can be extended for each deletable environment
#
# @arg ENV_TYPE : environment type
# @arg ENV_APP_NAME : env-specific application name
# @arg ENV_APP_SUFFIX: env-specific application suffix
# @arg ENV_API_URL : env-specific {{ cookiecutter.template_name }} API url
# @arg ENV_API_TOKEN : env-specific {{ cookiecutter.template_name }} API token
# @arg ENV_PROJECT : env-specific {{ cookiecutter.template_name }} project name
# @var ENV_TYPE : environment type
# @var ENV_APP_NAME : env-specific application name
# @var ENV_APP_SUFFIX: env-specific application suffix
.{{ cookiecutter.template_prefix }}-cleanup
:
extends
:
.{{ cookiecutter.template_prefix }}-base
stage
:
deploy
...
...
@@ -620,21 +623,11 @@ stages:
dependencies
:
[]
variables
:
ENV_APP_SUFFIX
:
"
-$CI_ENVIRONMENT_SLUG"
before_script
:
-
*
{{
cookiecutter.template_prefix
}}
-scripts
-
install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
-
assert_defined "${ENV_API_URL:-${{ cookiecutter.template_PREFIX }}_API_URL}" 'Missing required {{ cookiecutter.template_PREFIX }} API url'
-
assert_defined "${ENV_API_TOKEN:-${{ cookiecutter.template_PREFIX }}_API_TOKEN}" 'Missing required {{ cookiecutter.template_PREFIX }} API token'
-
assert_defined "$ENV_PROJECT" 'Missing required {{ cookiecutter.template_PREFIX }} project'
-
eval_secret "ENV_API_TOKEN"
-
eval_secret "{{ cookiecutter.template_PREFIX }}_API_TOKEN"
-
{{
cookiecutter.cli_tool
}}
login --url=${ENV_API_URL:-${{ cookiecutter.template_PREFIX }}_API_URL} --token=${ENV_API_TOKEN:-${{ cookiecutter.template_PREFIX }}_API_TOKEN} --project=$ENV_PROJECT
script
:
-
delete "$ENV_TYPE" "${ENV_APP_NAME:-{{ '${' }}{{ cookiecutter.template_PREFIX }}_BASE_APP_NAME}${ENV_APP_SUFFIX}}"
-
{{
cookiecutter.template_prefix
}}
_delete
environment
:
action
:
stop
# deploy to review env (only on feature branches)
# disabled by default, enable this job by setting ${{ cookiecutter.template_PREFIX }}_REVIEW_PROJECT.
{{
cookiecutter.template_prefix
}}
-review
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment