Update README.md #7
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 to Docker Hub | |
on: | |
push: | |
branches: | |
- "main" | |
env: | |
REGISTRY: docker.io | |
IMAGE_NAME: trueberryless/template | |
jobs: | |
build: | |
if: contains(github.event.head_commit.message, 'version') | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out the repo | |
uses: actions/checkout@v3 | |
- name: Build the Docker image | |
run: docker-compose build --no-cache --force-rm | |
test: | |
if: contains(github.event.head_commit.message, 'version') | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out the repo | |
uses: actions/checkout@v3 | |
- name: Test the Docker image | |
run: docker-compose up -d | |
push: | |
if: contains(github.event.head_commit.message, 'version') | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- name: Check out the repo | |
uses: actions/checkout@v3 | |
- name: Check if file exists | |
run: | | |
if [ -f .github/artifacts/version.json ]; then | |
echo "File exists" | |
echo "FILE_EXISTS=true" >> $GITHUB_ENV | |
else | |
echo "File does not exist" | |
echo "FILE_EXISTS=false" >> $GITHUB_ENV | |
fi | |
- name: read_json | |
if: ${{ env.FILE_EXISTS == 'true' }} | |
id: version | |
uses: zoexx/github-action-json-file-properties@release | |
with: | |
file_path: ".github/artifacts/version.json" | |
- name: save environment variables | |
if: ${{ env.FILE_EXISTS == 'true' }} | |
run: | | |
echo "MAJOR=${{steps.version.outputs.major}}" >> $GITHUB_ENV | |
echo "MINOR=${{steps.version.outputs.minor}}" >> $GITHUB_ENV | |
echo "PATCH=${{steps.version.outputs.patch}}" >> $GITHUB_ENV | |
- name: create environment variables | |
if: ${{ env.FILE_EXISTS == 'false' }} | |
run: | | |
echo "MAJOR=0" >> $GITHUB_ENV | |
echo "MINOR=0" >> $GITHUB_ENV | |
echo "PATCH=0" >> $GITHUB_ENV | |
- name: echo environment variables | |
run: | | |
echo ${{ env.MINOR }} | |
echo ${{ env.MINOR }} | |
echo ${{ env.MINOR }} | |
- name: Major version | |
if: contains(github.event.head_commit.message, 'major') | |
run: | | |
echo "New major version" | |
echo "MAJOR=$((${{ env.MAJOR }}+1))" >> $GITHUB_ENV | |
echo "MINOR=0" >> $GITHUB_ENV | |
echo "PATCH=0" >> $GITHUB_ENV | |
- name: Minor version | |
if: contains(github.event.head_commit.message, 'minor') | |
run: | | |
echo "New minor version" | |
echo "MINOR=$((${{ env.MINOR }}+1))" >> $GITHUB_ENV | |
echo "PATCH=0" >> $GITHUB_ENV | |
- name: Patch version | |
if: contains(github.event.head_commit.message, 'patch') | |
run: | | |
echo "New patch version" | |
echo "PATCH=$((${{ env.PATCH }}+1))" >> $GITHUB_ENV | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@v4 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v4 | |
with: | |
context: "{{defaultContext}}" | |
push: true | |
tags: | | |
${{ env.IMAGE_NAME }}:${{ env.MAJOR}}.${{ env.MINOR}}.${{ env.PATCH}} | |
${{ env.IMAGE_NAME }}:latest | |
labels: ${{ steps.meta.outputs.labels }} | |
- name: Check out the repo | |
uses: actions/checkout@v3 | |
- name: Create folder if necessary | |
if: ${{ env.FILE_EXISTS == 'false' }} | |
run: mkdir -p .github/artifacts | |
- name: write_json | |
id: create-json | |
uses: jsdaniell/create-json@v1.2.2 | |
with: | |
name: "version.json" | |
json: '{ "major": ${{ env.MAJOR }}, "minor": ${{ env.MINOR }}, "patch": ${{ env.PATCH }} }' | |
dir: '.github/artifacts/' | |
- uses: stefanzweifel/git-auto-commit-action@v4 | |
with: | |
commit_message: update version.json (automated) | |