|
| 1 | +name: $(MODULE_NAME)-$(date:yyyyMMdd)-$(rev:.r) |
| 2 | +parameters: |
| 3 | +- name: publish_module |
| 4 | + displayName: 'Publish Module ???' |
| 5 | + type: string |
| 6 | + default: 'false' |
| 7 | + values: |
| 8 | + - true |
| 9 | + - false |
| 10 | +trigger: |
| 11 | + branches: |
| 12 | + include: |
| 13 | + - refs/heads/master |
| 14 | + - refs/heads/main |
| 15 | +resources: |
| 16 | + repositories: |
| 17 | + - repository: self |
| 18 | +variables: |
| 19 | + - name: TERRAFORM_SEC_VERSION |
| 20 | + value: "v1.26.0" |
| 21 | + - name: GITHUB_REPO |
| 22 | + value: "Think-Cube/terraform-azure-cosmosdb-database" |
| 23 | + - group: GITHUB-PAT-TOKEN |
| 24 | + - name: PUBLISH_MODULE |
| 25 | + value: ${{parameters.publish_module}} |
| 26 | + - name: VM_IMAGE |
| 27 | + value: ubuntu-latest |
| 28 | + - name: MODULE_NAME |
| 29 | + value: "terraform-azure-cosmosdb-database" |
| 30 | + - name: MODULE_DESCRIPTION |
| 31 | + value: "Terraform module for azure cosmosdb database" |
| 32 | +pool: |
| 33 | + vmImage: $(VM_IMAGE) |
| 34 | +stages: |
| 35 | + - stage: Validate_Terraform_Module |
| 36 | + displayName: 'Validate Terraform Module' |
| 37 | + jobs: |
| 38 | + - job: Validate_Terraform_Module |
| 39 | + displayName: 'Validate Terraform Module' |
| 40 | + steps: |
| 41 | + - checkout: self |
| 42 | + displayName: 'Checkout Module' |
| 43 | + fetchDepth: 1 |
| 44 | + - task: CmdLine@2 |
| 45 | + displayName: 'Terraform Init' |
| 46 | + inputs: |
| 47 | + script: | |
| 48 | + terraform init |
| 49 | + workingDirectory: '$(System.DefaultWorkingDirectory)' |
| 50 | + - task: CmdLine@2 |
| 51 | + displayName: 'Terraform Validate' |
| 52 | + inputs: |
| 53 | + script: | |
| 54 | + terraform validate |
| 55 | + workingDirectory: '$(System.DefaultWorkingDirectory)' |
| 56 | + - task: tfsec@1 |
| 57 | + displayName: 'Terraform SEC check' |
| 58 | + inputs: |
| 59 | + version: '$(TERRAFORM_SEC_VERSION)' |
| 60 | + dir: '$(System.DefaultWorkingDirectory)' |
| 61 | + - stage: Publish_Terraform_Module |
| 62 | + condition: eq('${{parameters.publish_module}}', 'true') |
| 63 | + displayName: 'Publish Terraform Module' |
| 64 | + jobs: |
| 65 | + - job: Publish_Terraform_Module |
| 66 | + displayName: 'Publish Terraform Module' |
| 67 | + steps: |
| 68 | + - checkout: self |
| 69 | + - task: CopyFiles@2 |
| 70 | + displayName: 'Copy Terraform module files' |
| 71 | + inputs: |
| 72 | + SourceFolder: $(System.DefaultWorkingDirectory) |
| 73 | + Contents: '**.tf' |
| 74 | + TargetFolder: $(System.DefaultWorkingDirectory)/$(MODULE_NAME) |
| 75 | + - task: CmdLine@2 |
| 76 | + displayName: 'Fetch latest version and increment' |
| 77 | + inputs: |
| 78 | + script: | |
| 79 | + # Variables |
| 80 | + module_path="$(System.DefaultWorkingDirectory)/$(MODULE_NAME)" |
| 81 | + github_repo="$(GITHUB_REPO)" |
| 82 | + github_token="$(GITHUB_TOKEN)" |
| 83 | + description="New $(MODULE_DESCRIPTION) release" |
| 84 | +
|
| 85 | + # Remove unnecessary files from the module directory |
| 86 | + find "$module_path" -name ".git" -type d -exec rm -rf {} + |
| 87 | + find "$module_path" -name ".github" -type d -exec rm -rf {} + |
| 88 | +
|
| 89 | + # Fetch the latest tag and increment version |
| 90 | + latest_tag=$(git tag --list "v*" | sort -V | tail -n1) |
| 91 | + new_version="0.0.1" |
| 92 | + if [ -n "$latest_tag" ]; then |
| 93 | + IFS='.' read -r major minor patch <<< "${latest_tag#v}" |
| 94 | + if (( patch < 999 )); then |
| 95 | + patch=$((patch + 1)) |
| 96 | + else |
| 97 | + patch=0 |
| 98 | + if (( minor < 999 )); then |
| 99 | + minor=$((minor + 1)) |
| 100 | + else |
| 101 | + minor=0 |
| 102 | + major=$((major + 1)) |
| 103 | + fi |
| 104 | + fi |
| 105 | + new_version="$major.$minor.$patch" |
| 106 | + fi |
| 107 | +
|
| 108 | + # Create a tarball of the module |
| 109 | + tarball_name="$(MODULE_NAME)-v$new_version.tar.gz" |
| 110 | + tar -czf "$tarball_name" -C "$module_path" . |
| 111 | +
|
| 112 | + # Publish to GitHub Releases using gh CLI |
| 113 | + echo "$github_token" | gh auth login --with-token |
| 114 | + gh release create "v$new_version" "$tarball_name" \ |
| 115 | + --repo "$github_repo" \ |
| 116 | + --title "$(MODULE_NAME) v$new_version" \ |
| 117 | + --notes "$description" |
0 commit comments