Only Build and Push Test Docker Image #5
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: Only Build and Push Test Docker Image | |
on: | |
workflow_dispatch: | |
inputs: | |
push_type: | |
description: 'Choose which images to push' | |
required: true | |
default: 'both' | |
type: choice | |
options: | |
- minimal | |
- both | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Cache Docker layers | |
uses: actions/cache@v4 | |
with: | |
path: /tmp/.buildx-cache | |
key: ${{ runner.os }}-buildx-${{ github.ref }}-${{ github.sha }} | |
restore-keys: | | |
${{ runner.os }}-buildx- | |
# 构建 baota:minimal | |
- name: Build minimal image | |
run: | | |
docker build -t eyunzhu/baota:minimal -t eyunzhu/baota:minimal_test -f ./dockerfiles/dockerfile.baota . || exit 1 | |
docker save eyunzhu/baota:minimal_test -o minimal_test.tar | |
# 验证 baota:minimal 是否存在 | |
- name: Verify images exist | |
run: | | |
if ! docker image inspect eyunzhu/baota:minimal > /dev/null 2>&1; then | |
echo "Error: eyunzhu/baota:minimal does not exist!" | |
exit 1 | |
fi | |
# Upload the Minimal image as an artifact | |
- name: Upload minimal image artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: minimal_test_image | |
path: minimal_test.tar | |
# 根据选择决定是否构建 lnmp_test | |
- name: Build lnmp image | |
if: ${{ github.event.inputs.push_type == 'both' }} | |
run: | | |
docker build -t eyunzhu/baota:lnmp_test -f ./dockerfiles/dockerfile.baota-lnmp . || exit 1 | |
docker save eyunzhu/baota:lnmp_test -o lnmp_test.tar | |
# 验证 baota:lnmp_test 是否存在 | |
- name: Verify images exist | |
if: ${{ github.event.inputs.push_type == 'both' }} | |
run: | | |
if ! docker image inspect eyunzhu/baota:lnmp_test > /dev/null 2>&1; then | |
echo "Error: eyunzhu/baota:lnmp_test does not exist!" | |
exit 1 | |
fi | |
# Upload the LNMP image as an artifact | |
- name: Upload lnmp image artifact | |
if: ${{ github.event.inputs.push_type == 'both' }} | |
uses: actions/upload-artifact@v4 | |
with: | |
name: lnmp_test_image | |
path: lnmp_test.tar | |
push_minimal: | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
# Download the Minimal image artifact | |
- name: Download minimal image artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: minimal_test_image | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
# Push the Minimal image to Docker Hub | |
- name: Push minimal image to Docker Hub | |
run: | | |
docker load -i minimal_test.tar | |
docker push eyunzhu/baota:minimal_test | |
push_lnmp: | |
runs-on: ubuntu-latest | |
needs: build | |
if: ${{ github.event.inputs.push_type == 'both' }} | |
steps: | |
# Download the LNMP image artifact | |
- name: Download lnmp image artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: lnmp_test_image | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
# Push the LNMP image to Docker Hub | |
- name: Push LNMP image to Docker Hub | |
run: | | |
docker load -i lnmp_test.tar | |
docker push eyunzhu/baota:lnmp_test | |