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

Populate a CI_SERVICE_alias from the worker side for each service to allow easy reference from .gitlab-ci.yml #7

Closed
pauldoomgov opened this issue Jun 21, 2024 · 2 comments · Fixed by #27
Assignees
Labels
enhancement New feature or request

Comments

@pauldoomgov
Copy link
Contributor

pauldoomgov commented Jun 21, 2024

With #5 we added service workers but calculating the hostname inside gitlab-ci.yml requires this mess:

variables:
  SERVICE_PREFIX: "glrw-r$CI_RUNNER_ID-p$CI_PROJECT_ID-c$CI_CONCURRENT_PROJECT_ID-j$CI_JOB_ID-svc-"

That is ugly! It would be nice to have a variable like CI_SERVICE_PREFIX available in all jobs as a preset environment variable so steps could use it to find services, and CI versions of config files could reference it directly.

Update instead of a prefix, how about generating a FQDN? Yeah. Let's do that.

@pauldoomgov pauldoomgov added the enhancement New feature or request label Jun 21, 2024
@zjrgov
Copy link
Contributor

zjrgov commented Jul 24, 2024

Bonus item: do we want to do anything extra for URLs? i.e. http://${service_prefix}website.apps.internal?

@pauldoomgov
Copy link
Contributor Author

I tried a few approaches to this. For posterity, here is one I abandoned that may come in handy later if we need to have a persistent variable set on the manager side that is visible to all scripts (prepare.sh, run.sh, etc):

config.sh:

#!/usr/bin/env bash
#
# Generate custom configuration JSON - https://docs.gitlab.com/runner/executors/custom.html#config
# STDOUT needs to be well formed JSON or this gets ignored.

currentDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
source "${currentDir}/base.sh" # Get variables from base.

SERVICE_PREFIX="${CONTAINER_ID}-svc-"

CUSTOM_ENVIRONMENT=()

if [ -n "$CUSTOM_ENV_CI_JOB_SERVICES" ]; then
    CUSTOM_ENVIRONMENT+=$(echo "$CUSTOM_ENV_CI_JOB_SERVICES" | jq ".[] | {(\"CUSTOM_ENV_SERVICE_\" + .alias): (\"${SERVICE_PREFIX}\" + .alias + \".apps.internal\")}")
fi

if [[ -z ${CUSTOM_ENVIRONMENT[@]} ]]; then
    exit 0
fi

echo "[cf-driver] Adding configuration from config.sh" 1>&2

cat << EOS
{
  "job_env" : $( echo ${CUSTOM_ENVIRONMENT[@]} | jq -s 'add' )
}
EOS

To use this you also need to setup use of a config_exec step by setting the CUSTOM_CONFIG_EXEC variable to "/home/vcap/app/cf-driver/config.sh" in /manifest.yml.

If you use this read the linked documentation carefully. It does not populate variables into the jobs.

@pauldoomgov pauldoomgov changed the title Populate a CI_SERVICE_PREFIX from the worker side to allow easy reference from .gitlab-ci.yml Populate a CI_SERVICE_alias from the worker side for each service to allow easy reference from .gitlab-ci.yml Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants