1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GitHub Codespacesでの実行か、GitHub Actions上での実行かを環境変数で判定する

Last updated at Posted at 2023-08-04

GitHub Codespacesで開発して、GitHub Actionsにデプロイするときにデプロイ先なのかローカル(とは言わなそう)なのかの判定をして処理を振り分けたい時があります。

コピペ用振り分け

こんな感じで環境変数で振り分けができました。 2023/8/4時点の環境なので仕様が変わる可能性はあります。


if(process.env.CODESPACES){
  //Codespacesでの実行
}else if(process.env.GITHUB_ACTIONS){
  //GitHub Actionsでの実行
}

それぞれ独自の環境変数

GitHub Codespacesの場合は環境変数に{"CODESPACES": 'ture' }があり、GitHub Actionsの場合は{"GITHUB_ACIONS": 'ture' }が入っています。

  • Codespaces

スクリーンショット 2023-08-04 17.57.50.png

  • Actions

スクリーンショット 2023-08-04 18.15.06.png

GitHub Actionsの環境変数

参考までに出力した値を載せておきます。

{
  "GITHUB_STATE": "XXXXX",
  "STATS_TRP": "true",
  "DEPLOYMENT_BASEPATH": "/opt/runner",
  "DOTNET_NOLOGO": "1",
  "USER": "XXXXX",
  "npm_config_version_commit_hooks": "true",
  "npm_config_user_agent": "yarn/1.22.19 npm/? node/v20.5.0 linux x64",
  "CI": "true",
  "npm_config_bin_links": "true",
  "RUNNER_ENVIRONMENT": "github-hosted",
  "GITHUB_ENV": "XXXXX",
  "PIPX_HOME": "/opt/pipx",
  "npm_node_execpath": "/opt/hostedtoolcache/node/20.5.0/x64/bin/node",
  "npm_config_init_version": "1.0.0",
  "JAVA_HOME_8_X64": "/usr/lib/jvm/temurin-8-jdk-amd64",
  "SHLVL": "1",
  "HOME": "XXXXX",
  "RUNNER_TEMP": "XXXXX",
  "GITHUB_EVENT_PATH": "XXXXX",
  "JAVA_HOME_11_X64": "/usr/lib/jvm/temurin-11-jdk-amd64",
  "PIPX_BIN_DIR": "/opt/pipx_bin",
  "GITHUB_REPOSITORY_OWNER": "XXXXX",
  "npm_config_init_license": "MIT",
  "GRADLE_HOME": "/usr/share/gradle-8.2.1",
  "ANDROID_NDK_LATEST_HOME": "/usr/local/lib/android/sdk/ndk/25.2.9519653",
  "STATS_RDCL": "true",
  "GITHUB_RETENTION_DAYS": "90",
  "YARN_WRAP_OUTPUT": "false",
  "npm_config_version_tag_prefix": "v",
  "GITHUB_REPOSITORY_OWNER_ID": "XXXXX",
  "POWERSHELL_DISTRIBUTION_CHANNEL": "GitHub-Actions-ubuntu22",
  "AZURE_EXTENSION_DIR": "/opt/az/azcliextensions",
  "GITHUB_HEAD_REF": "",
  "SYSTEMD_EXEC_PID": "662",
  "npm_package_dependencies_dotenv": "^16.3.1",
  "npm_package_dependencies_dayjs": "^1.11.9",
  "npm_package_dependencies__notionhq_client": "^2.2.10",
  "GITHUB_GRAPHQL_URL": "https://api.github.com/graphql",
  "npm_package_description": "XXXXX",
  "GOROOT_1_20_X64": "/opt/hostedtoolcache/go/1.20.6/x64",
  "NVM_DIR": "XXXXX",
  "npm_package_readmeFilename": "readme.md",
  "DOTNET_SKIP_FIRST_TIME_EXPERIENCE": "1",
  "JAVA_HOME_17_X64": "/usr/lib/jvm/temurin-17-jdk-amd64",
  "ImageVersion": "20230728.3.0",
  "RUNNER_OS": "Linux",
  "GITHUB_API_URL": "https://api.github.com",
  "SWIFT_PATH": "/usr/share/swift/usr/bin",
  "RUNNER_USER": "XXXXX",
  "STATS_V3PS": "true",
  "CHROMEWEBDRIVER": "/usr/local/share/chromedriver-linux64",
  "JOURNAL_STREAM": "8:16374",
  "GITHUB_WORKFLOW": "60分ごと実行",
  "_": "/usr/local/bin/yarn",
  "npm_config_registry": "https://registry.yarnpkg.com",
  "GITHUB_RUN_ID": "XXXXX",
  "GITHUB_REF_TYPE": "branch",
  "BOOTSTRAP_HASKELL_NONINTERACTIVE": "1",
  "GITHUB_WORKFLOW_SHA": "XXXXX",
  "GITHUB_BASE_REF": "",
  "ImageOS": "ubuntu22",
  "npm_package_scripts_start": "node app.js",
  "npm_config_ignore_scripts": "",
  "GITHUB_WORKFLOW_REF": "XXXXX",
  "PERFLOG_LOCATION_SETTING": "RUNNER_PERFLOG",
  "GOROOT_1_18_X64": "/opt/hostedtoolcache/go/1.18.10/x64",
  "GITHUB_ACTION_REPOSITORY": "",
  "PATH": "XXXXX",
  "NODE": "/opt/hostedtoolcache/node/20.5.0/x64/bin/node",
  "ANT_HOME": "/usr/share/ant",
  "DOTNET_MULTILEVEL_LOOKUP": "0",
  "RUNNER_TRACKING_ID": "github_90121ade-274c-493e-9782-752f7e7a40c5",
  "INVOCATION_ID": "254f81b7aa8c4cbbb7ffbc34543084ec",
  "RUNNER_TOOL_CACHE": "/opt/hostedtoolcache",
  "GOROOT_1_19_X64": "/opt/hostedtoolcache/go/1.19.11/x64",
  "npm_package_name": "notion2discord",
  "GITHUB_ACTION": "__run_2",
  "GITHUB_RUN_NUMBER": "XXXXX",
  "GITHUB_TRIGGERING_ACTOR": "XXXXX",
  "RUNNER_ARCH": "X64",
  "XDG_RUNTIME_DIR": "/run/user/1001",
  "AGENT_TOOLSDIRECTORY": "/opt/hostedtoolcache",
  "LANG": "C.UTF-8",
  "VCPKG_INSTALLATION_ROOT": "/usr/local/share/vcpkg",
  "CONDA": "/usr/share/miniconda",
  "RUNNER_NAME": "GitHub Actions 2",
  "XDG_CONFIG_HOME": "XXXXX",
  "STATS_VMD": "true",
  "GITHUB_REF_NAME": "main",
  "GITHUB_REPOSITORY": "XXXXX",
  "npm_lifecycle_script": "node app.js",
  "npm_package_main": "index.js",
  "STATS_UE": "true",
  "ANDROID_NDK_ROOT": "/usr/local/lib/android/sdk/ndk/25.2.9519653",
  "GITHUB_ACTION_REF": "",
  "DEBIAN_FRONTEND": "noninteractive",
  "npm_config_version_git_message": "v%s",
  "GITHUB_REPOSITORY_ID": "XXXXX",
  "GITHUB_ACTIONS": "true",
  "STATS_NM": "true",
  "npm_lifecycle_event": "start",
  "npm_package_version": "1.0.1",
  "GITHUB_REF_PROTECTED": "false",
  "npm_config_argv": "{\"remain\":[],\"cooked\":[\"run\",\"start\"],\"original\":[\"start\"]}",
  "GITHUB_WORKSPACE": "XXXXX",
  "ACCEPT_EULA": "Y",
  "GITHUB_JOB": "build",
  "RUNNER_PERFLOG": "/home/runner/perflog",
  "GITHUB_SHA": "XXXXX",
  "GITHUB_RUN_ATTEMPT": "2",
  "npm_config_version_git_tag": "true",
  "npm_config_version_git_sign": "",
  "GITHUB_REF": "refs/heads/main",
  "GITHUB_ACTOR": "XXXXX",
  "ANDROID_SDK_ROOT": "/usr/local/lib/android/sdk",
  "npm_package_license": "ISC",
  "npm_config_strict_ssl": "true",
  "LEIN_HOME": "/usr/local/lib/lein",
  "GITHUB_PATH": "XXXXX",
  "JAVA_HOME": "/usr/lib/jvm/temurin-11-jdk-amd64",
  "PWD": "XXXXX",
  "GITHUB_ACTOR_ID": "XXXXX",
  "RUNNER_WORKSPACE": "XXXXX",
  "npm_execpath": "/usr/local/lib/node_modules/yarn/bin/yarn.js",
  "HOMEBREW_CLEANUP_PERIODIC_FULL_DAYS": "3650",
  "STATS_TIS": "mining",
  "GITHUB_EVENT_NAME": "push",
  "HOMEBREW_NO_AUTO_UPDATE": "1",
  "ANDROID_HOME": "/usr/local/lib/android/sdk",
  "GITHUB_SERVER_URL": "https://github.com",
  "GECKOWEBDRIVER": "/usr/local/share/gecko_driver",
  "LEIN_JAR": "/usr/local/lib/lein/self-installs/leiningen-2.10.0-standalone.jar",
  "GHCUP_INSTALL_BASE_PREFIX": "/usr/local",
  "GITHUB_OUTPUT": "XXXXX",
  "EDGEWEBDRIVER": "/usr/local/share/edge_driver",
  "STATS_EXT": "true",
  "npm_config_save_prefix": "^",
  "npm_config_ignore_optional": "",
  "ANDROID_NDK": "/usr/local/lib/android/sdk/ndk/25.2.9519653",
  "SGX_AESM_ADDR": "1",
  "CHROME_BIN": "/usr/bin/google-chrome",
  "npm_package_scripts_deploy": "git add . && git commit -m 'deploy' && git push origin main",
  "SELENIUM_JAR_PATH": "/usr/share/java/selenium-server.jar",
  "STATS_EXTP": "https://provjobdsettingscdn.blob.core.windows.net/settings/provjobdsettings-0.5.154/provjobd.data",
  "INIT_CWD": "XXXXX",
  "ANDROID_NDK_HOME": "/usr/local/lib/android/sdk/ndk/25.2.9519653",
  "GITHUB_STEP_SUMMARY": "XXXXX"
}

よもやま

ChatGPTにマスクしてーってお願いしたけどマスクしてくれてるのだろうか

スクリーンショット 2023-08-04 18.46.52.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?