-
Notifications
You must be signed in to change notification settings - Fork 1
88 lines (77 loc) · 2.93 KB
/
build.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
name: Docker Image CI
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
ec2_ips: ${{ steps.get_instance_ips.outputs.ips }}
image_version: ${{ steps.image_version.outputs.gitsha }}
steps:
- uses: actions/checkout@v3
- name: Set image version (tag)
id: image_version
run: echo "gitsha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
- name: login to Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: sarahabuirmeileh
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build the Docker image
run: docker build . --file Dockerfile --tag ghcr.io/sarahabuirmeileh/serve-and-shine:${{ steps.image_version.outputs.gitsha }}
- name: Puplish docker image
run: docker push ghcr.io/sarahabuirmeileh/serve-and-shine:${{ steps.image_version.outputs.gitsha }}
- name: Get EC2 Instance IPs
id: get_instance_ips
run: |
instances=$(aws ec2 describe-instances --filters "Name=tag:aws:autoscaling:groupName,Values=serve-and-shine-asg" --query 'Reservations[*].Instances[*].[PublicIpAddress]' --output json | jq --compact-output 'flatten')
echo "ips=$instances" >> $GITHUB_OUTPUT
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
deploy:
needs: ["build"]
runs-on: ubuntu-latest
strategy:
matrix:
ip: ${{ fromJson(needs.build.outputs.ec2_ips) }}
steps:
- uses: actions/checkout@v3
- name: configure docker host
uses: khaledez/configure-docker-host@v1
with:
host: ${{ matrix.ip }}
user: github
ssh-private-key: ${{ secrets.DEPLOYMENT_KEY }}
- name: login to Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: sarahabuirmeileh
password: ${{ secrets.GITHUB_TOKEN }}
- name: deploy
run: docker compose -f docker-compose-prod.yml up -d
env:
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
GIT_VERSISON: ${{ needs.build.outputs.image_version }}
SECRET_KEY: ${{ secrets.SECRET_KEY }}
DB_HOST: ${{ secrets.DB_HOST }}
DB_PORT: ${{ secrets.DB_PORT }}
DB_USERNAME: ${{ secrets.DB_USERNAME }}
DB_NAME: ${{ secrets.DB_NAME }}
AWS_BUCKET_NAME: ${{ secrets.AWS_BUCKET_NAME }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_CERTIFICATES_BUCKET_NAME: ${{ secrets.AWS_CERTIFICATES_BUCKET_NAME }}
PORT: ${{ secrets.PORT }}