|
1 | | -name: Build and Push Image, Update Helm Chart |
| 1 | +name: Build and Push Staking-api Image |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
5 | 5 | branches: |
6 | 6 | - 'staging' |
7 | 7 | - 'main' |
8 | 8 |
|
9 | | -env: |
10 | | - APP_NAME: staking-api |
11 | | - K8S_NS: staking |
12 | | - CLUSTER_NAME: ${{ vars.CLUSTER_NAME }} |
13 | | - |
14 | 9 | jobs: |
15 | | - build-and-update: |
16 | | - name: Build and Push Image, Update Helm Chart |
| 10 | + define-params: |
17 | 11 | runs-on: ubuntu-latest |
18 | | - |
19 | | - environment: ${{ (github.ref_name == 'main') && 'production' || 'staging' }} |
20 | | - |
| 12 | + outputs: |
| 13 | + app_name: staking-api |
| 14 | + k8s_ns: staking |
| 15 | + image_tag: ${{ github.sha }} |
| 16 | + target_environment: ${{ (github.ref_name == 'main') && 'production' || 'staging' }} |
21 | 17 | steps: |
22 | | - - name: Checkout Repository |
23 | | - uses: actions/checkout@v4 |
24 | | - |
25 | | - - name: Authenticate with GCP |
26 | | - uses: google-github-actions/auth@v2 |
27 | | - with: |
28 | | - credentials_json: ${{ secrets.GCP_SERVICE_ACCOUNT_KEY }} |
29 | | - |
30 | | - - name: Configure Docker for GCP |
31 | | - run: gcloud auth configure-docker |
32 | | - |
33 | | - - name: Set up Docker Buildx |
34 | | - uses: docker/setup-buildx-action@v3 |
35 | | - |
36 | | - - name: Build and Push Docker Image |
37 | | - run: | |
38 | | - IMAGE_NAME=gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ env.APP_NAME }}:${{ github.sha }} |
39 | | - docker build \ |
40 | | - --push \ |
41 | | - --tag $IMAGE_NAME \ |
42 | | - -f ./Dockerfile \ |
43 | | - . |
44 | | - env: |
45 | | - IMAGE_NAME: gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ env.APP_NAME }}:${{ github.sha }} |
46 | | - |
47 | | - - name: Checkout Target Repository |
48 | | - uses: actions/checkout@v4 |
49 | | - with: |
50 | | - repository: storyprotocol/story-helm |
51 | | - ref: main |
52 | | - token: ${{ secrets.CICD_GIT_TOKEN }} |
53 | | - |
54 | | - - name: Update Image Tag |
55 | | - uses: mikefarah/yq@master |
56 | | - with: |
57 | | - cmd: | |
58 | | - yq -i '.image.tag = "${{ github.sha }}"' '${{ env.K8S_NS }}/${{ env.APP_NAME }}/${{ env.CLUSTER_NAME }}.yaml' |
59 | | -
|
60 | | - - name: Push Changed Image Tag |
61 | | - run: | |
62 | | - git config --global user.name "${{ github.actor }}" |
63 | | - git config --global user.email "${{ github.actor }}@users.noreply.github.com" |
64 | | - git checkout main |
65 | | - |
66 | | - # If there's nothing to commit, stop here |
67 | | - if [ -z "$(git status --porcelain)" ]; then |
68 | | - echo "Nothing to commit, working directory clean" |
69 | | - exit 0 |
70 | | - fi |
71 | | - |
72 | | - # We'll try up to 3 attempts |
73 | | - for i in 1 2 3 |
74 | | - do |
75 | | - echo "Attempt #$i to commit and push changes..." |
76 | | - # Commit if there's something to commit |
77 | | - git commit -am "${{ env.APP_NAME }}: Update image tag to ${{ github.sha }}" |
78 | | - |
79 | | - if git push |
80 | | - then |
81 | | - echo "Push succeeded on attempt #$i!" |
82 | | - break |
83 | | - else |
84 | | - echo "Push failed (attempt #$i). Trying to pull and rebase with origin/main." |
85 | | - git fetch origin main |
86 | | - git pull --rebase origin main || (echo "Rebase failed!" && exit 1) |
87 | | - fi |
88 | | - |
89 | | - # If this was the last attempt, fail the job |
90 | | - if [ "$i" -eq "3" ] |
91 | | - then |
92 | | - echo "Exceeded max number of push attempts." |
93 | | - exit 1 |
94 | | - fi |
95 | | - done |
| 18 | + - run: echo "Exposing target environment" |
| 19 | + |
| 20 | + image-build: |
| 21 | + needs: define-params |
| 22 | + uses: storyprotocol/gha-workflows/.github/workflows/reusable-gcp-image-build-worker.yml@main |
| 23 | + with: |
| 24 | + app_name: ${{ needs.define-params.outputs.app_name }} |
| 25 | + k8s_ns: ${{ needs.define-params.outputs.k8s_ns }} |
| 26 | + target_environment: ${{ needs.define-params.outputs.target_environment }} |
| 27 | + image_tag: ${{ needs.define-params.outputs.image_tag }} |
| 28 | + dockerfile_path: "./Dockerfile" |
| 29 | + dockerfile_context: "." |
| 30 | + secrets: inherit |
| 31 | + |
| 32 | + request-app-release: |
| 33 | + needs: [ define-params, image-build ] |
| 34 | + uses: storyprotocol/gha-workflows/.github/workflows/reusable-gcp-app-release-publisher.yml@main |
| 35 | + with: |
| 36 | + app_name: ${{ needs.define-params.outputs.app_name }} |
| 37 | + k8s_ns: ${{ needs.define-params.outputs.k8s_ns }} |
| 38 | + target_environment: ${{ needs.define-params.outputs.target_environment }} |
| 39 | + image_tag: ${{ needs.define-params.outputs.image_tag }} |
| 40 | + secrets: inherit |
0 commit comments