✨ Feat: api 접근 권한 설정 #27
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: CI/CD Pipeline | |
on: | |
push: | |
branches: | |
- master | |
pull_request: | |
branches: | |
- master | |
permissions: | |
contents: read | |
env: | |
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} | |
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }} | |
PROJECT_ID: ${{ secrets.GCE_PROJECT_ID }} | |
INSTANCE_NAME: ${{ secrets.GCE_INSTANCE_NAME }} | |
INSTANCE_ZONE: ${{ secrets.GCE_INSTANCE_ZONE }} | |
SA_KEY: ${{ secrets.GCE_SA_KEY }} | |
CONTAINER_NAME: say-better | |
ENV_FILE: ${{ secrets.SERVER_ENV_FILE }} | |
jobs: | |
build: | |
name: CI | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
# Gradle 캐싱 빌드 시간 향상 | |
- name: Gradle Caching | |
uses: actions/cache@v3 | |
with: | |
path: | | |
~/.gradle/caches | |
~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*','**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
- name: Grant execute Permission for gradlew | |
run: chmod +x gradlew | |
- name: Build with Gradle | |
run: ./gradlew build -x test | |
- name: Login to DockerHub | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ env.DOCKERHUB_USERNAME }} | |
password: ${{ env.DOCKERHUB_PASSWORD }} | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: true | |
tags: ${{ env.DOCKERHUB_USERNAME }}/say-better:latest | |
deploy: | |
name: CD | |
runs-on: ubuntu-latest | |
needs: build | |
permissions: | |
contents: 'read' | |
id-token: 'write' | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Setup Cloud SDK | |
uses: google-github-actions/[email protected] | |
with: | |
project_id: ${{ env.PROJECT_ID }} | |
service_account_key: ${{ env.SA_KEY }} | |
export_default_credentials: true | |
- name: Deploy to GCE | |
run: | | |
gcloud compute ssh ${{ env.INSTANCE_NAME }} --zone ${{ env.INSTANCE_ZONE }} \ | |
--command \ | |
"docker pull ${{ env.DOCKERHUB_USERNAME }}/say-better:latest && \ | |
docker stop ${{ env.CONTAINER_NAME }} || true && \ | |
docker rm ${{ env.CONTAINER_NAME }} || true && \ | |
echo 'docker run -d -p 8080:8080 \ | |
--name ${{ env.CONTAINER_NAME }} \ | |
--env-file ${{ env.ENV_FILE }} \ | |
${{ env.DOCKERHUB_USERNAME }}/say-better:latest' \ | |
> run_docker.sh && \ | |
chmod +x run_docker.sh && \ | |
./run_docker.sh" |