kind: pipeline
name: default

steps:
- name: prepare
  image: v7lin/flutter:1.17.3-stable
  volumes:
  - name: pub-cache
    path: /opt/flutter/.pub-cache
  commands:
  - flutter packages get

#- name: build_runner
#  image: v7lin/flutter:1.17.3-stable
#  volumes:
#  - name: pub-cache
#    path: /opt/flutter/.pub-cache
#  commands:
#  - flutter pub run build_runner clean
#  - flutter pub run build_runner build --delete-conflicting-outputs

# docker run --rm -it -v ${PWD}:/src v7lin/clang:5.0.2-r0 sh -c "clang-format -style=file -i src/Classes/*.h src/Classes/*.m"
#- name: ios-format
#  image: v7lin/clang
#  commands:
#  - cd ios/
#  - clang-format -style=file -i src/Classes/*.h src/Classes/*.m

- name: format
  image: v7lin/flutter:1.17.3-stable
  volumes:
  - name: pub-cache
    path: /opt/flutter/.pub-cache
  commands:
  - flutter format --dry-run --set-exit-if-changed .

- name: analyze
  image: v7lin/flutter:1.17.3-stable
  volumes:
  - name: pub-cache
    path: /opt/flutter/.pub-cache
  commands:
  - flutter analyze

- name: test
  image: v7lin/flutter:1.17.3-stable
  volumes:
  - name: pub-cache
    path: /opt/flutter/.pub-cache
  commands:
  - flutter test --coverage
#  - cd example/
#  - flutter test

- name: proguard
  image: v7lin/flutter:1.17.3-stable
  volumes:
  - name: pub-cache
    path: /opt/flutter/.pub-cache
  - name: gradle
    path: /root/.gradle
  commands:
  - cd example/
  - flutter build apk

- name: coverage
  image: plugins/codecov:2.0.3
  settings:
    token:
      from_secret: CODECOV_TOKEN
    files:
    - ./coverage/lcov.info
  when:
    event:
      exclude:
      - pull_request

- name: publish-check
  image: v7lin/flutter:1.17.3-stable
  volumes:
  - name: pub-cache
    path: /opt/flutter/.pub-cache
  commands:
  - flutter packages pub publish --dry-run

volumes:
- name: pub-cache
  temp: {}
- name: gradle
  temp: {}

---
kind: pipeline
name: publish

steps:
- name: restore-cache
  image: alpine:3.9.3
  volumes:
  - name: pub-cache
    path: /opt/flutter/.pub-cache
  commands:
  - FLUTTER_HOME=/opt/flutter/.pub-cache
  - wget -P $FLUTTER_HOME https://raw.githubusercontent.com/v7lin/pub_credentials/master/credentials.json.enc

- name: restore-cache-openssl
  image: v7lin/openssl:1.1.1b
  volumes:
  - name: pub-cache
    path: /opt/flutter/.pub-cache
  environment:
    ENC_METHOD:
      from_secret: ENC_METHOD
    ENC_PASSWORD:
      from_secret: ENC_PASSWORD
  commands:
  - FLUTTER_HOME=/opt/flutter/.pub-cache
  - openssl enc -d -$ENC_METHOD -k $ENC_PASSWORD -in $FLUTTER_HOME/credentials.json.enc -out $FLUTTER_HOME/credentials.json
  - rm $FLUTTER_HOME/credentials.json.enc

- name: publish
  image: v7lin/flutter:1.17.3-stable
  volumes:
  - name: pub-cache
    path: /opt/flutter/.pub-cache
  commands:
  - echo "y" | flutter packages pub publish

- name: save-cache-openssl
  image: v7lin/openssl:1.1.1b
  volumes:
  - name: pub-cache
    path: /opt/flutter/.pub-cache
  environment:
    ENC_METHOD:
      from_secret: ENC_METHOD
    ENC_PASSWORD:
      from_secret: ENC_PASSWORD
  commands:
  - FLUTTER_HOME=/opt/flutter/.pub-cache
  - openssl enc -e -$ENC_METHOD -k $ENC_PASSWORD -in $FLUTTER_HOME/credentials.json -out $FLUTTER_HOME/credentials.json.enc
  - rm /opt/flutter/.pub-cache/credentials.json

- name: save-cache
  image: docker:git
  volumes:
  - name: pub-cache
    path: /opt/flutter/.pub-cache
  environment:
    GIT_USER_EMAIL:
      from_secret: GIT_USER_EMAIL
    GIT_USER_NAME:
      from_secret: GIT_USER_NAME
    GIT_USER_PASSWORD:
      from_secret: GIT_USER_PASSWORD # 密码含'@'，用'%40'替换 -> URLEncoder.encode("@","utf-8");
  commands:
  - FLUTTER_HOME=/opt/flutter/.pub-cache
  - git config --global user.email $GIT_USER_EMAIL
  - git config --global user.name $GIT_USER_NAME
  - git config --global credential.helper store
  - git clone -b master https://$GIT_USER_NAME:$GIT_USER_PASSWORD@github.com/v7lin/pub_credentials.git $FLUTTER_HOME/pub_credentials
  - rm $FLUTTER_HOME/pub_credentials/credentials.json.enc
  - mv $FLUTTER_HOME/credentials.json.enc $FLUTTER_HOME/pub_credentials/credentials.json.enc
  - cd $FLUTTER_HOME/pub_credentials
  - git commit -am "update credentials by ci/cd tools"
  - git push

volumes:
- name: pub-cache
  temp: {}

trigger:
  status:
  - success
  event:
  - tag

depends_on:
- default
