Create docker-image.yml #9
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: Java CI/CD Pipeline | |
on: | |
push: | |
branches: | |
- main | |
- develop | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
services: | |
mysql_database: | |
image: mysql:5.7 | |
env: | |
MYSQL_ROOT_PASSWORD: root | |
MYSQL_DATABASE: mysql_db | |
ports: | |
- 3306:3306 | |
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v2 | |
with: | |
java-version: '17' | |
distribution: 'adopt' | |
- name: Build with Gradle | |
run: ./gradlew build | |
env: | |
DATABASE_URL: jdbc:mysql://127.0.0.1:3306/mysql_db?useSSL=false | |
- name: Run Tests with Gradle | |
if: github.ref == 'refs/heads/develop' | |
run: ./gradlew test | |
env: | |
DATABASE_URL: jdbc:mysql://127.0.0.1:3306/mysql_db?useSSL=false | |
- name: Configure AWS credentials | |
if: github.ref == 'refs/heads/main' | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ap-northeast-2 | |
- name: Login to Amazon ECR | |
if: github.ref == 'refs/heads/main' | |
uses: aws-actions/amazon-ecr-login@v1 | |
- name: Build, tag, and push image to Amazon ECR | |
if: github.ref == 'refs/heads/main' | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
ECR_REPOSITORY: todays-menu | |
IMAGE_TAG: ${{ github.sha }} | |
run: | | |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | |
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
- name: Setup Kustomize | |
if: github.ref == 'refs/heads/main' | |
uses: imranismail/setup-kustomize@v1 | |
- name: Checkout kustomize repository | |
if: github.ref == 'refs/heads/main' | |
uses: actions/checkout@v2 | |
with: | |
repository: royroyee/todays-menu-gitops | |
ref: main | |
token: ${{ secrets.ACTION_TOKEN }} | |
path: todays-menu-gitops | |
- name: Update Kubernetes resources | |
if: github.ref == 'refs/heads/main' | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
ECR_REPOSITORY: todays-menu | |
IMAGE_TAG: ${{ github.sha }} | |
run: | | |
cd todays-menu-gitops/backend | |
sed -i "s|image:.*|image: $ECR_REGISTRY\/$ECR_REPOSITORY:$IMAGE_TAG|g" deployment.yaml | |
- name: Commit files | |
if: github.ref == 'refs/heads/main' | |
env: | |
ECR_REPOSITORY: todays-menu | |
IMAGE_TAG: ${{ github.run_number }} | |
run: | | |
cd todays-menu-gitops | |
git config --global user.email "kim131325@naver.com" | |
git config --global user.name "royroyee" | |
git add backend/deployment.yaml | |
git commit -m "Update image $ECR_REPOSITORY tag $IMAGE_TAG" | |
git push -u origin main | |
continue-on-error: true |