build-docker-image #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |