Home > Net >  Docker container doesn't have the same behaviour in Pipeline(grep,head,sed,awk)
Docker container doesn't have the same behaviour in Pipeline(grep,head,sed,awk)

Time:03-17

i got a docker container(docker:stable) where i want to get the version of a pom.xml file with the command:

grep version pom.xml | grep -v -e '<?xml|~'| head -n 1 | sed 's/[[:space:]]//g' | sed -E 's/<.{0,1}version>//g' | awk '{print $1}'

and it prints me out what i need 2021.1.13-SNAPSHOT

now if i do the same thing in a gitlab pipeline with the same image i get

<?xmlversion="1.0"encoding="UTF-8"?>

how come? Does anybody know anything about this behaviour?

As Requested by RavinderSingh13 here the pom.xml in question

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>at.hello</groupId>
    <artifactId>kanutr-3IT</artifactId>
    <version>2021.1.13-SNAPSHOT</version>
    <name>kanuTR_3IT</name>

    <organization>
        <name>hello GmbH</name>
        <url>https://www.hello.at/</url>
    </organization>

    <repositories>
        <repository>
            <id>hello-repo</id>
            <name>company internal artifacts</name>
            <url>https://services.hello.at/maven/hello-repo</url>
        </repository>
        <repository>
            <id>hello-extern</id>
            <name>company extern artifacts</name>
            <url>https://services.hello.at/maven/hello-extern</url>
        </repository>
    </repositories>

    <properties>
        <kanutr.version>2022.1.2-SNAPSHOT</kanutr.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.8.RELEASE</spring.version>
        <maven-jar-plugin.mainClass>de.hello.balu.Server</maven-jar-plugin.mainClass>
        <maven-compiler-plugin.source>1.8</maven-compiler-plugin.source>
        <maven-compiler-plugin.target>1.8</maven-compiler-plugin.target>

        <mapping-scripts.outputDirectory>${project.build.directory}/mapping_scripts</mapping-scripts.outputDirectory>
        <mapping-scripts.inputDirectory>${project.build.scriptSourceDirectory}/mapping_scripts</mapping-scripts.inputDirectory>
        <resources.outputDirectory>${project.build.directory}/resources</resources.outputDirectory>
        <resources.inputDirectory>${project.basedir}/src/main/roll-out-resources</resources.inputDirectory>
    </properties>

    <dependencies>
        <!-- Company intern dependencies -->
        <dependency>
            <groupId>at.hello</groupId>
            <artifactId>kanutr</artifactId>
            <version>${kanutr.version}</version>
        </dependency>
        <dependency>
            <groupId>at.hello</groupId>
            <artifactId>kanutr</artifactId>
            <version>${kanutr.version}</version>
            <classifier>tests</classifier>
            <scope>test</scope>
        </dependency>
        <!-- Dependency for mapping scripts in kanuTR project -->
        <dependency>
            <groupId>at.hello</groupId>
            <artifactId>kanutr</artifactId>
            <version>${kanutr.version}</version>
            <classifier>mappings</classifier>
            <type>tar.gz</type>
            <scope>provided</scope>
        </dependency>
        <!-- Dependency for some resources (Regis-TR XSDs) in kanuTR project -->
        <dependency>
            <groupId>at.hello</groupId>
            <artifactId>kanutr</artifactId>
            <version>${kanutr.version}</version>
            <classifier>resources</classifier>
            <type>tar.gz</type>
            <scope>provided</scope>
        </dependency>
        <!-- Dependency for scripts in kanuTR project -->
        <dependency>
            <groupId>at.hello</groupId>
            <artifactId>kanutr</artifactId>
            <version>${kanutr.version}</version>
            <classifier>scripts</classifier>
            <type>tar.gz</type>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.10</version>
            <scope>test</scope>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.mockito</groupId>-->
<!--            <artifactId>mockito-core</artifactId>-->
<!--            <version>2.7.22</version>-->
<!--            <scope>test</scope>-->
<!--        </dependency>-->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                            <mainClass>${maven-jar-plugin.mainClass}</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>${maven-compiler-plugin.source}</source>
                    <target>${maven-compiler-plugin.target}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20</version>
                <configuration>
                    <forkCount>0</forkCount>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>

                <configuration>
                    <tarLongFileMode>posix</tarLongFileMode>
                    <descriptors>
                        <descriptor>src/assembly/roll-out.xml</descriptor>
                        <descriptor>src/assembly/roll-out-mifid.xml</descriptor>
                    </descriptors>
                </configuration>

                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>

            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>at.hello</groupId>
                                    <artifactId>kanutr</artifactId>
                                    <classifier>mappings</classifier>
                                    <type>tar.gz</type>

                                    <includes>registr/**,deboe_apa/**</includes>
                                    <outputDirectory>${mapping-scripts.outputDirectory}</outputDirectory>
                                    <overWrite>true</overWrite>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>at.hello</groupId>
                                    <artifactId>kanutr</artifactId>
                                    <classifier>resources</classifier>
                                    <type>tar.gz</type>

                                    <outputDirectory>${resources.outputDirectory}</outputDirectory>
                                    <overWrite>true</overWrite>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${resources.outputDirectory}</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${resources.inputDirectory}</directory>
                                </resource>
                            </resources>
                            <overwrite>true</overwrite>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-scripts</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${mapping-scripts.outputDirectory}</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${mapping-scripts.inputDirectory}</directory>
                                </resource>
                            </resources>
                            <overwrite>true</overwrite>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

CodePudding user response:

1st solution: With your shown samples, please try following GNU awk code. Written and tested in GNU awk.

awk '
/<\?xml version="1\.0" encoding="UTF-8"\?>/{
  found=1
  next
}
found && match($0,/^[[:space:]]*<version>([^<]*)<\/version>/,arr){
  print arr[1]
  exit
}
' Input_file


2nd solution: Should work with any version of awk.

awk '
/<\?xml version="1\.0" encoding="UTF-8"\?>/{
  found=1
  next
}
found && match($0,/^[[:space:]]*<version>[^<]*<\/version>/,arr){
  val=substr($0,RSTART,RLENGTH)
  gsub(/.*<version>|<\/version>/,"",val)
  print val
  exit
}
'  Input_file


Explanation of 1st solution: Adding detailed explanation for above.

##Starting awk program from here.
awk '
##Checking condition if line contains <?xml version="1.0" encoding="UTF-8"?> then do following.
/<\?xml version="1\.0" encoding="UTF-8"\?>/{
##Setting found variable value to 1 here.
  found=1
##next will skip all further statements from here.
  next
}
##Checking condition if found is SET and using match function to match regex ^[[:space:]]*<version>([^<]*)<\/version> where needed value SNAPSHOT value is stored in array arr's 1st value.
found && match($0,/^[[:space:]]*<version>([^<]*)<\/version>/,arr){
##Printing array arr's 1st value here.
  print arr[1]
##Exitig from here, to save some cycles and time.
  exit
}
' Input_file ##Mentioning Input_file name here.
  • Related