name: Docker build and push on: push: jobs: build: name: Build Docker image runs-on: ubuntu-latest env: IMAGE_TAG: ghcr.io/${{ github.repository_owner }}/tdlightbotapi steps: - name: Checkout current repo uses: actions/checkout@v2 with: submodules: "recursive" - name: Get version run: | # Get latest commit short hash HASH_VERSION=$(git rev-parse --short HEAD) # Strip git ref prefix from version VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') # Strip "v" prefix from tag name [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') # Use Docker `latest` tag convention [ "$VERSION" == "master" ] && VERSION=latest # Convert IMAGE_TAG, HASH_VERSION and VERSION to lowercase (repository name must be lowercase) IMAGE_TAG=$(echo "$IMAGE_TAG" | awk '{print tolower($0)}') HASH_VERSION=$(echo "$HASH_VERSION" | awk '{print tolower($0)}') VERSION=$(echo "$VERSION" | awk '{print tolower($0)}') ARCH=${{ matrix.arch }} SAFE_ARCH=${ARCH///} # linux/amd64 -> linuxamd64 # Store variable for future use echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV echo "HASH_VERSION=$HASH_VERSION" >> $GITHUB_ENV echo "VERSION=$VERSION" >> $GITHUB_ENV echo "SAFE_ARCH=$SAFE_ARCH" >> $GITHUB_ENV # Print debug info echo "hash version: $HASH_VERSION" echo "version: $VERSION" echo "safe arch: $SAFE_ARCH" # Save env to file cat $GITHUB_ENV > github.env - name: Login to ghcr registry uses: docker/login-action@v1 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GH_ACCESS_TOKEN }} - name: Build and push image run: | docker build . -t ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }} docker push ${{ env.IMAGE_TAG }}:${{ env.HASH_VERSION }}