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

Add CI_SERVICE_alias environment variables to make referencing the FQDN of services fun! #27

Merged
merged 3 commits into from
Aug 5, 2024

Conversation

pauldoomgov
Copy link
Contributor

This addresses #7

This change injects an environment variable named CI_SERVICE_<alias> for each service and sets the value to the fully qualified domain name of the service. Here is an example snippet of .gitlab-ci.yml showing its use:

echo-test-job:
  stage: test
  image: public.ecr.aws/docker/library/ubuntu:jammy
  services:
  - name: ghcr.io/mendhak/http-https-echo:latest
    alias: echo
  script:
  - curl http://${CI_SERVICE_echo}:8080

The old method to get to the same outcome required cobbling the variable together from 5 other variables.

Instead of attempting to hack the GitLab generated command list, we just calculate and inject the CI_SERVICE_alias variables in prepare.sh using a modified CloudFoundry manifest. It might be hideous, but since the need to calculate a service name dynamically is not present in Docker, Kubernetes, and other executors with ephemeral local network namespaces, it seems right.

`config.sh` prints JSON to STDOUT following the expected form defined in
https://docs.gitlab.com/runner/executors/custom.html#config

This change also moves `base.sh` warnings to STDERR.
Much ado about little here. Some restructuring was needed in order
to populate a CI_SERVICE_<alias> variables as services are started,
then inject them into a temporary manifest. On the way I found
that the old TMPVARFILE was not working correctly. I 🪿 love 🪿 bash🪿!!!
@pauldoomgov
Copy link
Contributor Author

Pipeline proof of life for folks who want "evidence" - https://gitlab.com/pauldoomgov/hyperloop-traffic-jam/-/pipelines/1399063314

Copy link
Contributor

@zjrgov zjrgov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remember exactly which thing it is that voids approvals… but hopefully none of these comments will.

cat "${currentDir}/worker-manifest.yml" > "$TMPMANIFEST"

# Align additional environment variables with YAML at end of source manifest
local padding=" "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought (non-blocking): 😨

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, right? 🧌

Comment on lines +13 to +18
echo "WARNING: DEFAULT_JOB_IMAGE not set! Falling back to ${DEFAULT_JOB_IMAGE}" 1>&2
fi

# Complain if no Docker Hub credentials so we aren't bad neighbors
if [ -z "$DOCKER_HUB_USER" ] || [ -z "$DOCKER_HUB_TOKEN" ]; then
echo "WARNING: Docker Hub credentials not set! Falling back to public access which could result in rate limiting."
echo "WARNING: Docker Hub credentials not set! Falling back to public access which could result in rate limiting." 1>&2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: good thinking

Comment on lines +56 to +59
# Add any CI_SERVICE_x variables populated by start_service()
for v in "${!CI_SERVICE_@}"; do
echo "${padding}${v}: \"${!v}\"" >> "$TMPMANIFEST"
done
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: the indirection is pretty nifty, I haven't used that before

@pauldoomgov pauldoomgov merged commit e3669e8 into main Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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