Skip to content

Continuous Delivery #83

Continuous Delivery

Continuous Delivery #83

Workflow file for this run

name: Continuous Delivery
on:
workflow_run:
workflows: [ "Continuous Integration" ]
types:
- completed
conclusions:
- success
permissions:
contents: write
packages: write
env:
MAVEN_DIR: ./
jobs:
delivery:
runs-on: ubuntu-latest
steps:
- name: Check out the code
uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: 21
distribution: 'zulu'
- name: Download Maven details artifact
uses: actions/download-artifact@v4
with:
github-token: ${{ github.token }}
repository: ${{ github.repository }}
run-id: ${{ github.event.workflow_run.id }}
name: maven-details
path: .
- name: Download New Docker image artifact
uses: actions/download-artifact@v4
with:
github-token: ${{ github.token }}
repository: ${{ github.repository }}
run-id: ${{ github.event.workflow_run.id }}
name: docker-image
path: .
- name: Setup
run: |
sudo apt-get install -y jq
set -a
source maven_details.env
set +a
echo "Loaded GROUP_ID: $GROUP_ID, ARTIFACT_ID: $ARTIFACT_ID, VERSION: $VERSION, IMAGE_NAME: $IMAGE_NAME"
if [[ -z "$GROUP_ID" || -z "$ARTIFACT_ID" || -z "$VERSION" ]]; then
echo "Error: One or more Maven coordinates (GROUP_ID, ARTIFACT_ID, VERSION) could not be extracted."
exit 1
fi
#Encode REPO
REPO=$(echo "${{ github.repository }}" | cut -d'/' -f2)
echo "image_name=$IMAGE_NAME" >> $GITHUB_ENV # <-- Export IMAGE_NAME to the GitHub environment
echo "group_id=$GROUP_ID" >> $GITHUB_ENV # <-- Export GROUP_ID to the GitHub environment
echo "artifact_id=$ARTIFACT_ID" >> $GITHUB_ENV # <-- Export GROUP_ID to the GitHub environment
echo "version=$VERSION" >> $GITHUB_ENV # <-- Export GROUP_ID to the GitHub environment
echo "REPO_NAME=$REPO" >> $GITHUB_ENV
- name: Convert path to URL encoding
run: |
ENCODED_PATH=$(echo "${{env.REPO_NAME}}/${{ env.group_id }}/${{ env.artifact_id }}" | sed 's/\//%2F/g')
echo "Encoded path: $ENCODED_PATH"
echo "ENCODED_PATH=$ENCODED_PATH" >> $GITHUB_ENV
- name: Load New Docker image locally
run: docker load -i blank-service-image.tar
- name: Verify if the new Docker image has been load success
id: check_local_image
run: |
if [[ "$(docker images -q ${{ env.image_name }})" == "" ]]; then
echo "Error: Local Docker image '${{ env.image_name }}' not found."
exit 1 # Salir con un código de error
fi
echo "Local Docker image '${{ env.image_name }}' exists."
- name: Log in to GitHub Packages
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: echo $GITHUB_TOKEN | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Get Docker image ID from Github Packages
id: get_local_image_id
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
RESPONSE=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/orgs/${{ github.repository_owner }}/packages/container/${{ env.ENCODED_PATH }}/versions")
VERSION_ID=$(echo "$RESPONSE" | jq -r ".[] | select(.metadata.container.tags[] == \"${{ env.version }}\") | .id")
if [ -z "$VERSION_ID" ]; then
echo "Error: Version ID for image tag '${{ env.version }}' not found."
echo "image_exists=false" >> $GITHUB_OUTPUT
exit 1
fi
echo "Found Version ID: $VERSION_ID"
echo "VERSION_ID=$VERSION_ID" >> $GITHUB_ENV
echo "image_exists=true" >> $GITHUB_OUTPUT
- name: Tag Docker image for GitHub Packages
run: |
docker tag ${{ env.image_name }} ghcr.io/${{ github.repository }}/${{ env.image_name }}
- name: Push Docker Image to GitHub Packages
run: |
docker push ghcr.io/${{ github.repository }}/${{ env.image_name }}
- name: Delete OLD Docker image from GitHub Packages
if: steps.get_local_image_id.outputs.image_exists == 'true'
run: |
DELETE_URL="https://api.github.com/orgs/${{ github.repository_owner }}/packages/container/${{ env.ENCODED_PATH }}/versions/${{ env.VERSION_ID }}"
curl -X DELETE -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "$DELETE_URL"