-
Notifications
You must be signed in to change notification settings - Fork 0
108 lines (93 loc) · 3.45 KB
/
docker-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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
name: Build and deploy
run-name: "[${{ github.event.workflow_run.head_branch }}] ${{ github.event.workflow_run.head_commit.message }}"
on:
workflow_run:
workflows: ["Django tests"]
types:
- completed
branches:
- dev
- main
jobs:
build:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.event == 'push' && github.event.workflow_run.conclusion == 'success' }}
environment: build
concurrency:
group: deploy-group-${{ github.event.workflow_run.head_branch }}
cancel-in-progress: true
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
ref: ${{ github.event.workflow_run.head_branch }}
- run: git branch
- run: env
- name: Debug github refs
run: |
echo "github.event.workflow_run.head_branch = ${{ github.event.workflow_run.head_branch }}"
- name: Check if Dockerfile or requirements.txt was changed
id: check_dependencies
run: |
if git diff --name-only "${{ github.event.workflow_run.head_branch }}" | grep -q -e 'Dockerfile' -e 'requirements.txt'; then
echo "Dockerfile or requirements.txt changed"
echo echo "updated=true" >> $GITHUB_OUTPUT
else
echo "Dockerfile or requirements.txt not changed"
echo echo "updated=false" >> $GITHUB_OUTPUT
fi
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
if: steps.check_dependencies.outputs.updated == 'true'
- name: Log in to DockerHub
uses: docker/login-action@v2
if: steps.check_dependencies.outputs.updated == 'true'
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build Docker image
if: steps.check_dependencies.outputs.updated == 'true'
run: docker build -t ${{ secrets.DOCKER_IMAGE }}:latest .
- name: Push Docker image
if: steps.check_dependencies.outputs.updated == 'true'
run: docker push ${{ secrets.DOCKER_IMAGE }}:latest
deploy:
runs-on: ubuntu-latest
environment: deployment
concurrency:
group: deploy-group-${{ github.event.workflow_run.head_branch }}
cancel-in-progress: true
needs: build
if: needs.build.result == 'success' || needs.build.result == 'skipped'
steps:
- name: Checkout Repository
uses: actions/checkout@v2
with:
ref: ${{ github.event.workflow_run.head_branch }}
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.11'
- name: Install Ansible
run: |
python -m pip install --upgrade pip
pip install ansible
- name: Install ssh keys
# For reference: https://stackoverflow.com/a/70447517
run: |
install -m 600 -D /dev/null ~/.ssh/galaxy
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/galaxy
echo "${{ secrets.SSH_KNOWN_HOSTS }}" > ~/.ssh/known_hosts
- name: Run Ansible Playbook
run: |
if [ ${{ github.event.workflow_run.head_branch }} == "main" ]; then
PLAYBOOK=prod.yml
else
PLAYBOOK=dev.yml
fi
cd ansible
ansible-playbook -i hosts $PLAYBOOK --tags update
env:
ANSIBLE_HOST_KEY_CHECKING: 'False'
ANSIBLE_REMOTE_USER: ${{ secrets.SSH_USER }}
ANSIBLE_PRIVATE_KEY_FILE: ~/.ssh/galaxy