Build and push Docker image #17
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 and push Docker image | |
on: | |
workflow_run: | |
workflows: NX-CI | |
types: completed | |
branches: [main] | |
env: | |
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} | |
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_TOKEN }} | |
jobs: | |
check-skip: | |
if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
runs-on: ubuntu-latest | |
outputs: | |
commit_message: ${{ steps.get_commit_message.outputs.commit_message }} | |
skip_docker: ${{ steps.check_skip_docker.outputs.skip_docker }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Get commit message | |
id: get_commit_message | |
run: | | |
commit_message=$(git log -1 --pretty=%B) | |
commit_message="${commit_message//'%'/'%25'}" | |
commit_message="${commit_message//$'\n'/' '}" | |
commit_message="${commit_message//$'\r'/' '}" | |
echo $commit_message | |
echo "commit_message=$commit_message" >> $GITHUB_OUTPUT | |
- name: Check if commit message contains [skip docker] | |
id: check_skip_docker | |
run: | | |
if [[ "${{ steps.get_commit_message.outputs.commit_message }}" =~ "[skip docker]" ]]; then | |
echo "skip_docker=true" >> $GITHUB_OUTPUT | |
echo "skip docker found, skipping docker build and push..." | |
else | |
echo "skip_docker=false" >> $GITHUB_OUTPUT | |
fi | |
build: | |
needs: [check-skip] | |
if: ${{ needs.check-skip.outputs.skip_docker != 'true' }} | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ env.DOCKERHUB_USERNAME }} | |
password: ${{ env.DOCKERHUB_PASSWORD }} | |
- name: Free disk space up | |
run: rm -rf /opt/hostedtoolcache | |
- name: Install dotenvx | |
run: curl -sfS https://dotenvx.sh/install.sh | sh | |
- name: Set short SHA | |
id: set_short_sha | |
run: echo "SHORT_SHA=$(echo ${{ github.sha }} | cut -c 1-7)" >> $GITHUB_OUTPUT | |
- name: 'Create .env file' | |
run: | | |
touch .env | |
echo "# .env created" >> .env | |
echo "# .env created" >> ./apps/fe/.env | |
echo "# .env created" >> ./apps/be/.env | |
cat .env | |
- name: Set COMPOSE_TAG as short commit SHA | |
run: dotenvx set --plain COMPOSE_TAG ${{ steps.set_short_sha.outputs.SHORT_SHA }} | |
- name: Build and push images with short commit SHA tag | |
id: build_and_push | |
run: | | |
docker compose -f docker-compose.yml build | |
docker compose -f docker-compose.yml push | |
- name: Set COMPOSE_TAG as latest | |
if: contains(needs.check-skip.outputs.commit_message, '[docker-latest]') | |
run: dotenvx set --plain COMPOSE_TAG latest | |
- name: Build and push images with latest tag | |
id: build_and_push_latest | |
if: contains(needs.check-skip.outputs.commit_message, '[docker-latest]') | |
run: | | |
docker compose -f docker-compose.yml build | |
docker compose -f docker-compose.yml push | |
- name: Remove .env file | |
run: | | |
rm .env |