Skip to content

Commit 12ff9af

Browse files
committed
ci: use github app to commit changes in workflow
1 parent d1cb6dd commit 12ff9af

File tree

3 files changed

+136
-70
lines changed

3 files changed

+136
-70
lines changed

.github/workflows/build-and-push-production.yml

Lines changed: 70 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,73 @@ on:
77
types:
88
- closed
99

10+
11+
1012
jobs:
11-
merge_and_publish_prod:
13+
metadata:
1214
runs-on: ubuntu-latest
1315
if: github.event.pull_request.merged == true &&
1416
(startsWith(github.event.pull_request.head.ref, 'release/') || startsWith(github.event.pull_request.head.ref, 'hotfix/'))
15-
permissions:
16-
contents: write
17-
packages: write
18-
actions: write
19-
pull-requests: write
17+
outputs:
18+
version: ${{ env.RELEASE_VERSION }}
19+
branch: ${{ env.BRANCH }}
20+
app_token: ${{ steps.app-token.outputs.token }}
21+
app_username: '${{ steps.app-token.outputs.app-slug }}[bot]'
22+
app_email: '${{ steps.get-user-id.outputs.user-id }}+${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.com'
2023
steps:
24+
- name: Get Green Ecolution App Token
25+
uses: actions/create-github-app-token@v2
26+
id: app-token
27+
with:
28+
app-id: ${{ vars.APP_ID }}
29+
private-key: ${{ secrets.APP_PRIVATE_KEY }}
30+
permission-contents: write
31+
permission-pull-request: write
32+
33+
- name: Get Green Ecolution App User ID
34+
id: get-user-id
35+
env:
36+
GH_TOKEN: ${{ steps.app-token.outputs.token }}
37+
run: echo "user-id=$(gh api "/users/${{ steps.app-token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"
38+
2139
- name: Extract version from branch name (for release branches)
2240
if: startsWith(github.event.pull_request.head.ref, 'release/')
2341
run: |
2442
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
2543
VERSION=${BRANCH_NAME#release/}
26-
2744
echo "RELEASE_VERSION=$VERSION" >> $GITHUB_ENV
45+
echo "BRANCH=$BRANCH_NAME" >> $GITHUB_ENV
2846
2947
- name: Extract version from branch name (for hotfix branches)
3048
if: startsWith(github.event.pull_request.head.ref, 'hotfix/')
3149
run: |
3250
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
3351
VERSION=${BRANCH_NAME#hotfix/}
34-
3552
echo "RELEASE_VERSION=$VERSION" >> $GITHUB_ENV
53+
echo "BRANCH=$BRANCH_NAME" >> $GITHUB_ENV
3654
55+
release:
56+
runs-on: ubuntu-latest
57+
needs: metadata
58+
env:
59+
version: ${{ needs.metadata.outputs.version }}
60+
app_token: ${{ needs.metadata.outputs.app_token }}
61+
steps:
3762
- name: Create Release
3863
uses: thomaseizinger/create-release@2.0.0
3964
env:
4065
GITHUB_TOKEN: ${{ secrets.GREEN_ECOLUTION_PAT }}
4166
with:
4267
target_commitish: ${{ github.event.pull_request.merge_commit_sha }}
43-
tag_name: ${{ env.RELEASE_VERSION }}
44-
name: ${{ env.RELEASE_VERSION }}
68+
tag_name: ${{ env.version }}
69+
name: ${{ env.version }}
4570
draft: false
4671
prerelease: false
4772

4873
- name: Merge main into develop branch
4974
uses: thomaseizinger/create-pull-request@1.4.0
5075
env:
51-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
76+
GITHUB_TOKEN: ${{ env.app_token }}
5277
with:
5378
head: main
5479
base: develop
@@ -57,6 +82,14 @@ jobs:
5782
This PR merges the main branch back into develop.
5883
This happens to ensure that the updates that happend on the release branch, i.e. CHANGELOG and manifest updates are also present on the develop branch.
5984
85+
build_and_push_docker:
86+
runs-on: ubuntu-latest
87+
needs: metadata
88+
permissions:
89+
packages: write
90+
env:
91+
version: ${{ needs.metadata.outputs.version }}
92+
steps:
6093
- name: Checkout code
6194
uses: actions/checkout@v4
6295

@@ -70,20 +103,31 @@ jobs:
70103
username: ${{ github.actor }}
71104
password: ${{ secrets.GITHUB_TOKEN }}
72105

73-
- name: set lower case owner name
74-
run: |
75-
echo "REPO_LC=${REPO,,}" >>${GITHUB_ENV}
76-
env:
77-
REPO: '${{ github.repository }}'
78-
79106
- name: Build and push
80107
uses: docker/build-push-action@v6
81108
with:
82109
context: .
83110
file: ./.docker/Dockerfile.prod
84111
platforms: linux/amd64
85112
push: true
86-
tags: ghcr.io/${{ env.REPO_LC }}:${{ env.RELEASE_VERSION }}, ghcr.io/${{ env.REPO_LC }}:latest
113+
tags: ghcr.io/${{ github.repository }}:${{ env.version }}, ghcr.io/${{ github.repository }}:latest
114+
115+
update_deployment:
116+
runs-on: ubuntu-latest
117+
needs: [build_and_push_docker, metadata]
118+
permissions:
119+
contents: write
120+
env:
121+
version: ${{ needs.metadata.outputs.version }}
122+
branch: ${{ needs.metadata.outputs.branch }}
123+
app_token: ${{ needs.metadata.outputs.app_token }}
124+
app_username: ${{ needs.metadata.outputs.app_username }}
125+
app_email: ${{ needs.metadata.outputs.app_email }}
126+
steps:
127+
- name: Checkout code
128+
uses: actions/checkout@v4
129+
with:
130+
token: ${{ env.app_token }}
87131

88132
- name: 'Setup yq'
89133
uses: dcarbone/install-yq-action@v1.3.1
@@ -93,31 +137,15 @@ jobs:
93137

94138
- name: Initialize mandatory git config
95139
run: |
96-
git config --global user.name 'GitHub Actions'
97-
git config --global user.email 'noreply@github.com'
98-
99-
- name: Bump version in values/develop.yaml
100-
run: yq -i '.deployment.image.tag=strenv(RELEASE_VERSION)' ./k8s/values/prod.yaml
101-
102-
- name: Commit k8s values files
103-
id: make-commit
104-
run: |
105-
git add ./k8s/values/prod.yaml
106-
git commit --message "chore: update prod image to version ${{ env.RELEASE_VERSION }}"
107-
108-
echo "::set-output name=commit::$(git rev-parse HEAD)"
140+
git config --global user.name ${{ env.app_username }}
141+
git config --global user.email ${{ env.app_email }}
109142
110-
- name: Commit k8s values files
111-
id: make-commit
143+
- name: Bump version in values/stage.yaml
112144
run: |
113-
git add ./k8s/values/prod.yaml
114-
git commit --message "chore: update prod image to version ${{ env.RELEASE_VERSION }}"
145+
yq -i '.deployment.image.tag=strenv(version)' ./k8s/values/stage.yaml
146+
git add ./k8s/values/stage.yaml
147+
git commit --message "chore: update stage image to version ${{ env.version }} [skip ci]"
115148
116-
- name: Commit k8s values and push changes
117-
env:
118-
GITHUB_TOKEN: ${{ secrets.GREEN_ECOLUTION_PAT }}
149+
- name: Push k8s changes
119150
run: |
120-
git add ./k8s/values/stage.yaml
121-
git commit --message "chore: update stage image to version ${{ env.RELEASE_VERSION }}" \
122-
&& git push origin ${{ steps.extract_branch.outputs.branch }} \
123-
|| echo "No changes to commit"
151+
git push origin ${{ env.branch }} || echo "No changes to commit"

.github/workflows/build-and-push-stage.yml

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ name: Build and Push Docker Image Staging
88
- develop
99

1010
jobs:
11-
build_and_deploy_stage:
11+
build_and_push_docker:
1212
runs-on: ubuntu-latest
1313
permissions:
14-
contents: write
1514
packages: write
1615
actions: write
16+
outputs:
17+
branch: ${{ steps.extract_branch.outputs.branch }}
18+
version: ${{ env.RELEASE_VERSION }}
1719
steps:
1820
- name: Checkout code
1921
uses: actions/checkout@v4
@@ -61,12 +63,6 @@ jobs:
6163
username: ${{ github.actor }}
6264
password: ${{ secrets.GITHUB_TOKEN }}
6365

64-
- name: Set lower case owner name
65-
run: |
66-
echo "REPO_LC=${REPO,,}" >>${GITHUB_ENV}
67-
env:
68-
REPO: '${{ github.repository }}'
69-
7066
- name: Set commit sha
7167
run: |
7268
echo "COMMIT_SHA=$(git rev-parse --short $GITHUB_SHA)" >> $GITHUB_ENV
@@ -78,27 +74,57 @@ jobs:
7874
file: ./.docker/Dockerfile.stage
7975
platforms: linux/amd64
8076
push: true
81-
tags: ghcr.io/${{ env.REPO_LC }}:${{ env.RELEASE_VERSION }},ghcr.io/${{ env.REPO_LC }}:${{ env.COMMIT_SHA }}
77+
tags: ghcr.io/${{ github.repository }}:${{ env.RELEASE_VERSION }},ghcr.io/${{ github.repository }}:${{ env.COMMIT_SHA }}
78+
79+
update_deployment:
80+
runs-on: ubuntu-latest
81+
needs: build_and_push_docker
82+
permissions:
83+
contents: write
84+
env:
85+
branch: ${{ needs.build_and_push_docker.outputs.branch }}
86+
version: ${{ needs.build_and_push_docker.outputs.version }}
87+
steps:
88+
- name: Get Green Ecolution App Token
89+
uses: actions/create-github-app-token@v2
90+
id: app-token
91+
with:
92+
app-id: ${{ vars.APP_ID }}
93+
private-key: ${{ secrets.APP_PRIVATE_KEY }}
94+
permission-contents: write
95+
96+
- name: Checkout code
97+
uses: actions/checkout@v4
98+
with:
99+
token: ${{ steps.app-token.outputs.token }}
100+
fetch-depth: 0
101+
fetch-tags: true
82102

83103
- name: 'Setup yq'
84104
uses: dcarbone/install-yq-action@v1.3.1
85105
with:
86106
version: 'v4.44.3'
87107
force: true
88108

109+
- name: Bump version in values/stage.yaml
110+
run: yq -i '.deployment.image.tag=strenv(version)' ./k8s/values/stage.yaml
111+
112+
- name: Get Green Ecolution App User ID
113+
id: get-user-id
114+
env:
115+
GH_TOKEN: ${{ steps.app-token.outputs.token }}
116+
run: echo "user-id=$(gh api "/users/${{ steps.app-token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"
117+
89118
- name: Initialize mandatory git config
90119
run: |
91-
git config --global user.name 'GitHub Actions'
92-
git config --global user.email 'noreply@github.com'
93-
94-
- name: Bump version in values/stage.yaml
95-
run: yq -i '.deployment.image.tag=strenv(RELEASE_VERSION)' ./k8s/values/stage.yaml
120+
git config --global user.name '${{ steps.app-token.outputs.app-slug }}[bot]'
121+
git config --global user.email '${{ steps.get-user-id.outputs.user-id }}+${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.com'
96122
97123
- name: Commit k8s values and push changes
98124
env:
99-
GITHUB_TOKEN: ${{ secrets.GREEN_ECOLUTION_PAT }}
125+
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
100126
run: |
101127
git add ./k8s/values/stage.yaml
102-
git commit --message "chore: update stage image to version ${{ env.RELEASE_VERSION }}" \
103-
&& git push origin ${{ steps.extract_branch.outputs.branch }} \
128+
git commit --message "chore: update stage image to version ${{ env.version }} [skip ci]" \
129+
&& git push origin ${{ env.branch }} \
104130
|| echo "No changes to commit"

.github/workflows/draft-new-release.yml

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,32 @@ jobs:
1111
draft_new_release:
1212
runs-on: ubuntu-latest
1313
permissions:
14-
contents: write
1514
packages: write
16-
actions: write
17-
pull-requests: write
1815
steps:
16+
- name: Get Green Ecolution App Token
17+
uses: actions/create-github-app-token@v2
18+
id: app-token
19+
with:
20+
app-id: ${{ vars.APP_ID }}
21+
private-key: ${{ secrets.APP_PRIVATE_KEY }}
22+
permission-contents: write
23+
permission-pull-request: write
24+
1925
- name: Checkout code
2026
uses: actions/checkout@v4
27+
with:
28+
token: ${{ steps.app-token.outputs.token }}
29+
30+
- name: Get Green Ecolution App User ID
31+
id: get-user-id
32+
env:
33+
GH_TOKEN: ${{ steps.app-token.outputs.token }}
34+
run: echo "user-id=$(gh api "/users/${{ steps.app-token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"
35+
36+
- name: Initialize mandatory git config
37+
run: |
38+
git config --global user.name '${{ steps.app-token.outputs.app-slug }}[bot]'
39+
git config --global user.email '${{ steps.get-user-id.outputs.user-id }}+${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.com'
2140
2241
- name: Create release branch
2342
run: git checkout -b release/${{ github.event.inputs.version }}
@@ -27,11 +46,6 @@ jobs:
2746
with:
2847
tag: ${{ github.event.inputs.version }}
2948

30-
- name: Initialize mandatory git config
31-
run: |
32-
git config user.name "GitHub Actions"
33-
git config user.email noreply@github.com
34-
3549
- name: Bump version in package.json
3650
run: yarn version --new-version ${{ github.event.inputs.version }} --no-git-tag-version
3751

@@ -54,14 +68,12 @@ jobs:
5468
echo "commit=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
5569
5670
- name: Push new branch
57-
env:
58-
GITHUB_TOKEN: ${{ secrets.GREEN_ECOLUTION_PAT }}
5971
run: git push origin release/${{ github.event.inputs.version }}
6072

6173
- name: Create pull request
6274
uses: thomaseizinger/create-pull-request@1.4.0
6375
env:
64-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
76+
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
6577
with:
6678
head: release/${{ github.event.inputs.version }}
6779
base: main

0 commit comments

Comments
 (0)