[#90] Fix: 버그 수정 (#91) #144
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Deploy | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v2 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v2 | |
with: | |
java-version: '17' | |
distribution: 'adopt' | |
- name: Decode application.yml | |
run: | | |
echo "${{ secrets.ENCODED_APPLICATION_YML }}" | base64 --decode > ./src/main/resources/application.yml | |
cat ./src/main/resources/application.yml | |
- name: Grant execute permission for Gradlew | |
run: chmod +x ./gradlew | |
- name: Cache Gradle packages | |
uses: actions/cache@v2 | |
with: | |
path: | | |
~/.gradle/caches | |
~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
- name: Build with Gradle | |
run: ./gradlew build --no-daemon | |
- name: Build Docker image | |
run: docker build -t ${{ secrets.DOCKERHUB_URL }}:${{ github.sha }} . | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Push Docker image | |
run: docker push ${{ secrets.DOCKERHUB_URL }}:${{ github.sha }} | |
- name: Notify Slack | |
uses: rtCamp/action-slack-notify@v2 | |
env: | |
SLACK_CHANNEL: git_action | |
SLACK_COLOR: '#000000' | |
SLACK_MESSAGE: ${{ steps.get_commit_message.outputs.message }} | |
SLACK_TITLE: '이미지 빌드 결과' | |
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }} | |
if: success() | |
deploy: | |
needs: build | |
runs-on: ubuntu-latest | |
environment: production | |
steps: | |
- name: Checkout the repository | |
uses: actions/checkout@v2 | |
with: | |
repository: ${{ secrets.MANIFEST_REPO }} | |
ref: ${{ secrets.MANIFEST_REPO_BRANCH }} | |
token: ${{ secrets.MY_PAT }} | |
- name: Configure git | |
run: | | |
git config user.email ${{ secrets.GIT_USER_EMAIL }} | |
git config user.name ${{ secrets.GIT_USER_NAME }} | |
- name: Make changes | |
run: | | |
cd ${{ secrets.MANIFEST_FOLDER_NAME }} | |
FILENAME=${{ secrets.DEPLOYMENT_FILE_NAME }} | |
IMAGE_TAG=${{ secrets.DOCKERHUB_URL }}:${{ github.sha }} | |
REPO_NAME=$(echo "${{ github.repository }}" | awk -F '/' '{print $2}') | |
if [ ! -f "$FILENAME" ]; then | |
echo "apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: $REPO_NAME | |
spec: | |
replicas: ${{ secrets.POD_REPLICAS }} | |
selector: | |
matchLabels: | |
app: $REPO_NAME | |
template: | |
metadata: | |
labels: | |
app: $REPO_NAME | |
spec: | |
containers: | |
- name: $REPO_NAME | |
image: $IMAGE_TAG | |
ports: | |
- containerPort: 8080" > $FILENAME | |
else | |
yq eval -i ".spec.template.spec.containers[0].image = \"$IMAGE_TAG\"" $FILENAME | |
yq eval -i ".spec.replicas = ${{ secrets.POD_REPLICAS }}" $FILENAME | |
fi | |
- name: Commit and Push | |
run: | | |
SHORT_SHA="${{ github.sha }}" | |
SHORT_SHA="${SHORT_SHA:0:6}" | |
REPO_NAME="${{ github.repository }}" | |
git add . | |
git commit -m "update: $SHORT_SHA, $REPO_NAME to ${{ secrets.DEPLOYMENT_FILE_NAME }}" | |
git push |