Skip to content

Commit

Permalink
[ci] Added CI scripts from zardan/ui, modified to suit this repo
Browse files Browse the repository at this point in the history
  • Loading branch information
applejag committed Apr 24, 2019
1 parent 93514b0 commit 030d1c5
Show file tree
Hide file tree
Showing 19 changed files with 1,718 additions and 0 deletions.
190 changes: 190 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
version: 2.1

preset-filter1: &WORKFLOW_BUILD_TEST_FILTER
only:
- develop
- /feature.*/

preset-filter2: &WORKFLOW_BUILD_TEST_DEPLOY_FILTER
only:
- master

jobs:
# Job named 'build' is used on API triggered builds
build-test:
working_directory: ~/repo

environment:
SCRIPTS: /root/repo/ci/scripts/

docker:
- image: zifrose/kjell-testrunner

steps:
- add_ssh_keys:
fingerprints:
# SSH user key to github.com for UPM to access private repos
- "68:91:51:8b:69:ba:53:6f:53:4a:1b:79:b5:59:9f:6a"

# Checkout repo -> ~/repo
- checkout

- run:
name: Enable permissions on scripts
command: chmod +x $SCRIPTS/**.sh -v

- run:
name: Setup Unity license
command: $SCRIPTS/unity_login.sh

- run:
name: Update UPM dependencies & compile scripts
command: $SCRIPTS/unity_update_deps.sh ~/repo

- run:
name: Obtaining Playground UI & Mellis version
command: |
shopt -s globstar
# Get versions
PLAYGROUND_CARCONTROLLER_VERSION="$($SCRIPTS/get_attr_version.sh ~/repo/Assets/**/CarControllerAssemblyInfo.cs)"
echo ">>> Using Playground Kjell UI v$PLAYGROUND_CARCONTROLLER_VERSION"
PLAYGROUND_UI_VERSION="$($SCRIPTS/get_attr_version.sh ~/repo/Library/PackageCache/se.zifro.ui*/**/ZifroPlaygroundUIAssemblyInfo.cs)"
echo ">>> Using Playground UI v$PLAYGROUND_UI_VERSION"
MELLIS_VERSION="$($SCRIPTS/get_dll_version.sh ~/repo/Library/PackageCache/se.zifro.mellis*/**/Mellis.dll)"
MELLIS_PYTHON3_VERSION="$($SCRIPTS/get_dll_version.sh ~/repo/Library/PackageCache/se.zifro.mellis*/**/Mellis.Lang.Python3.dll)"
echo ">>> Using Mellis v$MELLIS_VERSION, and Python3 module v$MELLIS_PYTHON3_VERSION"
# Save to environment
echo "PLAYGROUND_CARCONTROLLER_VERSION='$PLAYGROUND_CARCONTROLLER_VERSION'" >> $BASH_ENV
echo "PLAYGROUND_UI_VERSION='$PLAYGROUND_UI_VERSION'" >> $BASH_ENV
echo "MELLIS_VERSION='$MELLIS_VERSION'" >> $BASH_ENV
echo "MELLIS_PYTHON3_VERSION='$MELLIS_PYTHON3_VERSION'" >> $BASH_ENV
- run:
name: Test Unity project
command: $SCRIPTS/unity_test.sh ~/repo ~/tests/nunit/

- run:
name: Convert NUnit to JUnit xml
when: always
command: $SCRIPTS/nunit2junit.sh ~/tests/nunit/ ~/tests/junit/

- store_test_results:
name: store test results -> ~/tests/junit
path: ~/tests/junit

- run:
command: echo 'export BUILD_STATUS="fail"' >> $BASH_ENV
name: Setting Failure Condition
when: on_fail
- run:
command: echo 'export BUILD_STATUS="success"' >> $BASH_ENV
name: Setting Success Condition
when: on_success

- run:
name: Send Slack notification
when: always
command: $SCRIPTS/slack_notify_testrunner.sh

# deploy-github-webgl:
# working_directory: ~/repo

# environment:
# SCRIPTS: /root/repo/ci/scripts/

# docker:
# - image: zifrose/unity3d-webgl

# steps:
# # Checkout repo -> ~/repo
# - checkout

# - run:
# name: Enable permissions on scripts
# command: chmod +x $SCRIPTS/**.sh -v

# - run:
# name: Add GPG login to git
# command: $SCRIPTS/git_login.sh

# - run:
# name: Checkout deployment branch (→ ~/upm)
# command: |
# : ${GITHUB_PUBLISH_BRANCH:=upm}
# : ${CIRCLE_REPOSITORY_URL?}

# git clone $CIRCLE_REPOSITORY_URL --single-branch --branch $GITHUB_PUBLISH_BRANCH ~/upm
# code=$?
# ls ~/upm
# exit $code

# - run:
# name: Obtaining Playground UI version
# command: |
# shopt -s globstar

# # Get versions
# PLAYGROUND_CARCONTROLLER_VERSION="$($SCRIPTS/get_attr_version.sh ~/repo/Assets/**/CarControllerAssemblyInfo.cs)"
# echo ">>> Using Playground Kjell UI v$PLAYGROUND_CARCONTROLLER_VERSION"

# # Save to environment
# echo "PLAYGROUND_CARCONTROLLER_VERSION='$PLAYGROUND_CARCONTROLLER_VERSION'" >> $BASH_ENV

# - run:
# name: Prepare files for deployment
# command: |
# : ${PLAYGROUND_CARCONTROLLER_VERSION?}

# echo ">>> Update version package.json to '$PLAYGROUND_CARCONTROLLER_VERSION'"
# echo "$(jq ".version=\"$PLAYGROUND_CARCONTROLLER_VERSION\"" ~/upm/package.json)" > ~/upm/package.json
# echo

# $SCRIPTS/move_upm_files.sh ~/repo ~/upm
# $SCRIPTS/generate_metafiles.sh ~/upm

# - run:
# name: Deploy to UPM branch
# command: $SCRIPTS/deploy_upm.sh ~/repo ~/upm

# - run:
# name: Setting Failure Condition
# command: echo 'export BUILD_STATUS="fail"' >> $BASH_ENV
# when: on_fail
# - run:
# name: Setting Success Condition
# command: echo 'export BUILD_STATUS="success"' >> $BASH_ENV
# when: on_success

# - run:
# name: Send Slack notification
# when: always
# command: $SCRIPTS/slack_notify_github_deploy.sh

workflows:
version: 2.1

work-build-test:
jobs:
- build-test:
filters:
branches:
<<: *WORKFLOW_BUILD_TEST_FILTER

work-build-test-deploy:
jobs:
- build-test:
filters:
branches:
<<: *WORKFLOW_BUILD_TEST_DEPLOY_FILTER

# - deploy-github-webgl:
# requires:
# - build-test

# filters:
# branches:
# <<: *WORKFLOW_BUILD_TEST_DEPLOY_FILTER
87 changes: 87 additions & 0 deletions ci/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Setup CircleCI

## Generate GPG key

```sh
$ gpg --full-generate-key
# follow generate instructions, but no passphrase
# use following settings:
# kind: (1) RSA and RSA (default)
# keysize: 4096
# expiration: /up to you, ex: 1y/
# real name: /your full name, first+last name/
# email: /your github.com email/
# comment: /name of your first tooth/

$ gpg --list-keys --keyid-format LONG
# to find the key id, as referred to as $KEY_ID in below two commands
# paste $KEY_ID into GITHUB_GPG_ID env var in circleci

$ gpg --armor --export $KEY_ID | clip
# paste gpg key at https://github.com/settings/keys

$ gpg --armor --export-secret-keys $KEY_ID | base64 | clip
# paste into GITHUB_GPG_SEC_B64 env var in circleci
```

## Generate SSH key

The SSH key is required for Unity Package Manager to pull the private dependencies.
See [README.md on upm branch](https://github.com/zardan/ui/blob/upm/README.md) for more details.

1. Go to <https://circleci.com/gh/zardan/ui/edit#checkout> and add a user key from an account that also can connect to the other repos from link above.
2. Add this step to the config.yml:

```yml
steps:
- add_ssh_keys:
fingerprints:
- "b7:35:a6:4e:9b:0d:6d:d4:78:1e:9a:97:2a:66:6b:be"
# Change to fingerprint of your new key
```

## Unity license

Gableroux on GitLab has a good tutorial. Follow it here to obtain a `.ulf` license file.
You can follow the instructions on here: <https://gitlab.com/gableroux/unity3d-gitlab-ci-example/tree/master#how-to-activate>

With the `.ulf` downloaded, convert it to base64 and add it to the `UNITY_LICENSE_CONTENT_B64` environment key in the [env settings on CircleCI](https://circleci.com/gh/zardan/ui/edit#env-vars).

```sh
$ base64 $UNITY_ULF_KEY_PATH | clip
# paste into UNITY_LICENSE_CONTENT_B64 env var in circleci
```

## Add Slack webhook for notifications

To use an existing integration, head to
[https://your_domain.slack.com/apps/manage](https://slack.com/apps/manage)

To create a new integration via CircleCI, head to
[https://your_domain.slack.com/apps/new/A0F7VRE7N-circleci](https://slack.com/apps/new/A0F7VRE7N-circleci)

Finally, paste the webhook url into the `SLACK_WEBHOOK` environment variable.

## Add CircleCI API key

For further detailed slack messages the script uses CircleCI's API.

Create a personal API token at:
<https://circleci.com/account/api>

Paste the key into `CIRCLE_API_KEY` in the [env settings on CircleCI](https://circleci.com/gh/zardan/ui/edit#env-vars).

## Summary: All env vars

List of all environment variables, to check if one is missing in CircleCI

| Key | Description |
| --------------------------- | ----------- |
| `GITHUB_GPG_ID` | ID of GPG key. |
| `GITHUB_GPG_SEC_B64` | GPG private key, base64 encoded. |
| `GITHUB_USER_EMAIL` | Deployment github account email, same as used in GPG and SSH key. |
| `GITHUB_USER_NAME` | Deployment github account display name (not username). |
| `GITHUB_USER_ID` | Deployment github account username (same as <https://github.com/your_user_id>) |
| `UNITY_LICENSE_CONTENT_B64` | Unity_lic.ulf in base 64 format |
| `SLACK_WEBHOOK` | Slack webhook url, for use in Slack integration |
| `CIRCLE_API_KEY` | CircleCI personal API token, for use in Slack integration |
51 changes: 51 additions & 0 deletions ci/build-and-push-docker.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

# Builds and pushes the docker images

# THIS SCRIPT IS MEANT TO BE USED FOR DEVELOPMENT PURPOSES
# DO NOT USE IN PRODUCTION

param(
# Docker image account
[string]
$Account = "zifrose",

# Docker images (same as filenames without .Dockerfile)
[string[]]
$images = @(
"unity3d-webgl",
"unity3d"
)
)

$basePath = Resolve-Path $PSCommandPath | Split-Path -Parent

Write-Host ">>> Building" -BackgroundColor Green
$step = 0
$steps = $images.Count * 2

foreach ($image in $images) {
$step++;
$file = Join-Path $basePath "$image.Dockerfile"
Write-Host "> Building $Account/$image docker image (step $step/$steps)" -BackgroundColor DarkGreen
Write-Host ""
docker build . -t $Account/$image -f $file
if (-not $?) {
throw "Failed to build $Account/$image (step $step/$steps)"
}
Write-Host ""
}

Write-Host ">>> Pushing" -BackgroundColor Blue

foreach ($image in $images) {
$step++;
Write-Host "> Pushing $Account/$image docker image (step $step/$steps)" -BackgroundColor DarkBlue
Write-Host ""
docker push $Account/$image
if (-not $?) {
throw "Failed to push $Account/$image (step $step/$steps)"
}
Write-Host ""
}

Write-Host "<<< Build and push complete" -BackgroundColor Cyan
58 changes: 58 additions & 0 deletions ci/docker-run-testrunner.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

# Opens the current folder with the docker image

# THIS SCRIPT IS MEANT TO BE USED FOR DEVELOPMENT PURPOSES
# DO NOT USE IN PRODUCTION

param(
# Unity license
[ValidateScript({
if(-Not ($_ | Test-Path) ){
throw "File or folder ""$_"" does not exist"
}
if(-Not ($_ | Test-Path -PathType Leaf) ){
throw "The Path argument must be a file. Folder paths are not allowed."
}
if($_ -notmatch "(\.ulf)$"){
throw "The file specified in the path argument must be filetype .ulf"
}
return $true
})]
[System.IO.FileInfo]
$UnityLicenseULF = "C:\ProgramData\Unity\Unity_lic.ulf",

# Docker image
[string]
$DockerImage = "zifrose/unity3d"
)

Write-Output "Using Unity license $UnityLicenseULF"
Write-Output "Using Docker image $DockerImage"
Write-Output ""

$UnityLicenseContent = Get-Content -Path $UnityLicenseULF -Raw
$UnityLicenseBytes = [System.Text.Encoding]::Unicode.GetBytes($UnityLicenseContent)
$UnityLicenseB64 = [Convert]::ToBase64String($UnityLicenseBytes)

$UnityID = Get-Credential -Message "Enter UnityID login"

docker run -it --rm `
-e "UNITY_USERNAME=$($UnityID.UserName)" `
-e "UNITY_PASSWORD=$($UnityID.GetNetworkCredential().Password)" `
-e "UNITY_LICENSE_CONTENT_B64=$UnityLicenseB64" `
-e "TEST_PLATFORM=linux" `
-e "WORKDIR=/root/repo" `
-v "$(Get-Location):/root/repo" `
$DockerImage `
'/bin/bash'

# To generate the .alf file run this:
#
# xvfb-run --auto-servernum --server-args='-screen 0 640x480x24' \
# /opt/Unity/Editor/Unity \
# -logFile \
# -batchmode \
# -username "$UNITY_USERNAME" -password "$UNITY_PASSWORD"

# Then copy the XML and paste into a new file and name if unity3d.alf
# Upload the .alf to https://license.unity3d.com/manual
Loading

0 comments on commit 030d1c5

Please sign in to comment.