forked from ZauJulio/API_BaseCAEPI
-
Notifications
You must be signed in to change notification settings - Fork 0
111 lines (98 loc) · 3.88 KB
/
build-docker-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
name: build-docker-image
on:
workflow_dispatch:
workflow_call:
push:
paths:
- 'requirements.txt'
- 'app/**'
- 'dockerfile'
- '.dockerignore'
branches:
- '!*'
- 'develop'
- 'main'
env:
BOT_USER_NAME: ${{ secrets.BOT_USER_NAME }}
DOCKER_PAT: ${{ secrets.DOCKER_PAT }}
jobs:
build:
runs-on: ubuntu-20.04
timeout-minutes: 10
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- uses: styfle/[email protected]
with:
workflow_id: build-docker-image.yml
access_token: ${{ github.token }}
- uses: actions/checkout@v3
with:
fetch-depth: 10
persist-credentials: false
- name: Git repository Setup
run: |
GIT_BRANCH=${{ github.ref }}
echo "REPOSITORY_NAME=$(echo "$GITHUB_REPOSITORY" | awk -F / '{print $2}')" >> $GITHUB_ENV
echo "GIT_BRANCH=$(echo "${GIT_BRANCH/refs\/heads\//}")" >> $GITHUB_ENV
echo "COMMIT_DATE=$(git log -n 1 --pretty='format:%cd' --date=format:'%y-%m-%d')" >> $GITHUB_ENV
echo "COMMIT_TIME=$(git log -n 1 --pretty='format:%cd' --date=format:'%H-%M-%S')" >> $GITHUB_ENV
echo "CURRENT_DATETIME=$(date +'%Y-%m-%d %H:%M:%S')" >> $GITHUB_ENV
echo "GH_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-7)" >> $GITHUB_ENV
git config --global user.name "$BOT_USER_NAME"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.BOT_USER_NAME }} # TODO: REPLACE THIS BY THE REAL BOT NAME
password: ${{ secrets.DOCKER_PAT }}
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: |
mendesbarreto/${{ env.REPOSITORY_NAME }}
tags: |
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
type=raw,value=dev,enable=${{ github.ref == format('refs/heads/{0}', 'develop') }}
type=raw,value=${{ env.GIT_BRANCH }}-${{ env.COMMIT_DATE }}-${{ env.COMMIT_TIME }}-${{ env.GH_SHA_SHORT }}
- name: Build and push
uses: docker/build-push-action@v3
timeout-minutes: 15
with:
context: .
network: host
build-args: |
GH_PAT=${{ secrets.GH_PAT }}
BUILD_NR=${{ env.GIT_BRANCH }}-${{ env.GH_SHA_SHORT }}
BUILD_BRANCH=${{ env.GIT_BRANCH }}
BUILD_COMMIT=${{ env.GH_SHA_SHORT }}
COMMIT_DATE=${{ env.COMMIT_DATE }}
BUILD_DATE=${{ env.CURRENT_DATETIME }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
- name: Job Summary
run: |
echo "Docker Image Tag: $GIT_BRANCH-$COMMIT_DATE-$COMMIT_TIME-$GH_SHA_SHORT" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "- Branch: $GIT_BRANCH" >> $GITHUB_STEP_SUMMARY
echo "- Commit Date: $COMMIT_DATE" >> $GITHUB_STEP_SUMMARY
echo "- Commit Time: $COMMIT_TIME" >> $GITHUB_STEP_SUMMARY
echo "- Commit SHA: $GH_SHA_SHORT" >> $GITHUB_STEP_SUMMARY