Skip to content

Merge pull request #42 from Leets-Official/feat/#38 #16

Merge pull request #42 from Leets-Official/feat/#38

Merge pull request #42 from Leets-Official/feat/#38 #16

Workflow file for this run

name: Deploy
on:
push:
branches:
- develop
- main
env:
REGISTRY: ghcr.io
permissions:
contents: read
concurrency:
group: deploy-${{ github.ref_name }}
cancel-in-progress: false
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Run tests
run: ./gradlew test
build-and-push:
needs: test
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
image: ${{ steps.vars.outputs.image }}
tag: ${{ steps.vars.outputs.tag }}
steps:
- uses: actions/checkout@v4
- name: Compute lowercase image name and tag
id: vars
run: |
echo "image=${REGISTRY}/$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT"
if [ "${{ github.ref_name }}" = "main" ]; then
echo "tag=prod-latest" >> "$GITHUB_OUTPUT"
else
echo "tag=dev-latest" >> "$GITHUB_OUTPUT"
fi
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64
push: true
tags: ${{ steps.vars.outputs.image }}:${{ steps.vars.outputs.tag }}
deploy-dev:
if: github.ref_name == 'develop'
needs: build-and-push
runs-on: ubuntu-latest
environment: development
env:
AWS_REGION: us-east-1
SG_ID: sg-04b6e6175adabdb30
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Whitelist runner IP for SSH
run: |
RUNNER_IP=$(curl -s https://api.ipify.org)
echo "RUNNER_IP=$RUNNER_IP" >> "$GITHUB_ENV"
aws ec2 authorize-security-group-ingress \
--group-id "$SG_ID" \
--protocol tcp --port 22 --cidr "${RUNNER_IP}/32"
- name: Deploy to EC2 (dev)
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
script: |
cd /srv/jobis-dev
docker compose pull
docker compose up -d
- name: Revoke runner IP from SSH
if: always()
run: |
aws ec2 revoke-security-group-ingress \
--group-id "$SG_ID" \
--protocol tcp --port 22 --cidr "${RUNNER_IP}/32"
deploy-prod:
if: github.ref_name == 'main'
needs: build-and-push
runs-on: ubuntu-latest
environment: production
env:
AWS_REGION: us-east-1
SG_ID: sg-0a99d63a691fd07c8
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Whitelist runner IP for SSH
run: |
RUNNER_IP=$(curl -s https://api.ipify.org)
echo "RUNNER_IP=$RUNNER_IP" >> "$GITHUB_ENV"
aws ec2 authorize-security-group-ingress \
--group-id "$SG_ID" \
--protocol tcp --port 22 --cidr "${RUNNER_IP}/32"
- name: Deploy to EC2 (prod)
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
script: |
cd /srv/jobis-prod
docker compose pull
docker compose up -d
- name: Revoke runner IP from SSH
if: always()
run: |
aws ec2 revoke-security-group-ingress \
--group-id "$SG_ID" \
--protocol tcp --port 22 --cidr "${RUNNER_IP}/32"