|
| 1 | +name: CI Docker Build and Push for Terraform Generator |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - develop |
| 8 | + pull_request: |
| 9 | + branches: |
| 10 | + - main |
| 11 | + - develop |
| 12 | + workflow_dispatch: # Manual trigger |
| 13 | + inputs: |
| 14 | + RELEASE_VERSION: |
| 15 | + description: 'Release version (e.g., 1.0.0). The "v" prefix will be automatically added.' |
| 16 | + required: false |
| 17 | + default: '' |
| 18 | + |
| 19 | +jobs: |
| 20 | + ci-build: |
| 21 | + name: CI Docker Build |
| 22 | + runs-on: ubuntu-latest |
| 23 | + if: github.event_name != 'workflow_dispatch' # Only run on push or PR, skip manual dispatch |
| 24 | + |
| 25 | + steps: |
| 26 | + # Step 1: Checkout the repository |
| 27 | + - name: Checkout Repository |
| 28 | + uses: actions/checkout@v3 |
| 29 | + |
| 30 | + # Step 2: Set up Docker Image Tag |
| 31 | + - name: Set up Docker Image Tag |
| 32 | + id: tag_step |
| 33 | + run: | |
| 34 | + DATE_TAG="v$(date +'%Y.%m.%d')" |
| 35 | + # Create a default tag for CI builds that isn't meant to be pushed |
| 36 | + NEW_TAG="${DATE_TAG}-ci" |
| 37 | + echo "Docker Tag: ${NEW_TAG}" |
| 38 | + echo "tag=${NEW_TAG}" >> "$GITHUB_OUTPUT" |
| 39 | +
|
| 40 | + # Step 3: Build Docker Image (without push) |
| 41 | + - name: Build Docker Image (No Push) |
| 42 | + uses: docker/build-push-action@v5 |
| 43 | + with: |
| 44 | + file: ./infra_as_code/terraform_generator/backend/Dockerfile # Path to the single Dockerfile |
| 45 | + context: ./infra_as_code/terraform_generator/backend |
| 46 | + push: false |
| 47 | + tags: | |
| 48 | + sam123ben/terraform-generator:${{ steps.tag_step.outputs.tag }} |
| 49 | +
|
| 50 | + manual-build-push: |
| 51 | + name: Manual Docker Build and Push |
| 52 | + runs-on: ubuntu-latest |
| 53 | + if: github.event_name == 'workflow_dispatch' # Only run on manual trigger |
| 54 | + |
| 55 | + steps: |
| 56 | + # Step 1: Checkout the repository |
| 57 | + - name: Checkout Repository |
| 58 | + uses: actions/checkout@v3 |
| 59 | + |
| 60 | + # Step 2: Validate Release Version |
| 61 | + - name: Validate Release Version |
| 62 | + run: | |
| 63 | + if [ -z "${{ github.event.inputs.RELEASE_VERSION }}" ]; then |
| 64 | + echo "Error: Please provide a valid release version for manual build and push." |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +
|
| 68 | + # Step 3: Log in to Docker Hub using the Access Token |
| 69 | + - name: Log in to Docker Hub |
| 70 | + uses: docker/login-action@v2 |
| 71 | + with: |
| 72 | + username: ${{ secrets.DOCKER_USERNAME }} |
| 73 | + password: ${{ secrets.DOCKER_HUB_PAT_TOKEN }} |
| 74 | + |
| 75 | + # Step 4: Build and Push Docker Image |
| 76 | + - name: Build and Push Docker Image |
| 77 | + uses: docker/build-push-action@v5 |
| 78 | + with: |
| 79 | + file: ./infra_as_code/terraform_generator/backend/Dockerfile # Path to the single Dockerfile |
| 80 | + context: ./infra_as_code/terraform_generator/backend |
| 81 | + push: true |
| 82 | + tags: | |
| 83 | + sam123ben/terraform-generator:latest |
| 84 | + sam123ben/terraform-generator:${{ github.event.inputs.RELEASE_VERSION }} |
0 commit comments