Commit cd10c437 authored by Adrian DC's avatar Adrian DC
Browse files

feat: implement #245: add 'CI_LOCAL_USER_HOST_{GID,UID,USERNAME}'

Signed-off-by: default avatarAdrian DC <radian.dc@gmail.com>
parent 5f7555b8
Loading
Loading
Loading
Loading
+6 −0
Changes for README.md: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -174,6 +174,9 @@ For example, `-e TERM=ansi` may enable colored terminal outputs.
The variable `CI_LOCAL` is automatically defined to `true` by `gitlabci-local`  
to allow specific conditions for local purposes in jobs' scripts.

The variable `CI_LOCAL_HOST` is automatically defined to `true` by `gitlabci-local`  
if running the job natively on the host (for example wiht `--host`)

The following variables are also defined by `gitlabci-local`:

- `CI_COMMIT_REF_NAME`: The branch or tag name for which project is built (GitLab CI)
@@ -183,6 +186,9 @@ The following variables are also defined by `gitlabci-local`:
- `CI_COMMIT_SHORT_SHA`: The first eight characters of CI_COMMIT_SHA (GitLab CI)
- `CI_PROJECT_NAME`: The name of the directory for the project (GitLab CI)
- `CI_PROJECT_NAMESPACE`: The project namespace (username or group name) of the job (GitLab CI)
- `CI_LOCAL_USER_HOST_GID`: The host user's group ID value
- `CI_LOCAL_USER_HOST_UID`: The host user's user ID value
- `CI_LOCAL_USER_HOST_USERNAME`: The host user's username value

---

+8 −0
Changes for gitlabci_local/jobs/runner.py: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -571,6 +571,11 @@ class Runner:
            environ[Bundle.ENV_COMMIT_SHORT_SHA1] = git.head_revision_short_hash(
                path_project)

        # Prepare user variables
        environ[Bundle.ENV_USER_HOST_GID] = str(Platform.getgid())
        environ[Bundle.ENV_USER_HOST_UID] = str(Platform.getuid())
        environ[Bundle.ENV_USER_HOST_USERNAME] = Platform.getusername()

        # Prepare CI variables
        variables[env_job_name] = environ[env_job_name]
        variables[env_job_path] = environ[env_job_path]
@@ -581,6 +586,9 @@ class Runner:
        variables[Bundle.ENV_LOCAL] = environ[Bundle.ENV_LOCAL]
        variables[Bundle.ENV_PROJECT_NAME] = environ[Bundle.ENV_PROJECT_NAME]
        variables[Bundle.ENV_PROJECT_NAMESPACE] = environ[Bundle.ENV_PROJECT_NAMESPACE]
        variables[Bundle.ENV_USER_HOST_GID] = environ[Bundle.ENV_USER_HOST_GID]
        variables[Bundle.ENV_USER_HOST_UID] = environ[Bundle.ENV_USER_HOST_UID]
        variables[Bundle.ENV_USER_HOST_USERNAME] = environ[Bundle.ENV_USER_HOST_USERNAME]

        # Prepare job variables
        for variable in variables:
+3 −0
Changes for gitlabci_local/package/bundle.py: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -48,6 +48,9 @@ class Bundle:
    ENV_UPDATES_DISABLE: str = 'CI_LOCAL_UPDATES_DISABLE'
    ENV_UPDATES_FAKE: str = 'CI_LOCAL_UPDATES_FAKE'
    ENV_UPDATES_OFFLINE: str = 'CI_LOCAL_UPDATES_OFFLINE'
    ENV_USER_HOST_GID: str = 'CI_LOCAL_USER_HOST_GID'
    ENV_USER_HOST_UID: str = 'CI_LOCAL_USER_HOST_UID'
    ENV_USER_HOST_USERNAME: str = 'CI_LOCAL_USER_HOST_USERNAME'
    ENV_VERSION_FAKE: str = 'CI_LOCAL_VERSION_FAKE'
    ENV_WINPTY: str = 'CI_LOCAL_WINPTY'
    ENV_WINPTY_PATH: str = 'WINPTY_BINARY_PATH'
+33 −0
Changes for gitlabci_local/system/platform.py: 33 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -71,6 +71,39 @@ class Platform:
        # Flush output
        print('', end='', flush=Platform.IS_TTY_STDOUT)

    # Get GID
    @staticmethod
    def getgid() -> int:

        # Result, pylint: disable=import-outside-toplevel
        try:
            from os import getgid
            return getgid()
        except ImportError:
            return 0

    # Get UID
    @staticmethod
    def getuid() -> int:

        # Result, pylint: disable=import-outside-toplevel
        try:
            from os import getuid
            return getuid()
        except ImportError:
            return 0

    # Get username
    @staticmethod
    def getusername() -> str:

        # Result, pylint: disable=import-outside-toplevel
        try:
            from getpass import getuser
            return getuser()
        except ImportError:
            return 'root'

    # Userspace
    @staticmethod
    def userspace(name: str) -> Path:
+22 −0
Changes for tests/local/.gitlab-ci.yml: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ stages:
  - local_seventh
  - local_eighth
  - local_ninth
  - local_tenth

.local:
  after: true
@@ -87,6 +88,7 @@ variables:
  before_script:
    - echo '__BEFORE__'
  script:
    - set -x
    - pwd
    - ls /etc/alpine-release && exit 1 || true
    - uname -a
@@ -103,6 +105,7 @@ variables:
  before_script:
    - echo '__BEFORE__'
  script:
    - set -x
    - ls -la /opt/src/
    - ls -la /opt/parent/
    - touch /opt/parent/test && exit 1 || true
@@ -175,6 +178,7 @@ variables:
  before_script:
    - echo '__BEFORE__'
  script:
    - set -x
    - echo "CI_LOCAL=${CI_LOCAL}"
    - test -z "${CI_LOCAL}" && exit 1 || true
    - echo "CI_LOCAL_ENGINE=${CI_LOCAL_ENGINE}"
@@ -189,6 +193,7 @@ variables:
  variables:
    VARIABLE_JOB: 'JOB_VALUE'
  script:
    - set -x
    - echo "HOSTNAME=${HOSTNAME}"
    - test -z "${HOSTNAME}" && exit 1 || true
    - echo "MISSING=${MISSING}"
@@ -208,3 +213,20 @@ variables:
    - echo "VARIABLE_NEW=${VARIABLE_NEW}"
    - test ! "${VARIABLE_NEW}" = 'value_1' && exit 1 || true
  when: manual

'Job 10':
  stage: local_tenth
  image: registry.gitlab.com/adriandc/gitlabci-local/alpine:3
  before_script:
    - echo '__BEFORE__'
  script:
    - set -x
    - echo "CI_LOCAL_HOST=${CI_LOCAL_HOST}"
    - echo "CI_LOCAL_USER_HOST_GID=${CI_LOCAL_USER_HOST_GID}"
    - echo "CI_LOCAL_USER_HOST_UID=${CI_LOCAL_USER_HOST_UID}"
    - echo "CI_LOCAL_USER_HOST_USERNAME=${CI_LOCAL_USER_HOST_USERNAME}"
    - test -z "${CI_LOCAL_USER_HOST_GID}" && exit 1 || true
    - test -z "${CI_LOCAL_USER_HOST_UID}" && exit 1 || true
    - test -z "${CI_LOCAL_USER_HOST_USERNAME}" && exit 1 || true
  after_script:
    - echo '__AFTER__'
Loading