Last active
November 5, 2016 07:08
-
-
Save theqwan-chengwei/1d37e817a95f6d69c685d455137363e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 請在所有的 apt-get install 後面補上 -y | |
sudo apt-get install software-properties-common | |
if ! grep -q "ansible/ansible" /etc/apt/sources.list /etc/apt/sources.list.d/*; then | |
sudo apt-add-repository -y ppa:ansible/ansible | |
fi | |
sudo apt-get update | |
sudo apt-get install ansible | |
sudo apt-get install python-pip python-dev | |
sudo pip install httplib2 | |
sudo pip install passlib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: Create GitLab Repository | |
hosts: localhost | |
connection: local | |
#輸入正確的 `gitlab_url`、`gitlab_token`、`project_name` | |
vars: | |
gitlab_url: "your_gitlab_url_and_port" | |
gitlab_token: "your_gitlab_token" | |
project_name: "your_project_name" | |
project_team: "root" | |
tasks: | |
- name: check repository exists | |
uri: | |
url: "{{ gitlab_url }}/api/v3/projects/all?search={{ project_name }}" | |
method: GET | |
HEADER_PRIVATE-TOKEN: "{{ gitlab_token }}" | |
status_code: 200 | |
timeout: 90 | |
register: check_gitlab_repo_exists | |
tags: create_repo | |
- fail: | |
msg: "repository {{ project_name }} exists" | |
when: (check_gitlab_repo_exists.x_total >= '1') and (check_gitlab_repo_exists.status == 200) | |
- name: check repository namespace | |
uri: | |
url: "{{ gitlab_url }}/api/v3/namespaces?search={{ project_team }}" | |
method: GET | |
HEADER_PRIVATE-TOKEN: "{{ gitlab_token }}" | |
status_code: 200 | |
timeout: 90 | |
register: check_gitlab_namespace_id | |
tags: create_repo | |
- name: get repository namespace | |
set_fact: "gitlab_namespace_id={{ item.id }}" | |
when: item.path == "{{ project_team }}" | |
with_items: "{{ check_gitlab_namespace_id.json }}" | |
tags: create_repo | |
- name: create gitlab repo | |
uri: | |
url: "{{ gitlab_url }}/api/v3/projects" | |
method: POST | |
HEADER_Content-Type: "application/json" | |
HEADER_PRIVATE-TOKEN: "{{ gitlab_token }}" | |
body_format: json | |
body: '{"name": "{{ project_name }}", "namespace_id": "{{ gitlab_namespace_id }}", "shared_runners_enabled": "true"}' | |
status_code: 201 | |
timeout: 90 | |
register: create_gitlab_repo | |
when: (gitlab_namespace_id is defined) and (check_gitlab_repo_exists.x_total == '0') and (check_gitlab_repo_exists.status == 200) | |
tags: create_repo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pipeline 中可以有多個 stages | |
stages: | |
- build | |
# 每個 stages 可以有多個 Job | |
# Job Name = ComposerAndArchive | |
ComposerAndArchive: | |
# 在哪個 docker image 上執行自動化腳本 | |
# Azure 環境請設定為 image: chengweisdocker/phpconf2016-composer:php7 | |
# Local VM 請設定為 image: ci/composer:php7 | |
image: ci/composer:php7 | |
# 此 Job 屬於哪個 stage | |
stage: build | |
# 自動化腳本 | |
script: | |
# check env | |
- env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 自動化腳本 | |
script: | |
# check env | |
- env | |
# 先確認一下目前資料夾中有哪些檔案 | |
- ls | |
# 執行 composer install | |
- composer install | |
# 在確認是不是真的有順利產生 vendor | |
- ls vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 自動化腳本 | |
script: | |
# check env | |
- env | |
# 先確認一下目前資料夾中有哪些檔案 | |
- ls | |
# 執行 composer install | |
- composer install | |
# 在確認是不是真的有順利產生 vendor | |
- ls vendor | |
# 為了取得只有前六碼的 CI_BUILD_REF | |
- export CI_COMMIT=$(echo $CI_BUILD_REF | cut -b 1-6) | |
# 驗證一下 | |
- echo "$CI_COMMIT" | |
# Archive files | |
- cd .. | |
- tar -zc "$CI_PROJECT_NAME"/ -f "$CI_PROJECT_NAME"-"$CI_BUILD_REF_NAME"-"$CI_COMMIT".tar.gz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 自動化腳本 | |
script: | |
# check env | |
- env | |
# 先確認一下目前資料夾中有哪些檔案 | |
- ls | |
# 執行 composer install | |
- composer install | |
# 在確認是不是真的有順利產生 vendor | |
- ls vendor | |
# 為了取得只有前六碼的 CI_BUILD_REF | |
- export CI_COMMIT=$(echo $CI_BUILD_REF | cut -b 1-6) | |
# 驗證一下 | |
- echo "$CI_COMMIT" | |
# Archive files | |
- cd .. | |
- tar -zc "$CI_PROJECT_NAME"/ -f "$CI_PROJECT_NAME"-"$CI_BUILD_REF_NAME"-"$CI_COMMIT".tar.gz | |
# 將 Archive 搬回 code 路徑內 | |
- mv "$CI_PROJECT_NAME"-"$CI_BUILD_REF_NAME"-"$CI_COMMIT".tar.gz "$CI_PROJECT_NAME"/ | |
artifacts: | |
paths: | |
- $CI_PROJECT_NAME-$CI_BUILD_REF_NAME-$(echo $CI_BUILD_REF | cut -b 1-6).tar.gz | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# dev_server_ip 如果是 Azure 請輸入真實的 dev server IP | |
# Local VM 則輸入 172.17.0.7,因為我們的 dev_server container ip 即是 172.17.0.7 | |
variables: | |
dev_server_ip: "xxx.xxx.xxx.xxx" | |
CODE_PATH: "/builds/root/" | |
stages: | |
- build | |
- deploy-dev | |
# --- 中略 --- 可不要直接全部複製貼上此檔,這裡只會記錄要新增的內容。 | |
# 自動化部署 (ssh) | |
deploy: | |
# Azure 環境請設定為 image: chengweisdocker/phpconf2016-ansible:dev | |
# Local VM 請設定為 image: ci/ansible:dev | |
image: ci/ansible:dev | |
stage: deploy-dev | |
script: | |
- export CI_COMMIT=$(echo $CI_BUILD_REF | cut -b 1-6) | |
- export TAR_FILENAME="$CI_PROJECT_NAME"-"$CI_BUILD_REF_NAME"-"$CI_COMMIT".tar.gz | |
# Azure 環境請將 ansible_port=22 改成 2222 | |
- echo "dev ansible_host=$dev_server_ip ansible_port=22" > /etc/ansible/hosts | |
- cat /etc/ansible/hosts | |
- echo "[defaults]" > /etc/ansible/ansible.cfg ; echo "host_key_checking = False" >> /etc/ansible/ansible.cfg | |
- cat /etc/ansible/ansible.cfg | |
- ansible-playbook ansible-deploy.yml -e MYHOST=dev -e TAR_FILENAME=$TAR_FILENAME -e CODE_PATH=$CODE_PATH -e CI_PROJECT_NAME=$CI_PROJECT_NAME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: deploy | |
hosts: "{{ MYHOST }}" | |
tasks: | |
- name: check artifact name | |
debug: msg={{ TAR_FILENAME }} | |
- name: "upload artifact to {{ MYHOST }} server" | |
copy: | |
src: "{{ CODE_PATH }}{{ CI_PROJECT_NAME }}/{{ TAR_FILENAME }}" | |
dest: "{{ CODE_PATH }}" | |
- command: ls -al {{ CODE_PATH }} | |
register: check_artifact_upload | |
- name: check artifact upload | |
debug: | |
msg: "{{ check_artifact_upload.stdout_lines }}" | |
- name: remove app | |
file: | |
path: "{{ CODE_PATH }}{{ CI_PROJECT_NAME }}" | |
state: absent | |
- name: unarchive artifact | |
shell: "/bin/tar -zxf {{ CODE_PATH }}{{ TAR_FILENAME }} -C {{ CODE_PATH }}" | |
- name: setup .env | |
shell: "cp {{ CODE_PATH }}{{ CI_PROJECT_NAME }}/.env_test {{ CODE_PATH }}{{ CI_PROJECT_NAME }}/.env" | |
- name: composer dump-autoload | |
shell: "/usr/local/bin/composer dump-autoload -d {{ CODE_PATH }}{{ CI_PROJECT_NAME }}/" | |
- name: update nginx site config root path | |
shell: echo "root {{ CODE_PATH }}{{ CI_PROJECT_NAME }}/public/;" > /etc/nginx/default_root.conf | |
- name: reload nginx | |
service: | |
name: nginx | |
state: reloaded |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
APP_ENV=local | |
APP_KEY=base64:A/KuNhfKQ8koLcrj5eFxzA1E5I3TGxFx4LmpasxKKZs= | |
APP_DEBUG=true | |
APP_LOG_LEVEL=debug | |
APP_URL=http://localhost | |
DB_CONNECTION=sqlite | |
DB_HOST=127.0.0.1 | |
DB_PORT=3306 | |
#DB_DATABASE=homestead | |
DB_USERNAME=homestead | |
DB_PASSWORD=secret | |
BROADCAST_DRIVER=log | |
CACHE_DRIVER=file | |
SESSION_DRIVER=file | |
QUEUE_DRIVER=sync | |
REDIS_HOST=127.0.0.1 | |
REDIS_PASSWORD=null | |
REDIS_PORT=6379 | |
MAIL_DRIVER=smtp | |
MAIL_HOST=mailtrap.io | |
MAIL_PORT=2525 | |
MAIL_USERNAME=null | |
MAIL_PASSWORD=null | |
MAIL_ENCRYPTION=null | |
PUSHER_APP_ID= | |
PUSHER_KEY= | |
PUSHER_SECRET= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#3-3 增加以下這段 | |
- name: check deploy | |
shell: "curl http://127.0.0.1 | grep forge.laravel.com" | |
register: check_deploy | |
- name: echo msg | |
debug: msg="{{ check_deploy.stdout_lines }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#3-4 修改最後一行 | |
- ansible-playbook /ansible/ansible-deploy.yml -e MYHOST=dev -e TAR_FILENAME=$TAR_FILENAME -e CODE_PATH=$CODE_PATH -e CI_PROJECT_NAME=$CI_PROJECT_NAME -e KEYWORD="Laravel" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pipeline 中可以有多個 stages | |
stages: | |
- build | |
- deploy-dev | |
- test-dev | |
# -- 一樣中略 -- | |
# 4-1 增加 test | |
test: | |
# Azure 環境請設定為 image: chengweisdocker/phpconf2016-ansible:dev | |
# Local VM 請設定為 image: ci/ansible:dev | |
image: ci/ansible:dev | |
stage: test-dev | |
script: | |
- echo "StrictHostKeyChecking no" > ssh.config | |
# Azure 環境,請多補上 -p 2222 如下面註解這一行,因為 dev_server 的 port 是 2222。 | |
# - ssh root@$dev_server_ip -F ssh.config -p 2222 php $CODE_PATH$CI_PROJECT_NAME/vendor/bin/phpunit -c $CODE_PATH$CI_PROJECT_NAME/phpunit.xml | |
- ssh root@$dev_server_ip -F ssh.config php $CODE_PATH$CI_PROJECT_NAME/vendor/bin/phpunit -c $CODE_PATH$CI_PROJECT_NAME/phpunit.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 修改此行 --> | |
<env name="BROWSER" value="firefox"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 修改以下幾行 --> | |
<env name="WEBSERVER_URL" value="172.17.0.7"/> | |
<env name="WEBSERVER_PORT" value="80"/> | |
<env name="SELENIUM_URL" value="172.17.0.8"/> | |
<!-- 也可順便修改此行 --> | |
<env name="AJAX_DELAY" value="5"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Azure 環境,記得補上 -p 2222。 | |
#- ssh root@$dev_server_ip -F ssh.config -p 2222 php $CODE_PATH$CI_PROJECT_NAME/vendor/bin/phpunit -c $CODE_PATH$CI_PROJECT_NAME/phpunit.xml --coverage-text | |
- ssh root@$dev_server_ip -F ssh.config php $CODE_PATH$CI_PROJECT_NAME/vendor/bin/phpunit -c $CODE_PATH$CI_PROJECT_NAME/phpunit.xml --coverage-text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Azure 環境,記得補上 -p 2222。 | |
#- ssh root@$dev_server_ip -F ssh.config -p 2222 php $CODE_PATH$CI_PROJECT_NAME/vendor/bin/phpunit -c $CODE_PATH$CI_PROJECT_NAME/phpunit.xml --coverage-text --colors=never | |
- ssh root@$dev_server_ip -F ssh.config php $CODE_PATH$CI_PROJECT_NAME/vendor/bin/phpunit -c $CODE_PATH$CI_PROJECT_NAME/phpunit.xml --coverage-text --colors=never |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Azure 環境的學員,請依據 Azure 後台顯示的 ip 資訊來設定正確的 URL | |
Local VM 則改為 http://localhost:10080 | |
![coverage report](http://localhost:10080/root/demo/badges/master/coverage.svg) | |
![build status](http://localhost:10080/root/demo/badges/master/build.svg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stages: | |
- build | |
- deploy-dev | |
- test-dev | |
- dev-to-master | |
# ...... 中略 ...... | |
deploy: | |
# Azure 環境請設定為 image: chengweisdocker/phpconf2016-ansible:dev | |
# Local VM 請設定為 image: ci/ansible:dev | |
image: ci/ansible:dev | |
stage: deploy-dev | |
only: | |
- dev | |
# ...... 中略 ...... | |
test: | |
# Azure 環境請設定為 image: chengweisdocker/phpconf2016-ansible:dev | |
# Local VM 請設定為 image: ci/ansible:dev | |
image: ci/ansible:dev | |
stage: test-dev | |
only: | |
- dev | |
# ..... 中略 ...... | |
merge-code: | |
image: ci/ansible:dev | |
stage: dev-to-master | |
only: | |
- dev | |
script: | |
- git checkout master | |
- git merge origin/dev | |
# Azure 環境請設定 xxx.xxx.xxx.xxx 為正確的 GitLab IP | |
# Local VM 請設定為 172.17.0.4 | |
- git remote add temp-push http://root:phpconf2016@xxx.xxx.xxx.xxx/root/demo.git | |
- git push temp-push master |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# your_token 請改成正確的 token | |
# Azure 環境的學員,請依據 Azure 後台顯示的 ip 資訊來設定正確的 xxx.xxx.xxx.xxx | |
# Local VM 則改為 172.17.0.4 | |
# your_project_id 請改成正確的 project id,基本上在 | |
curl -X POST \ | |
-F token=your_token \ | |
-F ref=master \ | |
http://xxx.xxx.xxx.xxx/api/v3/projects/your_project_id/trigger/builds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment