diff --git a/README.md b/README.md index 86a32c0..57246e8 100644 --- a/README.md +++ b/README.md @@ -30,3 +30,12 @@ We welcome [issues](https://github.com/game-ci/unity-orb/issues) to and [pull re 5. Now ensure the version tag selected is semantically accurate based on the changes included. 6. Click _"Publish Release"_. - This will push a new tag and trigger your publishing pipeline on CircleCI. + +### Manual Deploy +If you want a private orb for your build env. The following steps allow you to do so. These are adapted from the CircleCI +[Manual Orb Authoring Process](https://circleci.com/docs/orb-author-validate-publish/#publish-your-orb) +1. `circleci namespace create <name> --org-id <your-organization-id>` +2. `circleci orb create <my-namespace>/<my-orb-name> --private` +3. `circleci orb pack src > unity-orb.yml` +4. `circleci orb publish unity-orb.yml <my-namespace>/<my-orb-name>@dev:first` +5. `circleci orb publish promote <my-namespace>/<my-orb-name>@dev:first patch` diff --git a/src/commands/prepare-env.yml b/src/commands/prepare-env.yml index 32e7372..6869289 100644 --- a/src/commands/prepare-env.yml +++ b/src/commands/prepare-env.yml @@ -24,6 +24,11 @@ parameters: description: | Enter the name of the environment variable containing your Unity license file encoded in base64. Required if you have a Personal license. + git-private-token-var-name: + type: env_var_name + default: "GIT_PRIVATE_TOKEN" + description: | + Enter your Github PAT that you want to pass to the credential manager cache-version: type: string default: "v1" @@ -64,3 +69,15 @@ steps: SCRIPT_PREPARE_ENV_MACOS: << include(scripts/macos/prepare-env.sh) >> SCRIPT_UTILS: << include(scripts/utils.sh) >> command: << include(scripts/prepare-env.sh) >> + - run: + name: Set Git Credentials + no_output_timeout: << parameters.no_output_timeout >> + environment: + PARAM_GIT_PRIVATE_TOKEN: << parameters.git-private-token-var-name >> + PARAM_PROJECT_PATH: << parameters.project-path >> + SCRIPT_UTILS: << include(scripts/utils.sh) >> + SCRIPT_GIT_CREDENTIAL_LINUX: << include(scripts/linux/set-gitcredential.sh) >> + SCRIPT_GIT_CREDENTIAL_WINDOWS: << include(scripts/windows/set-gitcredential.sh) >> + SCRIPT_GIT_CREDENTIAL_MACOS: << include(scripts/macos/set-gitcredential.sh) >> + command: << include(scripts/set-gitcredential.sh) >> + diff --git a/src/jobs/build.yml b/src/jobs/build.yml index 9170c0f..b105dc3 100755 --- a/src/jobs/build.yml +++ b/src/jobs/build.yml @@ -33,6 +33,11 @@ parameters: description: | Enter the name of the environment variable containing your Unity license file encoded in base64. Required if you have a Personal license. + git-private-token-var-name: + type: env_var_name + default: "GIT_PRIVATE_TOKEN" + description: | + Enter your Github PAT that you want to pass to the credential manager project-path: type: string default: "." @@ -113,6 +118,7 @@ steps: unity-password-var-name: << parameters.unity-password-var-name >> unity-serial-var-name: << parameters.unity-serial-var-name >> unity-license-var-name: << parameters.unity-license-var-name >> + git-private-token-var-name: << parameters.git-private-token-var-name >> project-path: <<parameters.project-path>> no_output_timeout: << parameters.no_output_timeout>> - build: diff --git a/src/scripts/linux/set-gitcredential.sh b/src/scripts/linux/set-gitcredential.sh new file mode 100644 index 0000000..3cc7bc0 --- /dev/null +++ b/src/scripts/linux/set-gitcredential.sh @@ -0,0 +1,25 @@ +#!/bin/false +# shellcheck shell=bash +# shellcheck disable=SC2154 + +if [ -z "${GIT_PRIVATE_TOKEN}" ] +then + echo "GIT_PRIVATE_TOKEN unset skipping" +else + echo "GIT_PRIVATE_TOKEN is set configuring git credentials" + + git config --global credential.helper store + git config --global --replace-all url."https://token:$GIT_PRIVATE_TOKEN@github.com/".insteadOf ssh://git@github.com/ + git config --global --add url."https://token:$GIT_PRIVATE_TOKEN@github.com/".insteadOf git@github.com + git config --global --add url."https://token:$GIT_PRIVATE_TOKEN@github.com/".insteadOf "https://github.com/" + + git config --global url."https://ssh:$GIT_PRIVATE_TOKEN@github.com/".insteadOf "ssh://git@github.com/" + git config --global url."https://git:$GIT_PRIVATE_TOKEN@github.com/".insteadOf "git@github.com:" + +fi + +echo "---------- git config --list -------------" +git --no-pager config --list + +echo "---------- git config --list --show-origin -------------" +git --no-pager config --list --show-origin \ No newline at end of file diff --git a/src/scripts/macos/set-gitcredential.sh b/src/scripts/macos/set-gitcredential.sh new file mode 100644 index 0000000..3cc7bc0 --- /dev/null +++ b/src/scripts/macos/set-gitcredential.sh @@ -0,0 +1,25 @@ +#!/bin/false +# shellcheck shell=bash +# shellcheck disable=SC2154 + +if [ -z "${GIT_PRIVATE_TOKEN}" ] +then + echo "GIT_PRIVATE_TOKEN unset skipping" +else + echo "GIT_PRIVATE_TOKEN is set configuring git credentials" + + git config --global credential.helper store + git config --global --replace-all url."https://token:$GIT_PRIVATE_TOKEN@github.com/".insteadOf ssh://git@github.com/ + git config --global --add url."https://token:$GIT_PRIVATE_TOKEN@github.com/".insteadOf git@github.com + git config --global --add url."https://token:$GIT_PRIVATE_TOKEN@github.com/".insteadOf "https://github.com/" + + git config --global url."https://ssh:$GIT_PRIVATE_TOKEN@github.com/".insteadOf "ssh://git@github.com/" + git config --global url."https://git:$GIT_PRIVATE_TOKEN@github.com/".insteadOf "git@github.com:" + +fi + +echo "---------- git config --list -------------" +git --no-pager config --list + +echo "---------- git config --list --show-origin -------------" +git --no-pager config --list --show-origin \ No newline at end of file diff --git a/src/scripts/set-gitcredential.sh b/src/scripts/set-gitcredential.sh new file mode 100644 index 0000000..3331cfe --- /dev/null +++ b/src/scripts/set-gitcredential.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2034 + +readonly base_dir="${CIRCLE_WORKING_DIRECTORY/\~/$HOME}" +readonly unity_project_full_path="$base_dir/$PARAM_PROJECT_PATH" + +# Import "utils.sh". +eval "$SCRIPT_UTILS" + +# Detect host OS. +detect-os + +# Expand environment name variable parameters. +readonly git_private_token="${!PARAM_GIT_PRIVATE_TOKEN}" + +if [ "$PLATFORM" = "linux" ]; then eval "$SCRIPT_GIT_CREDENTIAL_LINUX"; +elif [ "$PLATFORM" = "macos" ]; then eval "$SCRIPT_GIT_CREDENTIAL_MACOS"; +elif [ "$PLATFORM" = "windows" ]; then eval "$SCRIPT_GIT_CREDENTIAL_WINDOWS"; +fi diff --git a/src/scripts/windows/return-license.sh b/src/scripts/windows/return-license.sh index 345bed7..365a8e0 100644 --- a/src/scripts/windows/return-license.sh +++ b/src/scripts/windows/return-license.sh @@ -3,19 +3,35 @@ # shellcheck disable=SC2154 trap_exit() { - local exit_status="$?" - - if [ "$exit_status" -ne 0 ]; then - printf '%s\n' 'The script did not complete successfully.' - - printf '%s\n' "Removing the container \"$CONTAINER_NAME\"." - docker rm -f "$CONTAINER_NAME" &> /dev/null || true - - exit "$exit_status" - fi + local exit_status="$?" + + if [ "$exit_status" -ne 0 ]; then + printf '%s\n' 'The script did not complete successfully.' + + printf '%s\n' "Removing the container \"$CONTAINER_NAME\"." + docker rm -f "$CONTAINER_NAME" &> /dev/null || true + + exit "$exit_status" + fi } trap trap_exit EXIT +running_builds=$(docker ps --filter "Name=$CONTAINER_NAME" --format "{{.ID}}") +if [ -z "$running_builds" ]; then + # The build failed before and it killed the host + # Startup so we can return the license + docker run -dit \ + --name "$CONTAINER_NAME" \ + --env PROJECT_PATH="C:/unity_project" \ + --env UNITY_USERNAME="$unity_username" \ + --env UNITY_PASSWORD="$unity_password" \ + --volume "C:/Program Files (x86)/Microsoft Visual Studio":"C:/Program Files (x86)/Microsoft Visual Studio" \ + --volume "C:/Program Files (x86)/Windows Kits":"C:/Program Files (x86)/Windows Kits" \ + --volume "C:/ProgramData/Microsoft/VisualStudio":"C:/ProgramData/Microsoft/VisualStudio" \ + "unityci/editor:windows-${GAMECI_EDITOR_VERSION}-${GAMECI_TARGET_PLATFORM}-2" \ + powershell +fi + set -x # Return license docker exec "$CONTAINER_NAME" powershell '& "C:\Program Files\Unity\Hub\Editor\*\Editor\Unity.exe" -returnlicense -batchmode -quit -nographics -username $Env:UNITY_USERNAME -password $Env:UNITY_PASSWORD -logfile | Out-Host' diff --git a/src/scripts/windows/set-gitcredential.sh b/src/scripts/windows/set-gitcredential.sh new file mode 100644 index 0000000..6451b5c --- /dev/null +++ b/src/scripts/windows/set-gitcredential.sh @@ -0,0 +1,53 @@ +#!/bin/false +# shellcheck shell=bash +# shellcheck disable=SC2016,SC2154 + +readonly base_dir="${CIRCLE_WORKING_DIRECTORY/\~/$HOME}" +readonly unity_project_full_path="$base_dir/$PARAM_PROJECT_PATH" + +# Import "utils.sh". +eval "$SCRIPT_UTILS" + +# Detect host OS. +detect-os + +# Expand environment name variable parameters. +readonly git_private_token="${!PARAM_GIT_PRIVATE_TOKEN}" +readonly CONTAINER_NAME="${CIRCLE_PROJECT_REPONAME}-${CIRCLE_BUILD_NUM}" + +trap_exit() { + local exit_status="$?" + + if [ "$exit_status" -ne 0 ]; then + printf '%s\n' 'The script did not complete successfully.' + + printf '%s\n' "Removing the container \"$CONTAINER_NAME\"." + docker rm -f "$CONTAINER_NAME" &> /dev/null || true + + exit "$exit_status" + fi +} +trap trap_exit EXIT + + +if [ -z "${git_private_token}" ] +then + echo "GIT_PRIVATE_TOKEN unset skipping" +else + echo "GIT_PRIVATE_TOKEN is set configuring git credentials" + + docker exec "$CONTAINER_NAME" "git config --global credential.helper store" + docker exec "$CONTAINER_NAME" "git config --global --replace-all url.\"https://token:$git_private_token@github.com/\".insteadOf ssh://git@github.com/" + docker exec "$CONTAINER_NAME" "git config --global --add url.\"https://token:$git_private_token@github.com/\".insteadOf git@github.com" + docker exec "$CONTAINER_NAME" "git config --global --add url.\"https://token:$git_private_token@github.com/\".insteadOf \"https://github.com/\"" + + docker exec "$CONTAINER_NAME" "git config --global url.\"https://ssh:$git_private_token@github.com/\".insteadOf \"ssh://git@github.com/\"" + docker exec "$CONTAINER_NAME" "git config --global url.\"https://git:$git_private_token@github.com/\".insteadOf \"git@github.com:\"" + +fi + +echo "---------- git config --list -------------" +docker exec "$CONTAINER_NAME" "git --no-pager config --list" + +echo "---------- git config --list --show-origin -------------" +docker exec "$CONTAINER_NAME" "git --no-pager config --list --show-origin" \ No newline at end of file