Skip to content

add ghcr

add ghcr #4

Workflow file for this run

name: Docker CI
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
env:
DOCKER_REPO: file2md
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Generate tags
id: tags
run: |
COMMIT_HASH=$(git rev-parse --short HEAD)
TIMESTAMP=$(date +%s)
echo "commit_hash=$COMMIT_HASH" >> $GITHUB_OUTPUT
echo "timestamp=$TIMESTAMP" >> $GITHUB_OUTPUT
- name: Build Docker image
env:
COMMIT_HASH: ${{ steps.tags.outputs.commit_hash }}
TIMESTAMP: ${{ steps.tags.outputs.timestamp }}
run: |
docker build . --file Dockerfile \
--tag ${{ secrets.DOCKER_USERNAME }}/${{ env.DOCKER_REPO }}:$TIMESTAMP \
--tag ${{ secrets.DOCKER_USERNAME }}/${{ env.DOCKER_REPO }}:$COMMIT_HASH \
--tag ${{ secrets.DOCKER_USERNAME }}/${{ env.DOCKER_REPO }}:latest \
--tag ghcr.io/${{ github.repository_owner }}/${{ env.DOCKER_REPO }}:$TIMESTAMP \
--tag ghcr.io/${{ github.repository_owner }}/${{ env.DOCKER_REPO }}:$COMMIT_HASH \
--tag ghcr.io/${{ github.repository_owner }}/${{ env.DOCKER_REPO }}:latest
- name: Push to Docker Hub
env:
COMMIT_HASH: ${{ steps.tags.outputs.commit_hash }}
TIMESTAMP: ${{ steps.tags.outputs.timestamp }}
run: |
docker push ${{ secrets.DOCKER_USERNAME }}/${{ env.DOCKER_REPO }}:$TIMESTAMP
docker push ${{ secrets.DOCKER_USERNAME }}/${{ env.DOCKER_REPO }}:$COMMIT_HASH
docker push ${{ secrets.DOCKER_USERNAME }}/${{ env.DOCKER_REPO }}:latest
- name: Push to GitHub Container Registry
env:
COMMIT_HASH: ${{ steps.tags.outputs.commit_hash }}
TIMESTAMP: ${{ steps.tags.outputs.timestamp }}
run: |
docker push ghcr.io/${{ github.repository_owner }}/${{ env.DOCKER_REPO }}:$TIMESTAMP
docker push ghcr.io/${{ github.repository_owner }}/${{ env.DOCKER_REPO }}:$COMMIT_HASH
docker push ghcr.io/${{ github.repository_owner }}/${{ env.DOCKER_REPO }}:latest