name: Update Docker Image Tags and Release Helm Chart on: schedule: - cron: '0 2 * * 1' workflow_dispatch: jobs: update-and-release: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 with: fetch-depth: 0 - name: Install Helm uses: azure/setup-helm@v3 with: version: 'v3.12.0' - name: Check for new nginx version id: nginx run: | set -e echo "Checking nginx versions..." CURRENT=$(grep -A3 "nginx:" values.yaml | grep "tag:" | head -1 | sed 's/.*tag: *"\([^"]*\)".*/\1/' | tr -d ' ') echo "Current nginx: $CURRENT" LATEST=$(curl -s "https://registry.hub.docker.com/v2/repositories/library/nginx/tags?page_size=100&name=alpine-perl" | jq -r '.results[].name' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+-alpine-perl$' | sort -V | tail -1) if [ -z "$LATEST" ]; then echo "Warning: Could not fetch latest nginx version, using current" LATEST="$CURRENT" fi echo "Latest nginx: $LATEST" echo "current=$CURRENT" >> $GITHUB_OUTPUT echo "latest=$LATEST" >> $GITHUB_OUTPUT - name: Check for new PHP version id: php run: | set -e echo "Checking PHP versions..." CURRENT=$(grep -A3 "php:" values.yaml | grep "tag:" | head -1 | sed 's/.*tag: *"\([^"]*\)".*/\1/' | tr -d ' ') echo "Current PHP: $CURRENT" LATEST=$(curl -s "https://registry.hub.docker.com/v2/repositories/library/php/tags?page_size=100&name=fpm-alpine" | jq -r '.results[].name' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+-php[0-9]+\.[0-9]+-fpm-alpine$' | sort -V | tail -1) if [ -z "$LATEST" ]; then echo "Warning: Could not fetch latest PHP version, using current" LATEST="$CURRENT" fi APP_VERSION=$(echo "$LATEST" | grep -oE '^[0-9]+\.[0-9]+\.[0-9]+') if [ -z "$APP_VERSION" ]; then echo "Error: Could not extract PHP version" exit 1 fi echo "Latest PHP: $LATEST" echo "PHP version: $APP_VERSION" echo "current=$CURRENT" >> $GITHUB_OUTPUT echo "latest=$LATEST" >> $GITHUB_OUTPUT echo "app_version=$APP_VERSION" >> $GITHUB_OUTPUT - name: Determine if update is needed id: check_update run: | set -e NGINX_CURRENT="${{ steps.nginx.outputs.current }}" NGINX_LATEST="${{ steps.nginx.outputs.latest }}" APP_CURRENT="${{ steps.php.outputs.current }}" APP_LATEST="${{ steps.php.outputs.latest }}" echo "Nginx: $NGINX_CURRENT vs $NGINX_LATEST" echo "PHP: $APP_CURRENT vs $APP_LATEST" if [ "$NGINX_CURRENT" != "$NGINX_LATEST" ] || [ "$APP_CURRENT" != "$APP_LATEST" ]; then echo "update_needed=true" >> $GITHUB_OUTPUT echo "Update is needed" else echo "update_needed=false" >> $GITHUB_OUTPUT echo "Already up to date" fi - name: Update values.yaml if: steps.check_update.outputs.update_needed == 'true' run: | set -e echo "Updating values.yaml..." NGINX_OLD="${{ steps.nginx.outputs.current }}" NGINX_NEW="${{ steps.nginx.outputs.latest }}" sed -i "s|tag: \"${NGINX_OLD}\"|tag: \"${NGINX_NEW}\"|g" values.yaml APP_OLD="${{ steps.php.outputs.current }}" APP_NEW="${{ steps.php.outputs.latest }}" sed -i "s|tag: \"${APP_OLD}\"|tag: \"${APP_NEW}\"|g" values.yaml echo "values.yaml updated" git diff values.yaml - name: Update Chart.yaml version if: steps.check_update.outputs.update_needed == 'true' run: | set -e APP_VERSION="${{ steps.php.outputs.app_version }}" sed -i "s/^version: .*/version: $APP_VERSION/" Chart.yaml sed -i "s/^appVersion: .*/appVersion: \"$APP_VERSION\"/" Chart.yaml echo "Chart.yaml updated to version $APP_VERSION" cat Chart.yaml - name: Commit changes if: steps.check_update.outputs.update_needed == 'true' run: | git config user.name "GitHub Actions Bot" git config user.email "actions@github.com" git add values.yaml Chart.yaml git commit -m "chore: update to PHP ${{ steps.php.outputs.app_version }}" git push origin main - name: Package Helm Chart if: steps.check_update.outputs.update_needed == 'true' run: | helm package . echo "Helm chart packaged" - name: Create Git Tag if: steps.check_update.outputs.update_needed == 'true' run: | APP_VERSION="${{ steps.php.outputs.app_version }}" git tag -a "v$APP_VERSION" -m "Release PHP $APP_VERSION" git push origin "v$APP_VERSION" echo "Git tag v$APP_VERSION created" - name: Create Gitea Release if: steps.check_update.outputs.update_needed == 'true' env: GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} run: | APP_VERSION="${{ steps.php.outputs.app_version }}" CHART_NAME=$(grep '^name:' Chart.yaml | awk '{print $2}') PACKAGE_FILE="${CHART_NAME}-${APP_VERSION}.tgz" RELEASE_BODY="PHP Helm Chart v${APP_VERSION} - Automated release" curl -X POST -H "Authorization: token ${GITEA_TOKEN}" -H "Content-Type: application/json" -d "{\"tag_name\":\"v${APP_VERSION}\",\"name\":\"v${APP_VERSION}\",\"body\":\"${RELEASE_BODY}\"}" "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases" RELEASE_ID=$(curl -s -H "Authorization: token ${GITEA_TOKEN}" "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases/tags/v${APP_VERSION}" | jq -r '.id') curl -X POST -H "Authorization: token ${GITEA_TOKEN}" -H "Content-Type: application/gzip" --data-binary "@${PACKAGE_FILE}" "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}/assets?name=${PACKAGE_FILE}" echo "Release created" - name: Update Helm Repository Index if: steps.check_update.outputs.update_needed == 'true' run: | set -e APP_VERSION="${{ steps.php.outputs.app_version }}" CHART_NAME=$(grep '^name:' Chart.yaml | awk '{print $2}') PACKAGE_FILE="${CHART_NAME}-${APP_VERSION}.tgz" echo "Preparing Helm repository update..." # パッケージファイルを一時ディレクトリに移動 mkdir -p /tmp/helm-repo cp "${PACKAGE_FILE}" /tmp/helm-repo/ # gh-pagesブランチの処理 if git ls-remote --heads origin gh-pages | grep gh-pages; then echo "gh-pages branch exists, checking out..." git fetch origin gh-pages git checkout gh-pages else echo "Creating new gh-pages branch..." git checkout --orphan gh-pages git rm -rf . || true echo "# Helm Repository" > README.md git add README.md git config user.name "GitHub Actions Bot" git config user.email "actions@github.com" git commit -m "Initialize gh-pages branch" git push origin gh-pages fi # パッケージファイルをコピー cp /tmp/helm-repo/"${PACKAGE_FILE}" . # index.yamlを生成/更新 helm repo index . --url "https://gitea.example.com/${GITHUB_REPOSITORY}/raw/branch/gh-pages" # コミットしてプッシュ git config user.name "GitHub Actions Bot" git config user.email "actions@github.com" git add "${PACKAGE_FILE}" index.yaml git commit -m "chore: add ${CHART_NAME} v${APP_VERSION}" || echo "No changes to commit" git push origin gh-pages echo "Helm repository updated successfully" # mainブランチに戻る git checkout main - name: Summary if: steps.check_update.outputs.update_needed == 'true' run: | APP_VERSION="${{ steps.php.outputs.app_version }}" echo "Update completed to version ${APP_VERSION}"