...
 
Commits (6)
stages:
- build
- unit-test
- stg-deploy
- stg-test
- prod-deploy
- automation
build-test:
stage: build
tags:
- "docker"
image: chengweisdocker/coscup2017:ci-worker
script:
- export CI_COMMIT=$(echo $CI_BUILD_REF | cut -b 1-6)
- touch $CI_PROJECT_NAME-$CI_BUILD_REF_NAME-$CI_COMMIT
- composer install
- npm install
- npm run dev
- rm -rf node_modules
artifacts:
paths:
- "."
build-release:
stage: build
tags:
- "docker"
image: chengweisdocker/coscup2017:ci-worker
script:
- export CI_COMMIT=$(echo $CI_BUILD_REF | cut -b 1-6)
- export ARTIFACT_NAME="$CI_PROJECT_NAME"-"$CI_BUILD_REF_NAME"-"$CI_COMMIT".tar.gz
- touch $CI_PROJECT_NAME-$CI_BUILD_REF_NAME-$CI_COMMIT
- composer install --no-dev
- npm install
- npm run production
- rm -rf node_modules
- cd ..
- tar -zc $CI_PROJECT_NAME/ -f artifacts.tar.gz
- cp artifacts.tar.gz $CI_PROJECT_NAME/
- mv artifacts.tar.gz /tmp/$ARTIFACT_NAME
- ansible-playbook $CI_PROJECT_NAME/ansible/upload_artifacts.yml -e target_host=localhost -e artifact_name=$ARTIFACT_NAME
artifacts:
paths:
- "artifacts.tar.gz"
phpunit:
stage: unit-test
tags:
- "docker"
image: chengweisdocker/coscup2017:ci-worker
script:
- composer run-script post-root-package-install
- composer run-script post-create-project-cmd
- php vendor/bin/phpunit -c phpunit.xml --coverage-text --colors=never
dependencies:
- build-test
stg-deploy:
stage: stg-deploy
only:
- demo
tags:
- "my-runner"
image: chengweisdocker/coscup2017:ci-worker
script:
- export CI_COMMIT=$(echo $CI_BUILD_REF | cut -b 1-6)
- mv artifacts.tar.gz /tmp/artifacts.tar.gz
- ansible-playbook ansible/auto-deploy.yml -i ansible/inventory/demo -e target_host=stg -e host_ip=$stg_host_ip -e user_pass=$stg_user_pass -e CI_PROJECT_NAME=$CI_PROJECT_NAME
dependencies:
- build-release
environment:
name: staging
url: http://$stg_domain_name
test:
stage: stg-test
only:
- demo
tags:
- "my-runner"
image: chengweisdocker/coscup2017:ci-worker
script:
- curl http://$stg_domain_name
prod-deploy:
stage: prod-deploy
only:
- demo
tags:
- "my-runner"
image: chengweisdocker/coscup2017:ci-worker
script:
- export CI_COMMIT=$(echo $CI_BUILD_REF | cut -b 1-6)
- mv artifacts.tar.gz /tmp/artifacts.tar.gz
- ansible-playbook ansible/auto-deploy.yml -i ansible/inventory/demo -e target_host=prod -e host_ip=$prod_host_ip -e user_pass=$prod_user_pass -e CI_PROJECT_NAME=$CI_PROJECT_NAME
- curl http://$prod_domain_name
dependencies:
- build-release
environment:
name: production
url: http://$prod_domain_name
when: manual
close-project:
stage: automation
script:
- echo "do something like rm -rf .* ?"
when: manual
---
- name: Deploy
hosts: "{{ target_host }}"
gather_facts: no
become: yes
pre_tasks:
- set_fact:
ansible_host: "{{ host_ip }}"
ansible_ssh_pass: "{{ user_pass }}"
tasks:
- name: Create Folder
file:
path: "/deploy/"
state: directory
- name: Upload and unarchive artifacts
unarchive:
src: "/tmp/artifacts.tar.gz"
dest: "/deploy/"
backup: yes
group: root
owner: root
- name: "composer run-script {{ item }}"
composer:
command: run-script
arguments: "{{ item }}"
working_dir: "/deploy/{{ CI_PROJECT_NAME }}"
with_items:
- post-root-package-install
- post-create-project-cmd
- name: Replace nginx site config
template:
src: templates/website.j2
dest: /etc/nginx/website
backup: yes
group: root
owner: root
- name: Restagt php7.0-fpm
service:
name: "{{ item }}"
state: restarted
with_items:
- php7.0-fpm
- nginx
[container:vars]
ansible_port=22
ansible_user=root
[container]
stg
prod
\ No newline at end of file
root /deploy/{{ CI_PROJECT_NAME }}/public;
\ No newline at end of file
---
- name: Upload Artifacts
hosts: "{{ target_host }}"
gather_facts: no
become: yes
tasks:
- name: Create Folder
file:
path: /artifacts/
state: directory
- name: Copy Artifact
copy:
src: "/tmp/{{ artifact_name }}"
dest: "/artifacts/{{ artifact_name }}"
backup: yes
group: root
owner: root
## Ansible & GitLab CI/CD workshop 101
[![coverage report](https://gitlab.com/DevOpsTaiwan/coscup2017-workshop-101/badges/demo/coverage.svg)](https://gitlab.com/DevOpsTaiwan/coscup2017-workshop-101/commits/demo)
此範例程式碼即是 [PHP Framework Laravel 5.4.*](https://laravel.com/docs/5.4#installation),如果你熟悉 Laravel,你也可以自行預備。
取得程式碼後,即可依據 Workshop 101 的步驟開始嘗試建立你自己的 CI/CD Pipeline。
* [操作步驟](http://blog.chengweichen.com/2017/08/coscup-2017-ansible-gitlab-cicd.html)
## License
The Laravel framework is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).