From b400fd64829fd54680898a1d5a44a8f170ee2c25 Mon Sep 17 00:00:00 2001 From: Su Shi <1684739+metacpp@users.noreply.github.com> Date: Mon, 1 Jul 2019 13:10:24 -0700 Subject: [PATCH] Add Makefile and refactored Azure pipeline (#58) * add makefile for local running. * update pipeline * Update pipeline. * add newline for .gitignore. * Fix the building issue. * Fix the makefile issue. * Fix the makefile issue. * Fix makefile issue. * Fix the building issue. * fix the pipeline issue. * Enable vendor mode. * Fix the goimport issue. * Fix the goimport issue. --- .gitignore | 4 +++ .gitmodules | 4 --- Makefile | 57 +++++++++++++++++++++++++++++++++++++++ azure-pipelines.yml | 65 +++++++++++++++++++++++++-------------------- generated/ansible | 1 - 5 files changed, 97 insertions(+), 34 deletions(-) create mode 100644 Makefile delete mode 160000 generated/ansible diff --git a/.gitignore b/.gitignore index 5e1422c..50b1e56 100644 --- a/.gitignore +++ b/.gitignore @@ -48,3 +48,7 @@ build-iPhoneSimulator/ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: .rvmrc + + +# Code generation folder by Magic Modules +generated/ diff --git a/.gitmodules b/.gitmodules index d638572..d22a7ac 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,7 +1,3 @@ -[submodule "generated/ansible"] - path = generated/ansible - url = https://github.com/VSChina/ansible.git - branch = devel [submodule "tools/magic-modules"] path = tools/magic-modules url = https://github.com/VSChina/magic-modules.git diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fdb5e4d --- /dev/null +++ b/Makefile @@ -0,0 +1,57 @@ +ROOT=$(shell pwd) +GENROOT=$(ROOT)/generated +GOSRC=$(GENROOT)/go/src +TFROOT=$(GOSRC)/github.com/terraform-providers +TFGITURL=https://github.com/VSChina/terraform-provider-azurerm.git +TFREPO=terraform-provider-azurerm +ASGITURL=https://github.com/VSChina/ansible.git +ASREPO=ansible +MMROOT=$(ROOT)/tools/magic-modules + +# Environment variables. +export GOPATH=$(GENROOT)/go +export GO111MODULE=on +export GOFLAGS=-mod=vendor + +default: init + +all: clean init build format-terraform + +init: + @echo "==> cloning repos of terraform and ansible..." + if [ ! -d "$(TFROOT)" ]; then \ + mkdir -p "$(TFROOT)"; \ + fi + + if [ ! -d "$(TFROOT)/$(TFREPO)" ]; then \ + git clone $(TFGITURL) $(TFROOT)/$(TFREPO); \ + fi + + if [ ! -d "$(GENROOT)/$(ASREPO)" ]; then \ + git clone $(ASGITURL) $(GENROOT)/$(ASREPO); \ + fi + + git submodule update --init && \ + cd $(MMROOT) && \ + gem install bundler && \ + bundle install --retry=3 --jobs=4 + +clean: + rm -rf $(GENROOT) + +build: build-terraform build-ansible + +build-terraform: + cd $(MMROOT) && \ + jq '.[]' $(ROOT)/resources.json | xargs -I '{}' bundle exec compiler -d -p $(ROOT)/'{}' -e terraform -o $(TFROOT)/$(TFREPO)/ + +format-terraform: + cd $(TFROOT)/$(TFREPO) && \ + goimports -w azurerm && \ + make fmt + +build-ansible: + cd $(MMROOT) && \ + jq '.[]' $(ROOT)/resources.json | xargs -I '{}' bundle exec compiler -d -p $(ROOT)/'{}' -e ansible -o $(GENROOT)/$(ASREPO)/ + +.PHONY: init clean build build-terraform format-terraform build-ansible \ No newline at end of file diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 8930b27..25ce52c 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -7,29 +7,27 @@ trigger: - master variables: - # GOROOT: '$HOME/.goenv/versions/1.12.6' # Go installation path - GOPATH: '$(system.defaultWorkingDirectory)/$(Build.BuildNumber)/go-work' # Go workspace path + GENROOT: $(system.defaultWorkingDirectory)/generated + GOPATH: '$(GENROOT)/go' # Go workspace path GOBIN: '$(GOPATH)/bin' # Go binaries path - TFROOT: '$(GOPATH)/src/github.com/terraform-providers' + GOSRC: '$(GOPATH)/src' # Go source path + TFORG: '$(GOPATH)/src/github.com/terraform-providers' + TFREPO: 'terraform-provider-azurerm' + TFROOT: '$(TFORG)/$(TFREPO)' TFGITURL: 'https://github.com/VSChina/terraform-provider-azurerm.git' - TFPROVIDERNAME: 'terraform-provider-azurerm' + ASREPO: 'ansible' + ASROOT: '$(GENROOT)/$(ASREPO)' + ASGITURL: 'https://github.com/VSChina/ansible.git' + MMROOT: $(system.defaultWorkingDirectory)/tools/magic-modules steps: -# - task: UseRubyVersion@0 -# inputs: -# versionSpec: '>= 2.5' - -# - task: UsePythonVersion@0 -# inputs: -# versionSpec: '>=3.6' - - bash: | shopt -s extglob echo `go env` echo '##vso[task.prependpath]$(GOBIN)' - if [ ! -d "$(TFROOT)" ] + if [ ! -d "$(TFORG)" ] then - mkdir -p "$(TFROOT)" + mkdir -p "$(TFORG)" fi displayName: 'init Go environment' failOnStderr: true @@ -43,26 +41,31 @@ steps: failOnStderr: false - bash: | - git clone $(TFGITURL) $(TFPROVIDERNAME) - cd $(TFPROVIDERNAME) + git clone $(TFGITURL) $(TFROOT) + cd $(TFROOT) git checkout autogen-pr-$(system.pullRequest.pullRequestNumber) || git checkout -b autogen-pr-$(system.pullRequest.pullRequestNumber) if [[ $(git branch -a --list 'origin/autogen-pr-$(system.pullRequest.pullRequestNumber)') ]]; then git pull fi - workingDirectory: $(TFROOT) displayName: 'clone terraform repository' failOnStderr: false - bash: | - git submodule update --init - - pushd generated/ansible + git clone $(ASGITURL) $(ASROOT) + cd $(ASROOT) git checkout autogen-pr-$(system.pullRequest.pullRequestNumber) || git checkout -b autogen-pr-$(system.pullRequest.pullRequestNumber) if [[ $(git branch -a --list 'origin/autogen-pr-$(system.pullRequest.pullRequestNumber)') ]]; then git pull fi - popd - displayName: 'init submodules' + displayName: 'clone ansible repository' + failOnStderr: false + +- bash: | + git submodule update --init && \ + cd $(MMROOT) && \ + gem install bundler && \ + bundle install --retry=3 --jobs=4 + displayName: 'init magic modules' failOnStderr: false - bash: | @@ -76,18 +79,22 @@ steps: failOnStderr: true - bash: | - cd tools/magic-modules - gem install bundler - bundle install --retry=3 --jobs=4 - jq '.[]' ../../resources.json | xargs -I '{}' bundle exec compiler -d -p ../../'{}' -e terraform -o $(TFROOT)/$(TFPROVIDERNAME)/ - jq '.[]' ../../resources.json | xargs -I '{}' bundle exec compiler -d -p ../../'{}' -e ansible -o ../../generated/ansible/ + make build && \ + make format-terraform displayName: 'code generation' failOnStderr: false - bash: | - cd $(TFROOT)/$(TFPROVIDERNAME) + cd $(TFROOT) goimports -w azurerm make fmt + env: + GOPATH: $(GOPATH) + displayName: 'go import and format' + failOnStderr: false + +- bash: | + cd $(TFROOT) git add -A git commit -m "Code generation for Terraform." git push @@ -107,7 +114,7 @@ steps: - bash: | - cd generated/ansible + cd $(ASROOT) # run `black` against the directory of ansible if neccessary. git add -A git commit -m "Code generation for Ansible." diff --git a/generated/ansible b/generated/ansible deleted file mode 160000 index a688d2e..0000000 --- a/generated/ansible +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a688d2ee39860cdb22e5ecd4ca927f22744f7f9e