@@ -2,15 +2,15 @@ name: CI/CD — Backend (Build → Push → Deploy)
22
33on :
44 push :
5- branches : [ dev ]
5+ branches : [ dev, staging ]
66 pull_request :
7- branches : [ dev ]
7+ branches : [ dev, staging ]
88 workflow_dispatch :
99 inputs :
1010 tag :
1111 description : " 배포할 이미지 태그 (ex. release or <GIT_SHA>)"
1212 required : false
13- default : " release "
13+ default : " dev "
1414
1515concurrency :
1616 group : cicd-${{ github.ref_name }}
@@ -81,17 +81,32 @@ jobs:
8181 username : ${{ github.actor }}
8282 password : ${{ secrets.GITHUB_TOKEN }}
8383
84- - name : Build & Push image
84+ - name : Decide tags by branch
85+ if : github.event_name == 'push'
86+ id : tags
87+ shell : bash
88+ run : |
89+ REF="${{ github.ref_name }}" # dev / staging
90+ SHORT_SHA="${GITHUB_SHA::7}"
91+ SAFE_REF=$(echo "$REF" | tr '[:upper:]' '[:lower:]' | sed -E 's#[^a-z0-9._-]+#-#g')
92+ echo "channel=$SAFE_REF" >> $GITHUB_OUTPUT # ex) dev, staging
93+ echo "version=${SAFE_REF}-${SHORT_SHA}" >> $GITHUB_OUTPUT # ex) dev-abc1234
94+
95+ - name : Build & Push (branch-tagged)
8596 if : github.event_name == 'push'
8697 run : |
87- GIT_SHA=${{ github.sha }}
88- docker build -f "${{ steps.df.outputs.path }}" -t $IMAGE:release -t $IMAGE:$GIT_SHA "${{ steps.df.outputs.ctx }}"
89- docker push $IMAGE:release
90- docker push $IMAGE:$GIT_SHA
98+ CHAN=${{ steps.tags.outputs.channel }} # dev | staging
99+ VER=${{ steps.tags.outputs.version }} # dev-<sha> | staging-<sha>
100+ docker build -f "${{ steps.df.outputs.path }}" \
101+ -t $IMAGE:$CHAN \
102+ -t $IMAGE:$VER \
103+ "${{ steps.df.outputs.ctx }}"
104+ docker push $IMAGE:$CHAN
105+ docker push $IMAGE:$VER
91106
92107 deploy :
93108 needs : build-and-push
94- if : github.event_name == 'push' && (github.ref == 'refs/heads/dev') &&
109+ if : github.event_name == 'push' && (github.ref == 'refs/heads/dev' || github.ref == 'refs/heads/staging' ) &&
95110 (needs.build-and-push.result == 'success')
96111 || (github.event_name == 'workflow_dispatch')
97112 runs-on : ubuntu-latest
@@ -103,7 +118,7 @@ jobs:
103118 if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ github.event.inputs.tag }}" ]; then
104119 echo "value=${{ github.event.inputs.tag }}" >> $GITHUB_OUTPUT
105120 else
106- echo "value=release " >> $GITHUB_OUTPUT
121+ echo "value=${{ github.ref_name }} " >> $GITHUB_OUTPUT
107122 fi
108123
109124 - name : Deploy
@@ -115,4 +130,10 @@ jobs:
115130 port : ${{ secrets.CD_PORT }}
116131 script : |
117132 export DEPLOY_TAG='${{ steps.tag.outputs.value }}'
118- bash -lc '/srv/docsa/infra/deploy.sh'
133+ if [ "${{ github.ref_name }}" = "staging" ]; then
134+ bash -lc '/srv/docsa-stg/infra/deploy.sh'
135+ else
136+ bash -lc '/srv/docsa/infra/deploy.sh'
137+ fi
138+
139+
0 commit comments