-
Notifications
You must be signed in to change notification settings - Fork 0
146 lines (134 loc) · 4.87 KB
/
.build_image.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
name: Build Image and Deploy
on:
workflow_dispatch:
push:
branches:
- "trunk"
pull_request:
branches:
- "trunk"
concurrency:
group: docker-${{ github.ref }}
cancel-in-progress: true
env:
RABBITMQ_DEFAULT_USER: "guest"
RABBITMQ_DEFAULT_PASS: "guest"
jobs:
prep:
runs-on: ubuntu-latest
outputs:
taggedImage: ${{ steps.prep.outputs.tagged_image }}
tag: ${{ steps.prep.outputs.tag }}
registry: ${{ steps.prep.outputs.registry }}
shortSha: ${{ steps.prep.outputs.short_sha}}
branchName: ${{ steps.prep.outputs.branch_name }}
latestTag: ${{ steps.prep.outputs.latest_tag }}
repositoryName: ${{ steps.prep.outputs.repository_name }}
steps:
- name: Git branch name
id: git-branch-name
uses: EthanSK/git-branch-name-action@v1
- name: Prepare info
id: prep
run: |
SHORT_SHA=$(echo $GITHUB_SHA | head -c7)
REPO=$(echo '${{ github.repository }}' | awk -F '/' '{print $2}')
TAG=${{ env.GIT_BRANCH_NAME }}-$(echo $GITHUB_SHA | head -c7)
IMAGE=ideacrew/$REPO
echo ::set-output name=tagged_image::ghcr.io/${IMAGE}:${TAG}
echo ::set-output name=tag::${TAG}
echo ::set-output name=short_sha::$SHORT_SHA
echo ::set-output name=branch_name::${{ env.GIT_BRANCH_NAME }}
echo ::set-output name=repository_name::$REPO
echo ::set-output name=latest_tag::ghcr.io/${IMAGE}:latest
# Uses buildx to build and push the image
build-and-upload-image:
needs: [prep]
runs-on: ubuntu-latest
services:
rabbitmq:
image: rabbitmq:latest
ports:
- 5672:5672
- 15672:15672
options: >-
--name "rabbitmq"
--health-cmd "rabbitmqctl node_health_check"
--health-interval 10s
--health-timeout 5s
--health-retries 5
mongo:
image: mongo:4.2
ports:
- 27017:27017
options: >-
--name "mongo"
--health-cmd mongo
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
# Check out repository
- uses: actions/checkout@v3
- name: Add git HEAD info to docker image
run: |
git show --quiet HEAD > release.txt
git show --quiet HEAD > public/release.txt
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
with:
install: true
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
# Key is named differently to avoid collision
key: ${{ runner.os }}-multi-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-multi-buildx
- name: Login to GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build Image
uses: docker/build-push-action@v2
with:
context: .
builder: ${{ steps.buildx.outputs.name }}
file: .docker/production/Dockerfile.gha
# Set the desired build target here
target: deploy
# needed to access mongo and rabbit on GHA machine
network: host
# send to public registry if not a pull request
push: ${{ github.event_name != 'pull_request' }}
# create local image (for scanning) if it is a pull request
load: ${{ github.event_name == 'pull_request' }}
tags: ${{ needs.prep.outputs.taggedImage }}, ${{ needs.prep.outputs.latestTag }}
cache-from: type=local,src=/tmp/.buildx-cache
# Note the mode=max here
# More: https://github.com/moby/buildkit#--export-cache-options
# And: https://github.com/docker/buildx#--cache-tonametypetypekeyvalue
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new
build-args: |
HOSTNAME=172.17.0.1
BUNDLE_GITHUB__COM=x-access-token:${{ secrets.X12_TOKEN }}
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
notify-slack:
if: github.event_name != 'pull_request'
needs: [prep, build-and-upload-image]
runs-on: ubuntu-latest
steps:
- name: Post to a Slack channel
id: slack
uses: slackapi/[email protected]
with:
channel-id: "docker-images-${{ needs.prep.outputs.repositoryName }}"
slack-message: "New image pushed: ${{ needs.prep.outputs.taggedImage }} built from <https://github.com/ideacrew/${{ needs.prep.outputs.repositoryName }}/commit/${{ needs.prep.outputs.shortSha }}|${{ needs.prep.outputs.shortSha }}> on `${{ needs.prep.outputs.branchName }}`"
env:
SLACK_BOT_TOKEN: ${{ secrets.YELLR_BOT_TOKEN }}