|
| 1 | +name: Build and Deploy to EKS |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - dev |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 12 | + cancel-in-progress: true |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: write |
| 16 | + packages: write |
| 17 | + |
| 18 | +jobs: |
| 19 | + bump-version: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + outputs: |
| 22 | + version: ${{ steps.bump.outputs.version }} |
| 23 | + image_tag: ${{ steps.bump.outputs.image_tag }} |
| 24 | + environment: ${{ steps.bump.outputs.environment }} |
| 25 | + target_file: ${{ steps.bump.outputs.target_file }} |
| 26 | + steps: |
| 27 | + - name: Checkout code |
| 28 | + uses: actions/checkout@v4 |
| 29 | + with: |
| 30 | + fetch-depth: 0 |
| 31 | + |
| 32 | + - name: Bump patch version and set outputs |
| 33 | + id: bump |
| 34 | + run: | |
| 35 | + git config user.name "github-actions[bot]" |
| 36 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 37 | +
|
| 38 | + current=$(cat version.txt) |
| 39 | + IFS='.' read -r major minor patch <<< "$current" |
| 40 | + patch=$((patch + 1)) |
| 41 | + next="$major.$minor.$patch" |
| 42 | + echo "Bumping version: $current -> $next" |
| 43 | +
|
| 44 | + echo "$next" > version.txt |
| 45 | + git add version.txt |
| 46 | + git commit -m "ci: bump version to $next [skip ci]" |
| 47 | + git push |
| 48 | +
|
| 49 | + if [ "${{ github.ref_name }}" = "main" ]; then |
| 50 | + echo "environment=prod" >> "$GITHUB_OUTPUT" |
| 51 | + echo "image_tag=prod-$next" >> "$GITHUB_OUTPUT" |
| 52 | + echo "target_file=k8/aws/helm/service-layer/values-prod.yaml" >> "$GITHUB_OUTPUT" |
| 53 | + else |
| 54 | + echo "environment=dev" >> "$GITHUB_OUTPUT" |
| 55 | + echo "image_tag=dev-$next" >> "$GITHUB_OUTPUT" |
| 56 | + echo "target_file=k8/aws/helm/service-layer/values.yaml" >> "$GITHUB_OUTPUT" |
| 57 | + fi |
| 58 | + echo "version=$next" >> "$GITHUB_OUTPUT" |
| 59 | +
|
| 60 | + build-and-push: |
| 61 | + needs: bump-version |
| 62 | + runs-on: ubuntu-latest |
| 63 | + strategy: |
| 64 | + fail-fast: true |
| 65 | + matrix: |
| 66 | + include: |
| 67 | + - service: backend |
| 68 | + context: ./backend |
| 69 | + image: ghcr.io/ondc-official/workbench-backoffice-backend |
| 70 | + - service: frontend |
| 71 | + context: ./frontend |
| 72 | + image: ghcr.io/ondc-official/workbench-backoffice-frontend |
| 73 | + steps: |
| 74 | + - name: Checkout code |
| 75 | + uses: actions/checkout@v4 |
| 76 | + |
| 77 | + - name: Set up Docker Buildx |
| 78 | + uses: docker/setup-buildx-action@v3 |
| 79 | + |
| 80 | + - name: Log in to GitHub Container Registry |
| 81 | + uses: docker/login-action@v3 |
| 82 | + with: |
| 83 | + registry: ghcr.io |
| 84 | + username: ${{ github.actor }} |
| 85 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 86 | + |
| 87 | + - name: Build and push ${{ matrix.service }} image |
| 88 | + uses: docker/build-push-action@v6 |
| 89 | + with: |
| 90 | + context: ${{ matrix.context }} |
| 91 | + push: true |
| 92 | + tags: | |
| 93 | + ${{ matrix.image }}:${{ needs.bump-version.outputs.image_tag }} |
| 94 | + ${{ matrix.image }}:latest |
| 95 | + cache-from: type=registry,ref=${{ matrix.image }}:cache |
| 96 | + cache-to: type=registry,ref=${{ matrix.image }}:cache,mode=max |
| 97 | + |
| 98 | + update-iac: |
| 99 | + needs: [bump-version, build-and-push] |
| 100 | + runs-on: ubuntu-latest |
| 101 | + steps: |
| 102 | + - name: Generate GitHub App token |
| 103 | + id: app-token |
| 104 | + uses: actions/create-github-app-token@v1 |
| 105 | + with: |
| 106 | + app-id: ${{ secrets.APP_ID }} |
| 107 | + private-key: ${{ secrets.APP_PRIVATE_KEY }} |
| 108 | + repositories: automation-iac |
| 109 | + |
| 110 | + - name: Clone automation-iac repo |
| 111 | + uses: actions/checkout@v4 |
| 112 | + with: |
| 113 | + repository: ONDC-Official/automation-iac |
| 114 | + token: ${{ steps.app-token.outputs.token }} |
| 115 | + path: automation-iac |
| 116 | + |
| 117 | + - name: Update backoffice image tags in Helm values |
| 118 | + env: |
| 119 | + TARGET_FILE: ${{ needs.bump-version.outputs.target_file }} |
| 120 | + IMAGE_TAG: ${{ needs.bump-version.outputs.image_tag }} |
| 121 | + run: | |
| 122 | + python3 << 'PYEOF' |
| 123 | + import re, os |
| 124 | +
|
| 125 | + target = f"automation-iac/{os.environ['TARGET_FILE']}" |
| 126 | + image_tag = os.environ['IMAGE_TAG'] |
| 127 | +
|
| 128 | + # Sections in values.yaml whose `tag:` lines should be updated |
| 129 | + tracked_sections = {'backofficeBackend', 'backofficeFrontend'} |
| 130 | +
|
| 131 | + with open(target, 'r') as f: |
| 132 | + lines = f.readlines() |
| 133 | +
|
| 134 | + current_section = None |
| 135 | + result = [] |
| 136 | + for line in lines: |
| 137 | + # Top-level keys have no leading whitespace |
| 138 | + top_level = re.match(r'^([a-zA-Z]\S*):', line) |
| 139 | + if top_level: |
| 140 | + key = top_level.group(1) |
| 141 | + current_section = key if key in tracked_sections else None |
| 142 | + if current_section and re.match(r'^\s+tag:\s', line): |
| 143 | + line = re.sub(r'(tag:\s).*', rf'\g<1>{image_tag}', line) |
| 144 | + result.append(line) |
| 145 | +
|
| 146 | + with open(target, 'w') as f: |
| 147 | + f.writelines(result) |
| 148 | +
|
| 149 | + print(f"Updated {tracked_sections} tags to '{image_tag}' in {target}") |
| 150 | + PYEOF |
| 151 | +
|
| 152 | + - name: Commit and push tag update to IaC repo |
| 153 | + run: | |
| 154 | + cd automation-iac |
| 155 | + git config user.name "github-actions[bot]" |
| 156 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 157 | + git add "${{ needs.bump-version.outputs.target_file }}" |
| 158 | + git diff --cached --quiet && echo "No changes to commit" && exit 0 |
| 159 | + git commit -m "ci: update backoffice image tags to ${{ needs.bump-version.outputs.image_tag }} (${{ needs.bump-version.outputs.environment }})" |
| 160 | + git push |
0 commit comments