Merge pull request #2 from MrKoopie/introducing-images #10
This file contains hidden or 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 | |
| 'on': | |
| pull_request: | |
| types: [opened, synchronize, reopened, closed] | |
| push: | |
| branches: | |
| - main | |
| schedule: | |
| - cron: "10 2 * * 0" | |
| jobs: | |
| # Test AlmaLinux 8 with both Ansible versions | |
| test-almalinux8: | |
| name: Test AlmaLinux 8 - Ansible ${{ matrix.ansible_version }} | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| ansible_version: [latest, core-2.16] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Check if AlmaLinux 8 files changed | |
| id: changed | |
| run: | | |
| if [ "${{ github.event_name }}" == "pull_request" ]; then | |
| git fetch origin ${{ github.base_ref }} | |
| if git diff --name-only \ | |
| origin/${{ github.base_ref }}...HEAD | \ | |
| grep -q "^docker-almalinux8-ansible/"; then | |
| echo "changed=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "changed=false" >> $GITHUB_OUTPUT | |
| fi | |
| else | |
| # Always build on main branch or cron | |
| echo "changed=true" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Set Ansible version build arg and tags | |
| id: ansible | |
| if: steps.changed.outputs.changed == 'true' | |
| run: | | |
| if [ "${{ matrix.ansible_version }}" == "latest" ]; then | |
| echo "build_arg=ansible" >> $GITHUB_OUTPUT | |
| echo "tag_suffix=latest" >> $GITHUB_OUTPUT | |
| else | |
| echo "build_arg=ansible-core~=2.16.0" >> $GITHUB_OUTPUT | |
| echo "tag_suffix=core-2.16" >> $GITHUB_OUTPUT | |
| fi | |
| # Set PR-specific tag if this is a pull request | |
| if [ "${{ github.event_name }}" == "pull_request" ]; then | |
| PR_NUMBER="${{ github.event.pull_request.number }}" | |
| echo "docker_tag=pr-${PR_NUMBER}-${{ matrix.ansible_version }}" \ | |
| >> $GITHUB_OUTPUT | |
| echo "should_push=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "should_push=false" >> $GITHUB_OUTPUT | |
| fi | |
| - uses: docker/setup-qemu-action@v3 | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'true' | |
| - uses: docker/setup-buildx-action@v3 | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'true' | |
| - name: Login to DockerHub | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'true' | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Build and push PR image | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'true' | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./docker-almalinux8-ansible | |
| file: ./docker-almalinux8-ansible/Dockerfile | |
| platforms: linux/amd64,linux/arm64 | |
| push: true | |
| tags: | | |
| mrkoopie/almalinux8-ansible:${{ steps.ansible.outputs.docker_tag }} | |
| build-args: | | |
| ANSIBLE_VERSION=${{ steps.ansible.outputs.build_arg }} | |
| - name: Test building image locally | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'false' | |
| run: | | |
| docker build \ | |
| --build-arg \ | |
| ANSIBLE_VERSION="${{ steps.ansible.outputs.build_arg }}" \ | |
| -t docker-ansible \ | |
| ./docker-almalinux8-ansible | |
| - name: Run the built image | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'false' | |
| run: >- | |
| docker run --name test-container -d --privileged | |
| -v /sys/fs/cgroup:/sys/fs/cgroup:rw --cgroupns=host | |
| docker-ansible | |
| - name: Verify Ansible is accessible in the built image | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'false' | |
| run: | | |
| docker exec --tty test-container \ | |
| env TERM=xterm ansible --version | |
| # Test AlmaLinux 9 with both Ansible versions | |
| test-almalinux9: | |
| name: Test AlmaLinux 9 - Ansible ${{ matrix.ansible_version }} | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| ansible_version: [latest, core-2.16] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Check if AlmaLinux 9 files changed | |
| id: changed | |
| run: | | |
| if [ "${{ github.event_name }}" == "pull_request" ]; then | |
| git fetch origin ${{ github.base_ref }} | |
| if git diff --name-only \ | |
| origin/${{ github.base_ref }}...HEAD | \ | |
| grep -q "^docker-almalinux9-ansible/"; then | |
| echo "changed=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "changed=false" >> $GITHUB_OUTPUT | |
| fi | |
| else | |
| # Always build on main branch or cron | |
| echo "changed=true" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Set Ansible version build arg and tags | |
| id: ansible | |
| if: steps.changed.outputs.changed == 'true' | |
| run: | | |
| if [ "${{ matrix.ansible_version }}" == "latest" ]; then | |
| echo "build_arg=ansible" >> $GITHUB_OUTPUT | |
| echo "tag_suffix=latest" >> $GITHUB_OUTPUT | |
| else | |
| echo "build_arg=ansible-core~=2.16.0" >> $GITHUB_OUTPUT | |
| echo "tag_suffix=core-2.16" >> $GITHUB_OUTPUT | |
| fi | |
| # Set PR-specific tag if this is a pull request | |
| if [ "${{ github.event_name }}" == "pull_request" ]; then | |
| PR_NUMBER="${{ github.event.pull_request.number }}" | |
| echo "docker_tag=pr-${PR_NUMBER}-${{ matrix.ansible_version }}" \ | |
| >> $GITHUB_OUTPUT | |
| echo "should_push=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "should_push=false" >> $GITHUB_OUTPUT | |
| fi | |
| - uses: docker/setup-qemu-action@v3 | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'true' | |
| - uses: docker/setup-buildx-action@v3 | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'true' | |
| - name: Login to DockerHub | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'true' | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Build and push PR image | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'true' | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./docker-almalinux9-ansible | |
| file: ./docker-almalinux9-ansible/Dockerfile | |
| platforms: linux/amd64,linux/arm64 | |
| push: true | |
| tags: | | |
| mrkoopie/almalinux9-ansible:${{ steps.ansible.outputs.docker_tag }} | |
| build-args: | | |
| ANSIBLE_VERSION=${{ steps.ansible.outputs.build_arg }} | |
| - name: Test building image locally | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'false' | |
| run: | | |
| docker build \ | |
| --build-arg \ | |
| ANSIBLE_VERSION="${{ steps.ansible.outputs.build_arg }}" \ | |
| -t docker-ansible \ | |
| ./docker-almalinux9-ansible | |
| - name: Run the built image | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'false' | |
| run: >- | |
| docker run --name test-container -d --privileged | |
| -v /sys/fs/cgroup:/sys/fs/cgroup:rw --cgroupns=host | |
| docker-ansible | |
| - name: Verify Ansible is accessible in the built image | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'false' | |
| run: | | |
| docker exec --tty test-container \ | |
| env TERM=xterm ansible --version | |
| # Test AlmaLinux 10 with both Ansible versions | |
| test-almalinux10: | |
| name: Test AlmaLinux 10 - Ansible ${{ matrix.ansible_version }} | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| ansible_version: [latest, core-2.16] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Check if AlmaLinux 10 files changed | |
| id: changed | |
| run: | | |
| if [ "${{ github.event_name }}" == "pull_request" ]; then | |
| git fetch origin ${{ github.base_ref }} | |
| if git diff --name-only \ | |
| origin/${{ github.base_ref }}...HEAD | \ | |
| grep -q "^docker-almalinux10-ansible/"; then | |
| echo "changed=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "changed=false" >> $GITHUB_OUTPUT | |
| fi | |
| else | |
| # Always build on main branch or cron | |
| echo "changed=true" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Set Ansible version build arg and tags | |
| id: ansible | |
| if: steps.changed.outputs.changed == 'true' | |
| run: | | |
| if [ "${{ matrix.ansible_version }}" == "latest" ]; then | |
| echo "build_arg=ansible" >> $GITHUB_OUTPUT | |
| echo "tag_suffix=latest" >> $GITHUB_OUTPUT | |
| else | |
| echo "build_arg=ansible-core~=2.16.0" >> $GITHUB_OUTPUT | |
| echo "tag_suffix=core-2.16" >> $GITHUB_OUTPUT | |
| fi | |
| # Set PR-specific tag if this is a pull request | |
| if [ "${{ github.event_name }}" == "pull_request" ]; then | |
| PR_NUMBER="${{ github.event.pull_request.number }}" | |
| echo "docker_tag=pr-${PR_NUMBER}-${{ matrix.ansible_version }}" \ | |
| >> $GITHUB_OUTPUT | |
| echo "should_push=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "should_push=false" >> $GITHUB_OUTPUT | |
| fi | |
| - uses: docker/setup-qemu-action@v3 | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'true' | |
| - uses: docker/setup-buildx-action@v3 | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'true' | |
| - name: Login to DockerHub | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'true' | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Build and push PR image | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'true' | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./docker-almalinux10-ansible | |
| file: ./docker-almalinux10-ansible/Dockerfile | |
| platforms: linux/amd64,linux/arm64 | |
| push: true | |
| tags: | | |
| mrkoopie/almalinux10-ansible:${{ steps.ansible.outputs.docker_tag }} | |
| build-args: | | |
| ANSIBLE_VERSION=${{ steps.ansible.outputs.build_arg }} | |
| - name: Test building image locally | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'false' | |
| run: | | |
| docker build \ | |
| --build-arg \ | |
| ANSIBLE_VERSION="${{ steps.ansible.outputs.build_arg }}" \ | |
| -t docker-ansible \ | |
| ./docker-almalinux10-ansible | |
| - name: Run the built image | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'false' | |
| run: >- | |
| docker run --name test-container -d --privileged | |
| -v /sys/fs/cgroup:/sys/fs/cgroup:rw --cgroupns=host | |
| docker-ansible | |
| - name: Verify Ansible is accessible in the built image | |
| if: | | |
| steps.changed.outputs.changed == 'true' && | |
| steps.ansible.outputs.should_push == 'false' | |
| run: | | |
| docker exec --tty test-container \ | |
| env TERM=xterm ansible --version | |
| # Release AlmaLinux 8 images to DockerHub | |
| release-almalinux8: | |
| name: Release AlmaLinux 8 - Ansible ${{ matrix.ansible_version }} | |
| runs-on: ubuntu-latest | |
| needs: test-almalinux8 | |
| if: github.ref == 'refs/heads/main' || github.event_name == 'schedule' | |
| strategy: | |
| matrix: | |
| ansible_version: [latest, core-2.16] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set Ansible version build arg and tag | |
| id: ansible | |
| run: | | |
| if [ "${{ matrix.ansible_version }}" == "latest" ]; then | |
| echo "build_arg=ansible" >> $GITHUB_OUTPUT | |
| echo "tag=latest" >> $GITHUB_OUTPUT | |
| else | |
| echo "build_arg=ansible-core~=2.16.0" >> $GITHUB_OUTPUT | |
| echo "tag=core-2.16" >> $GITHUB_OUTPUT | |
| fi | |
| - uses: docker/setup-qemu-action@v3 | |
| - uses: docker/setup-buildx-action@v3 | |
| - name: Login to DockerHub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Build and push image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./docker-almalinux8-ansible | |
| file: ./docker-almalinux8-ansible/Dockerfile | |
| platforms: linux/amd64,linux/arm64 | |
| push: true | |
| tags: mrkoopie/almalinux8-ansible:${{ steps.ansible.outputs.tag }} | |
| build-args: | | |
| ANSIBLE_VERSION=${{ steps.ansible.outputs.build_arg }} | |
| # Release AlmaLinux 9 images to DockerHub | |
| release-almalinux9: | |
| name: Release AlmaLinux 9 - Ansible ${{ matrix.ansible_version }} | |
| runs-on: ubuntu-latest | |
| needs: test-almalinux9 | |
| if: github.ref == 'refs/heads/main' || github.event_name == 'schedule' | |
| strategy: | |
| matrix: | |
| ansible_version: [latest, core-2.16] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set Ansible version build arg and tag | |
| id: ansible | |
| run: | | |
| if [ "${{ matrix.ansible_version }}" == "latest" ]; then | |
| echo "build_arg=ansible" >> $GITHUB_OUTPUT | |
| echo "tag=latest" >> $GITHUB_OUTPUT | |
| else | |
| echo "build_arg=ansible-core~=2.16.0" >> $GITHUB_OUTPUT | |
| echo "tag=core-2.16" >> $GITHUB_OUTPUT | |
| fi | |
| - uses: docker/setup-qemu-action@v3 | |
| - uses: docker/setup-buildx-action@v3 | |
| - name: Login to DockerHub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Build and push image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./docker-almalinux9-ansible | |
| file: ./docker-almalinux9-ansible/Dockerfile | |
| platforms: linux/amd64,linux/arm64 | |
| push: true | |
| tags: mrkoopie/almalinux9-ansible:${{ steps.ansible.outputs.tag }} | |
| build-args: | | |
| ANSIBLE_VERSION=${{ steps.ansible.outputs.build_arg }} | |
| # Release AlmaLinux 10 images to DockerHub | |
| release-almalinux10: | |
| name: Release AlmaLinux 10 - Ansible ${{ matrix.ansible_version }} | |
| runs-on: ubuntu-latest | |
| needs: test-almalinux10 | |
| if: github.ref == 'refs/heads/main' || github.event_name == 'schedule' | |
| strategy: | |
| matrix: | |
| ansible_version: [latest, core-2.16] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set Ansible version build arg and tag | |
| id: ansible | |
| run: | | |
| if [ "${{ matrix.ansible_version }}" == "latest" ]; then | |
| echo "build_arg=ansible" >> $GITHUB_OUTPUT | |
| echo "tag=latest" >> $GITHUB_OUTPUT | |
| else | |
| echo "build_arg=ansible-core~=2.16.0" >> $GITHUB_OUTPUT | |
| echo "tag=core-2.16" >> $GITHUB_OUTPUT | |
| fi | |
| - uses: docker/setup-qemu-action@v3 | |
| - uses: docker/setup-buildx-action@v3 | |
| - name: Login to DockerHub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Build and push image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./docker-almalinux10-ansible | |
| file: ./docker-almalinux10-ansible/Dockerfile | |
| platforms: linux/amd64,linux/arm64 | |
| push: true | |
| tags: mrkoopie/almalinux10-ansible:${{ steps.ansible.outputs.tag }} | |
| build-args: | | |
| ANSIBLE_VERSION=${{ steps.ansible.outputs.build_arg }} | |
| # Cleanup PR Docker images when PR is closed | |
| cleanup-pr-images: | |
| name: Cleanup PR Docker Images | |
| runs-on: ubuntu-latest | |
| if: github.event_name == 'pull_request' && github.event.action == 'closed' | |
| steps: | |
| - name: Delete PR Docker images from DockerHub | |
| env: | |
| DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} | |
| DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} | |
| PR_NUMBER: ${{ github.event.pull_request.number }} | |
| run: | | |
| echo "Cleaning up Docker images for PR #${PR_NUMBER}" | |
| # Authenticate with DockerHub and get JWT token | |
| echo "Authenticating with DockerHub..." | |
| JSON_PAYLOAD="{\"username\":\"${DOCKERHUB_USERNAME}\",\"password\":\"${DOCKERHUB_TOKEN}\"}" | |
| TOKEN_RESPONSE=$(curl -f -s -X POST \ | |
| https://hub.docker.com/v2/users/login/ \ | |
| -H "Content-Type: application/json" \ | |
| -d "${JSON_PAYLOAD}") | |
| JWT_TOKEN=$(echo "${TOKEN_RESPONSE}" | jq -r '.token // empty') | |
| if [ -z "$JWT_TOKEN" ]; then | |
| echo "Failed to authenticate with DockerHub" | |
| exit 1 | |
| fi | |
| echo "Authentication successful" | |
| # Delete images for all OS and Ansible version combinations | |
| OS_VERSIONS=(8 9 10) | |
| ANSIBLE_VERSIONS=(latest core-2.16) | |
| FAILED=0 | |
| for OS_VERSION in "${OS_VERSIONS[@]}"; do | |
| for ANSIBLE_VERSION in "${ANSIBLE_VERSIONS[@]}"; do | |
| REPO="mrkoopie/almalinux${OS_VERSION}-ansible" | |
| TAG="pr-${PR_NUMBER}-${ANSIBLE_VERSION}" | |
| echo "Deleting ${REPO}:${TAG}..." | |
| HTTP_CODE=$(curl -f -s -o /dev/null -w "%{http_code}" \ | |
| -X DELETE \ | |
| "https://hub.docker.com/v2/repositories/${REPO}/tags/${TAG}/" \ | |
| -H "Authorization: JWT ${JWT_TOKEN}") | |
| if [ "$HTTP_CODE" = "204" ]; then | |
| echo " ✓ Successfully deleted ${REPO}:${TAG}" | |
| elif [ "$HTTP_CODE" = "404" ]; then | |
| echo " ⚠ Tag not found: ${REPO}:${TAG} (may not exist)" | |
| else | |
| echo " ✗ Failed to delete ${REPO}:${TAG} (HTTP ${HTTP_CODE})" | |
| FAILED=1 | |
| fi | |
| done | |
| done | |
| if [ $FAILED -eq 1 ]; then | |
| echo "Some deletions failed" | |
| exit 1 | |
| fi | |
| echo "All PR images cleaned up successfully" |